Skip to content

Commit

Permalink
Merge pull request #396 from flathub/revert-386-git-safe-directory-fo…
Browse files Browse the repository at this point in the history
…r-manifest-in-subdirectory

Don't run git against dubious-ownership repo
  • Loading branch information
wjt committed Nov 9, 2023
2 parents 3ec5bc0 + ac5a2ca commit 9dd1e12
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 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,33 +128,38 @@ def check_call(args):
subprocess.check_call(args)


def get_manifest_git_checkout(manifest: t.Union[Path, str]) -> str:
manifest_dir = os.path.dirname(manifest)
output = subprocess.check_output(
["git", "rev-parse", "--show-toplevel"],
cwd=manifest_dir,
)
return output.decode("utf-8").strip()
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.
for directory in Path(manifest).parents:
if os.path.exists(directory / ".git"):
return directory

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 9dd1e12

Please sign in to comment.