diff mbox series

scripts/oe-setup-layers: Update how to determine if directory is git repo

Message ID 20231003145636.1031-1-jermain.horsman@nedap.com
State Accepted, archived
Commit 0830f53900dd7fd669a7d6492325559ad3225302
Headers show
Series scripts/oe-setup-layers: Update how to determine if directory is git repo | expand

Commit Message

jhatnedap@gmail.com Oct. 3, 2023, 2:56 p.m. UTC
From: Jermain Horsman <jermain.horsman@nedap.com>

Previously _is_repo_git_repo() would return a result containing b'true\n' or
b'false\n' if 'git rev-parse' ran successfully, instead of True of False.

While this can be solved using e.g. result.strip().decode("utf-8") == "true",
there are some other cases to consider.
First, .git can be a file and not a directory when using a worktree.
Second, an emtpy .git directory in 'repodir' for which some parent of
'repodir' is an actual git repo will still return True in this case.

To account for these cases as well, use 'git rev-parse --show-toplevel'
and compare the result against 'repodir' instead of
using 'git rev-parse --is-inside-git-dir'.

Signed-off-by: Jermain Horsman <jermain.horsman@nedap.com>
---
 scripts/oe-setup-layers | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/scripts/oe-setup-layers b/scripts/oe-setup-layers
index c8012fa670..6d49688a32 100755
--- a/scripts/oe-setup-layers
+++ b/scripts/oe-setup-layers
@@ -20,13 +20,13 @@  import os
 import subprocess
 
 def _is_repo_git_repo(repodir):
-    git_dir = os.path.join(repodir, ".git")
-    if not os.access(git_dir, os.R_OK):
-        return False
     try:
-        return subprocess.check_output("git -C %s rev-parse --is-inside-git-dir" % git_dir, shell=True, stderr=subprocess.DEVNULL)
+        curr_toplevel = subprocess.check_output("git -C %s rev-parse --show-toplevel" % repodir, shell=True, stderr=subprocess.DEVNULL)
+        if curr_toplevel.strip().decode("utf-8") == repodir:
+            return True
     except subprocess.CalledProcessError:
-        return False
+        pass
+    return False
 
 def _is_repo_at_rev(repodir, rev):
     try: