git_sha1_gen.py: add submodule .git directory

When submodules are enabled, .git is a git link instead of a directory.
We should find the new git directory location and then find HEAD
revision there.

Bug: b/253329946
Change-Id: I0ec69df186e0f0d106689f78792854db5858d31d
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/mesa/+/818669
Reviewed-by: Craig Stout <cstout@google.com>
diff --git a/bin/git_sha1_gen.py b/bin/git_sha1_gen.py
index a457b31..28e32cb 100644
--- a/bin/git_sha1_gen.py
+++ b/bin/git_sha1_gen.py
@@ -12,6 +12,13 @@
 def get_git_sha1():
     """Try to get the git SHA1 with git rev-parse."""
     git_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', '.git')
+    # When submodules are enabled, .git is a file, not a directory.
+    if not os.path.isdir(git_dir):
+      with open(git_dir) as f:
+        git_subm_dir = f.readlines()[0].split()[1]
+      with open(os.path.join(git_subm_dir, "HEAD")) as f:
+        git_sha1=f.readlines()[0]
+      return git_sha1
     try:
         git_sha1 = subprocess.check_output([
             'git',