From ea2de4a88d768b7e5c7426c676e079c5b32dc337 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Thu, 9 Nov 2023 12:35:16 +0000 Subject: [PATCH] Use Path type when manipulating Git checkout Previously ensure_git_safe_directory() was declared to accept either str or Path. In fact, if passed a Path, it would fail to detect the case when the directory is already marked as safe, because Path(x) in [x] is False. Instead use Path throughout --- src/main.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main.py b/src/main.py index 5558eb85..5e9b8507 100755 --- a/src/main.py +++ b/src/main.py @@ -45,7 +45,7 @@ @contextlib.contextmanager -def indir(path): +def indir(path: Path): """ >>> with indir(path): ... # code executes with 'path' as working directory @@ -128,7 +128,7 @@ def check_call(args): subprocess.check_call(args) -def get_manifest_git_checkout(manifest: t.Union[Path, str]) -> str: +def get_manifest_git_checkout(manifest: t.Union[Path, str]) -> Path: # Can't use git rev-parse --show-toplevel because of a chicken-and-egg problem: we # need to find the checkout directory so that we can mark it as safe so that we can # use git against it. @@ -139,24 +139,27 @@ def get_manifest_git_checkout(manifest: t.Union[Path, str]) -> str: raise FileNotFoundError(f"Cannot find git checkout for {manifest}") -def ensure_git_safe_directory(checkout: t.Union[Path, str]): +def ensure_git_safe_directory(checkout: Path): uid = os.getuid() checkout_uid = os.stat(checkout).st_uid if uid == checkout_uid: return try: - safe_dirs_output = subprocess.check_output( - ["git", "config", "--get-all", "safe.directory"] + result = subprocess.run( + ["git", "config", "--get-all", "safe.directory"], + check=True, + capture_output=True, + encoding="utf-8", ) + safe_dirs = [Path(x) for x in result.stdout.splitlines()] except subprocess.CalledProcessError as err: # git config --get-all will return 1 if the key doesn't exist. # Re-raise the error for anything else. if err.returncode != 1: raise - safe_dirs_output = b"" + safe_dirs = [] - safe_dirs = safe_dirs_output.decode("utf-8").split() if checkout in safe_dirs: return