Skip to content

Commit

Permalink
Use Path type when manipulating Git checkout
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wjt committed Nov 9, 2023
1 parent 083add2 commit ea2de4a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@


@contextlib.contextmanager
def indir(path):
def indir(path: Path):
"""
>>> with indir(path):
... # code executes with 'path' as working directory
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down

0 comments on commit ea2de4a

Please sign in to comment.