diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-11-20 12:33:14 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-20 12:33:14 -0800 |
| commit | ed60e06e2489b50db8d4d4c9144f7fbd577f153d (patch) | |
| tree | 0cc162d3d7c3e699f60e078e11421d79694c6cb4 | |
| parent | 4f2303046518925cde9907d25e23bf85645849eb (diff) | |
| parent | ff2b81351ae2778c9cda5f5b600269410fd0331c (diff) | |
Merge pull request #7177 from anthropics/mhegazy/sg-other-repo-settings
security-guidance: skip other repositories whose settings change what git runs
| -rw-r--r-- | plugins/security-guidance/hooks/diffstate.py | 16 | ||||
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 639 | ||||
| -rw-r--r-- | plugins/security-guidance/hooks/reporesolve.py | 25 | ||||
| -rwxr-xr-x | plugins/security-guidance/hooks/security_reminder_hook.py | 43 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/conftest.py | 6 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_bare_repo_toplevel.py | 8 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_other_repo_config.py | 1508 |
7 files changed, 2180 insertions, 65 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py index a94363f4..932cabe2 100644 --- a/plugins/security-guidance/hooks/diffstate.py +++ b/plugins/security-guidance/hooks/diffstate.py @@ -19,7 +19,7 @@ from _base import debug_log, make_state_dir, portable_output, _PV import gitutil from gitutil import ( GIT_CMD, - _git_dir, _git_toplevel, _found_through_bare_folder, _git_status_porcelain, + _git_dir, _git_toplevel, _stay_out, _git_status_porcelain, _git_rev_parse_head, _is_ancestor, _git_name_only, _temp_index, _secret_rename_origins, index_entry_count, ) @@ -450,6 +450,8 @@ def get_baseline_file_content(session_id, file_path, cwd): # Windows for any file that is not directly in the session # directory. rel_path = rel_path.replace(os.sep, "/") + if _stay_out(git_cwd or cwd_abs): + return None result = subprocess.run( [*GIT_CMD, "show", f"{baseline_sha}:./{rel_path}"], cwd=git_cwd, capture_output=True, timeout=5 @@ -511,7 +513,9 @@ def _snapshot_commit(cwd, env, head_tree): # No pathspec: `add -u` has staged the whole work tree, whatever directory # of it `cwd` is, since git 2.0. `-- :/` would say the same but fails # outright under a user's GIT_LITERAL_PATHSPECS. - add = run(["add", "-u"]) + # Not for files in a directory that a sparse index stores as one entry. + sparse = gitutil.may_be_sparse(env["GIT_INDEX_FILE"]) + add = run([*(gitutil.SPARSE_AS_IT_IS if sparse else []), "add", "-u"]) if add.returncode != 0: debug_log(f"Snapshot: add -u failed: {_stderr_of(add)}") return None @@ -581,8 +585,8 @@ def capture_git_baseline(cwd): global last_baseline_fail, last_index_entries last_baseline_fail = 0 last_index_entries = None - if _found_through_bare_folder(cwd): - debug_log("Not an ordinary checkout, skipping baseline capture") + if _stay_out(cwd): + debug_log("Not a checkout to run git in, skipping baseline capture") return None try: # HEAD's commit and its tree in one call: the commit is the answer @@ -1383,7 +1387,7 @@ def _list_untracked(cwd): gitutil.py already follow the lenient pattern; this function and capture_git_baseline / _git_name_only / _git_status_porcelain were the holdouts.""" - if _found_through_bare_folder(cwd): + if _stay_out(cwd): return {} try: repo = _git_toplevel(cwd) or cwd @@ -1458,7 +1462,7 @@ def compute_v2_review_set(cwd, baseline_sha, head_at_capture, untracked_at_basel if not isinstance(untracked_at_baseline, dict): untracked_at_baseline = {} - tracked_dirty, untracked = (None, None) if _found_through_bare_folder(cwd) else _git_status_porcelain(repo) + tracked_dirty, untracked = (None, None) if _stay_out(cwd) else _git_status_porcelain(repo) if tracked_dirty is None: return [], "HEAD", repo, [], {"dirty_now_count": -1, "changed_since_count": -1, "review_set_count": 0} diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index 9f519dc3..13da15f1 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -16,9 +16,11 @@ Functions that DO compose patched leaves (``compute_v2_review_set``, """ import contextlib import errno +import hashlib import os import re import stat +import struct import subprocess import sys import tempfile @@ -26,6 +28,7 @@ import threading import time from _base import debug_log +from extensibility import _same_dir def _git_program(windows=os.name == "nt"): @@ -98,6 +101,9 @@ GIT_CMD = [ # With log.showSignature set they would also verify each signed commit, # adding lines to the output and starting the program gpg.program names. "-c", "log.showSignature=false", + # A `git show` without a format takes format.pretty, whose %G? verifies + # the signature too. + "-c", "format.pretty=medium", ] # diff.ignoreSubmodules is a default that the repository's own settings for @@ -111,6 +117,7 @@ SAFE_GIT_CONFIG = ( ("diff.ignoreSubmodules", "all"), ("diff.submodule", "short"), ("log.showSignature", "false"), + ("format.pretty", "medium"), ("safe.bareRepository", "explicit"), ) @@ -335,8 +342,8 @@ _path_list_too_long = set() # metrics: kind * 100000 + the paths the add was given. One number because a # review row is near CC's key cap. kind: 1 git could not write the empty blob # to the object store and the retry failed too, 2 any other git error, 3 -# timeout, 4 the retry with a private object directory worked. A git that -# speaks another language reports a failed retry as 2. With 1 to 3 the new +# timeout, 4 the retry with a private object directory worked, 5 there was no +# index copy without nested repositories to add them to. A git that speaks another language reports a failed retry as 2. But for 4 the new # files are not in the diff, so a turn that only added files looks like a turn # with no changes. `left_out` says so for the last get_git_diff alone: the code # stays set through a fallback diff that worked. @@ -712,7 +719,7 @@ def _add_new_files(cwd, paths, env, tmp_index): def add(add_env): results = _run_git_on_review_paths( - ["add", "--intent-to-add"], paths, timeout=10, cwd=cwd, env=add_env) + [*(SPARSE_AS_IT_IS if may_be_sparse(tmp_index) else []), "add", "--intent-to-add"], paths, timeout=10, cwd=cwd, env=add_env) return next((r for r in results if r.returncode != 0), None) try: @@ -806,11 +813,17 @@ def _copied_index(cwd, top, real_index, tmpdir, untracked_paths): env = {**os.environ, "GIT_INDEX_FILE": tmp_index} # Before `git add` runs, since add reads the gitlinks its paths cover. if not _drop_gitlinks(top, env, tmp_index): - debug_log("could not remove gitlinks from the index copy; going on " - "without the copy") + debug_log("no index copy that is known to hold no gitlinks; going on " + "without one") + if untracked_paths: + _note_add_failure(5, untracked_paths) yield None return if untracked_paths is None: + if may_be_sparse(tmp_index): + debug_log("no whole-tree add on a sparse index: it would take it apart") + yield None + return add_args = ["."] elif untracked_paths: # `git add -N -- a b nonexistent` is atomic — one missing path @@ -845,6 +858,22 @@ _GITLINK_MODE = b"\x00\x00\xe0\x00" _SPLIT_INDEX_EXT = b"link" +# And of the one, always empty, that says a directory's entries can be stored +# as one (a sparse index). +_SPARSE_INDEX_EXT = b"sdir\0\0\0\0" + + +def may_be_sparse(index_path): + """False only when the index file holds no directory stored as one entry. + git takes it apart when the directory is there on disk, at a time of its + own choosing, and a gitlink can come out of it.""" + return _index_holds(index_path, _SPARSE_INDEX_EXT) + + +# Keeps git from taking a sparse index apart for files that are there on disk. +SPARSE_AS_IT_IS = ["-c", "sparse.expectFilesOutsideOfPatterns=true"] + + def _may_have_gitlinks(index_path): """False only when the index file at `index_path` cannot hold a gitlink entry: neither a gitlink's mode bytes nor the split-index signature occur @@ -852,22 +881,163 @@ def _may_have_gitlinks(index_path): size, a path), so True means "list the entries and see". Most repositories have no gitlink, and for them this saves a git process on every index copy.""" - keep = max(len(_GITLINK_MODE), len(_SPLIT_INDEX_EXT)) - 1 + return _index_holds(index_path, _GITLINK_MODE, _SPLIT_INDEX_EXT) + + +def _open_file(path): + """open(path, "rb"). OSError for anything but a file: opening a named pipe + would wait for someone to write to it, and a device may have no end.""" + fd = os.open(path, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_BINARY", 0)) + if not stat.S_ISREG(os.fstat(fd).st_mode): + os.close(fd) + raise OSError(errno.EINVAL, "not a file", path) + return open(fd, "rb") + + +def _index_holds(index_path, *marks): + keep = max(map(len, marks)) - 1 try: - with open(index_path, "rb") as f: + with _open_file(index_path) as f: tail = b"" while True: chunk = f.read(1 << 20) if not chunk: return False data = tail + chunk - if _GITLINK_MODE in data or _SPLIT_INDEX_EXT in data: + if any(mark in data for mark in marks): return True tail = data[-keep:] except OSError: return True +# A larger index belongs to a repository that is big enough to ask git whether +# it is the session's own, before reading all of it here. +_BIG_INDEX_BYTES = 1 << 20 +# And this is more than is read here at all. +_MAX_INDEX_BYTES = 64 << 20 +_FILE_MODES = (0o100644, 0o100755, 0o120000) + + +def _file_entries(data, id_len=20): + """(index file with only the entries for files in the index file `data`, + number of gitlinks left out). ValueError when it has anything else that + changes how git reads it. Git reads an index by more than its entries: the + extensions say where to find them, and a mode or a name that git would not + write is taken for something else by one command and not by the next. So + an index that came with the files is not searched, it is written anew. + `id_len` is the length of an object id, and of the checksum.""" + if data[:4] != b"DIRC": + raise ValueError("not an index file") + version, count = struct.unpack_from(">II", data, 4) + fixed_len = 42 + id_len + if version not in (2, 3, 4) or len(data) < 12 + fixed_len * count + id_len: + raise ValueError(f"index version {version}") + end = len(data) - id_len + out, kept, left_out, extended, name, at = [], 0, 0, False, b"", 12 + size = 0 + for _ in range(count): + start = at + mode, flags = struct.unpack_from(">I", data, at + 24)[0], struct.unpack_from(">H", data, at + 40 + id_len)[0] + at += fixed_len + (2 if flags & 0x4000 else 0) + fixed = data[start:at] + if version == 4: + # How much to take off the previous name, then the rest. + cut = data[at] & 127 + while data[at] & 128 and cut <= len(name): # it only grows + at += 1 + cut = ((cut + 1) << 7) | (data[at] & 127) + at += 1 + if cut > len(name): + raise ValueError("name") + stop = data.index(b"\0", at, end) + name, at = name[:len(name) - cut] + data[at:stop], stop + 1 + else: + stop = data.index(b"\0", at, end) + name = data[at:stop] + at = start + ((stop - start + 8) & ~7) + if min(len(name), 0xFFF) != flags & 0xFFF: + raise ValueError("name length") + size += len(name) + 72 # gitlinks too: a name can repeat the one before it + if size > _MAX_INDEX_BYTES: + raise ValueError("size") + if mode & 0o170000 == 0o160000: + left_out += 1 + continue + if mode not in _FILE_MODES: + raise ValueError(f"mode {mode:o}") + extended = extended or bool(flags & 0x4000) + out.append(fixed + name + b"\0" * (8 - (len(fixed) + len(name)) % 8)) + kept += 1 + while at < end: # extensions: none of those git may not skip + if not 65 <= data[at] <= 90: + raise ValueError("extension") + at += 8 + struct.unpack_from(">I", data, at + 4)[0] + if at != end: + raise ValueError("length") + new = b"".join([b"DIRC", struct.pack(">II", 3 if extended else 2, kept), *out]) + return new + _ID_HASHES[id_len](new).digest(), left_out + + +_ID_HASHES = {20: hashlib.sha1, 32: hashlib.sha256} +_OBJECT_FORMATS = {b"sha1": 20, b"--show-object-format": 20, b"sha256": 32} + + +def _write_file_entries(top, tmp_index): + """Replace the index copy with one of only the entries for files. Returns + how many gitlinks that left out, or None when it was not replaced.""" + try: + was = os.stat(tmp_index) + if was.st_size > _MAX_INDEX_BYTES: + raise ValueError("size") + # Object ids of another length put every entry somewhere else. git + # before 2.25 has only the usual ones and prints the option. + r = subprocess.run([*GIT_CMD, "rev-parse", "--show-object-format"], + cwd=top, capture_output=True, timeout=5) + id_len = _OBJECT_FORMATS.get(r.stdout.strip()) if r.returncode == 0 else None + if not id_len: + raise ValueError(f"object format {r.stdout[:20]!r}") + with _open_file(tmp_index) as f: + data = f.read(_MAX_INDEX_BYTES + 1) + if len(data) > _MAX_INDEX_BYTES: + raise ValueError("size") + new, left_out = _file_entries(data, id_len) + # Not in place: the copy can be a hard link to the real index. + new_file = tmp_index + ".files" + with open(os.open(new_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0), 0o600), "wb") as f: + f.write(new) + # Keep the mtime the copy came with; see _copied_index. + os.utime(new_file, ns=(was.st_mtime_ns, was.st_mtime_ns)) + os.replace(new_file, tmp_index) + return left_out + except (subprocess.TimeoutExpired, OSError, ValueError, IndexError, struct.error, MemoryError) as e: + debug_log(f"not an index that can be written anew: {type(e).__name__}: {e}") + return None + + +def _is_session_checkout(top, project): + """True for the checkout the session was started in.""" + home = _folder_with_git_entry(project) + return bool(home and _same_dir(top, home)) + + +def _is_kept_for_session(top, index, ask=False): + """True for a checkout whose index git itself writes: the one the session + was started in, and the linked worktrees and submodules of its repository. + Those are asked about only for a big index, or with `ask`: it takes git + longer than it takes to write a small index anew.""" + project = _project_folder() + # An absolute GIT_DIR is one repository for every folder, the user's choice. + if not project or os.path.isabs(os.environ.get("GIT_DIR") or "") or _is_session_checkout(top, project): + return True + if not ask and os.path.getsize(index) <= _BIG_INDEX_BYTES: + return False + try: + return _is_session_repo(top, project) + except (subprocess.TimeoutExpired, OSError, ValueError): + return False + + _GITLINK_LIST_TIMEOUT = 10 # `git ls-files --sparse` (git 2.35) lists a sparse index as it is stored; # without it git first expands the index to every path in the repository. @@ -940,6 +1110,15 @@ def _drop_gitlinks(top, env, tmp_index): global last_dropped_gitlinks last_dropped_gitlinks = False try: + if not _is_kept_for_session(top, tmp_index): + left_out = _write_file_entries(top, tmp_index) + if left_out is not None: + last_dropped_gitlinks = left_out > 0 + if left_out: + debug_log(f"nested repositories left out of the index copy: {left_out}") + return True + if not _is_kept_for_session(top, tmp_index, ask=True): + return False if not _may_have_gitlinks(tmp_index): return True gitlinks = _gitlink_paths(top, env) @@ -959,6 +1138,11 @@ def _drop_gitlinks(top, env, tmp_index): debug_log(f"git update-index on the index copy failed (rc={rm.returncode}): " f"{rm.stderr[:200].decode('utf-8', errors='replace')}") return False + # git says nothing about an entry it does not find, as when the index + # is not in order (a commit's trees can be in any). + if _may_have_gitlinks(tmp_index) and _gitlink_paths(top, env) != set(): + debug_log("gitlink entries are still in the index copy") + return False # Keep the mtime the copy came with; see _copied_index. os.utime(tmp_index, ns=(mtime_ns, mtime_ns)) last_dropped_gitlinks = True @@ -971,30 +1155,6 @@ def _drop_gitlinks(top, env, tmp_index): return False -def _looks_like_git_dir(path): - """git's own test for a repository directory, near enough: objects and refs - it can enter, and a HEAD that is a link into refs/ or a file holding a ref - or a SHA. Anything else named HEAD stays unopened: a named pipe would block.""" - # With a commondir file, objects and refs are in the folder that it names. - if not (os.path.lexists(os.path.join(path, "commondir")) - or ((os.access(os.path.join(path, "objects"), os.X_OK) - or os.environ.get("GIT_OBJECT_DIRECTORY")) - and os.access(os.path.join(path, "refs"), os.X_OK))): - return False - head_path = os.path.join(path, "HEAD") - try: - mode = os.lstat(head_path).st_mode - if stat.S_ISLNK(mode): - return os.readlink(head_path).startswith("refs/") # git does not follow it - if not stat.S_ISREG(mode): - return False - with open(head_path, "rb") as f: - head = f.read(64) - except OSError: - return False - return bool(re.match(rb"ref:\s*refs/|[0-9a-fA-F]{40}", head)) - - def _found_through_bare_folder(cwd): """True when git, searching up from `cwd`, would take a folder with no `.git` entry for the repository. Git versions before 2.38 have no @@ -1002,17 +1162,408 @@ def _found_through_bare_folder(cwd): A repository the caller named through the environment is not searched for.""" if os.environ.get("GIT_DIR"): return False - d = os.path.realpath(cwd or os.curdir) - while True: + folders = [os.path.realpath(cwd or os.curdir)] + while os.path.dirname(folders[-1]) != folders[-1]: + folders.append(os.path.dirname(folders[-1])) + # Such a folder holds a HEAD. Without one below the first `.git` file, + # which git goes no further than, git need not be asked. + ask = checkout = False + for d in folders: + if not ask and os.path.isfile(os.path.join(d, ".git")): + return False + checkout = checkout or os.path.lexists(os.path.join(d, ".git")) + try: + mode = os.lstat(os.path.join(d, "HEAD")).st_mode + except (OSError, ValueError): + continue + if not (stat.S_ISREG(mode) or stat.S_ISLNK(mode) or stat.S_ISDIR(mode)): + if checkout: + continue + return True # a named pipe: git would open it and wait + ask = ask or not stat.S_ISDIR(mode) + if not ask: + return False + found, when = _bare_folders.get(folders[0], (False, None)) + if when is None or time.monotonic() - when > _OTHER_REPO_FINE_S: + found = _git_takes_a_folder_without_git_entry(folders) + _bare_folders[folders[0]] = (found, time.monotonic()) + return found + + +_bare_folders = {} # folder -> (the answer above, when) + + +def _git_takes_a_folder_without_git_entry(folders): + try: + # Newer versions of git refuse such a folder, unless told to name it. + named = _git_paths(folders[0], "-c", "safe.bareRepository=all", "rev-parse", "--absolute-git-dir") + except (subprocess.TimeoutExpired, OSError, ValueError): + return True + if not named: + return False # git finds no repository + # git looks for a `.git` in a folder first, then at the folder itself. A + # `.git` file names the repository or stops git. + for d in folders: entry = os.path.join(d, ".git") - if os.path.isfile(entry) or _looks_like_git_dir(entry): - return False # git passes over an empty `.git` folder or a broken link - if _looks_like_git_dir(d): + if os.path.isfile(entry) or (os.path.lexists(entry) and _same_dir(entry, named[0])): + return False + if _same_dir(d, named[0]): return os.path.basename(d) != ".git" + return True + + +def _folder_with_git_entry(path): + d = os.path.realpath(path) + while not os.path.lexists(os.path.join(d, ".git")): parent = os.path.dirname(d) if parent == d: - return False + return None d = parent + return d + + +def _git_paths(path, *args): + """The paths a `git rev-parse` prints, one per option, or None when it + fails. ValueError when a newline in one leaves them unreadable.""" + r = subprocess.run([*GIT_CMD, *args], cwd=path, capture_output=True, timeout=5) + out = r.stdout[:-1] if r.stdout.endswith(b"\n") else r.stdout + lines = out.split(b"\n") + if r.returncode != 0: + return None + if len(lines) != len(args) - args.index("rev-parse") - 1: + raise ValueError("a newline in a path") + return [os.path.realpath(os.path.join(path, os.fsdecode(line))) for line in lines] + + +def _is_session_repo(top, project): + """True when the checkout `top` belongs to the repository the session was + started in: its work tree, a linked worktree or a submodule that the + repository keeps and that is where the repository says it is. A folder + whose `.git` only points at that repository is not.""" + if not os.path.isdir(project): + return False + which = ("rev-parse", "--absolute-git-dir", "--git-common-dir") + # Without a `.git` around it, the folder can be a bare repository that + # holds the session's worktrees. + if _folder_with_git_entry(project): + if _found_through_bare_folder(project): + return False + bare = () + else: + bare = ("-c", "safe.bareRepository=all") + here, ours = _git_paths(top, *which), _git_paths(project, *bare, *which) + named = _git_paths(top, "rev-parse", "--show-toplevel") + if not here or not ours or not named: + return False + if os.path.islink(os.path.join(top, ".git")) or not _same_dir(named[0], top): + return False + # The same repository, a submodule of it, or the one it is a submodule of. + if not (_same_dir(here[1], ours[1]) or _is_kept_by(here[1], ours[1]) or _is_kept_by(ours[1], here[1])): + return False + if not _same_dir(here[0], here[1]): # a linked worktree + if not _same_dir(os.path.dirname(here[0]), os.path.join(here[1], "worktrees")): + return False + with _open_file(os.path.join(here[0], "gitdir")) as f: + back = f.read(4096) + back = os.path.join(here[0], os.fsdecode(back.rstrip(b"\r\n"))) + return _same_dir(os.path.dirname(back), top) + if _same_dir(os.path.dirname(here[1]), top): # a main checkout + return True + # Its config says where the work tree is, and git has named `top`. + return any(key == b"core.worktree" for key, _ in _own_settings(top)) + + +def _is_kept_by(inner, outer): + """True when the git directory `inner` is where git puts those of the + submodules of `outer`, or of its linked worktrees.""" + parent = os.path.dirname(inner) + while parent != inner: + if os.path.basename(parent) == "modules": + owner = os.path.dirname(parent) + if _same_dir(owner, outer): + return True + trees = os.path.dirname(owner) + if os.path.basename(trees) == "worktrees" and _same_dir(os.path.dirname(trees), outer): + return True + inner, parent = parent, os.path.dirname(parent) + return False + + +def _is_below(path, folders): + parent = os.path.dirname(path) + while parent != path: + if any(_same_dir(parent, f) for f in folders): + return True + path, parent = parent, os.path.dirname(parent) + return False + + +# Settings in a repository's own config that make the git commands here, or a +# filter the user has set up for all repositories, start a command. GIT_CMD +# takes care of core.fsmonitor, core.hooksPath and format.pretty, and the diffs +# of textconv and external diff. Hooks in the config are git 2.54 and later. core.virtualFilesystem is a fork's. +_COMMAND_SETTING = re.compile( + rb"(?:filter\..*\.(?:clean|process)|hook\..*\.(?:command|event)|lfs\.extension\..*" + rb"|core\.(?:virtualfilesystem|usegvfshelper))\Z", + re.DOTALL | re.IGNORECASE) +# What `git lfs install --local` writes, now and in older versions. +_LFS_VALUE = re.compile(rb"git-lfs (?:clean (?:-- )?%f|filter-process(?: --skip)?)\Z") +# git-lfs reads `git config --list` by line, so a value can hold a setting. +_LFS_IN_VALUE = re.compile(rb"[\r\n][^\S\r\n]*lfs\.", re.IGNORECASE) +# A partial clone: git downloads an object it left out when a command needs it ... +_PARTIAL_CLONE = re.compile( + rb"(?:extensions\.partialclone|remote\..*\.(?:promisor|partialclonefilter))\Z", re.DOTALL) +# ... by the remote's URL, unless one of these changes how. +_TRANSPORT_SETTING = re.compile( + rb"(?:core\.(?:sshcommand|gitproxy|askpass|alternaterefscommand|gvfs|unsetenvvars)|gvfs\..*|gc\.recentobjectshook" + rb"|remote\..*\.(?:uploadpack|vcs|proxy|proxyauthmethod|serveroption)|fetch\.bundle.*|bundle\..*" + rb"|alias\.(?:remote|credential)-.*|(?:http|url|protocol|transfer|ssh)\..*" + rb"|credential\.(?!(?:.*\.)?(?:usehttppath|validate)\Z).*)\Z", re.DOTALL | re.IGNORECASE) +# https, ssh, git, or host:path; no helper::, other scheme or Windows drive. +_PLAIN_URL = re.compile( + rb"(?:(?:https?|ssh|git)://|(?:[\w.+-]+@)?\w[\w.-]+:(?!:|//))[^\0-\x20]*\Z") + + +def no_download_env(): + """Environment for a git command that must not download an object a + partial clone left out. Git before 2.45 does not know the first variable; + the second then allows only a protocol that does not exist.""" + return {**os.environ, "GIT_NO_LAZY_FETCH": "1", "GIT_ALLOW_PROTOCOL": "x" + os.urandom(16).hex()} + + +# A config that lists more than this is not looked through. +_MAX_CONFIG_BYTES = 4 << 20 + + +def _list_config(cmd, **kwargs): + """subprocess.run(cmd, capture_output=True, timeout=5) for a `git config` + that lists settings. ValueError when it lists more than is held here.""" + try: + err = tempfile.TemporaryFile() + except OSError: # no folder to write in + err = open(os.devnull, "w+b") + with err: + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=err, **kwargs) + held = [] + # On a thread, as in _run_diff_bounded: what git started can hold the + # pipe open after git itself is gone. + reader = threading.Thread( + target=lambda: held.append(p.stdout.read(_MAX_CONFIG_BYTES + 1)), daemon=True) + try: + reader.start() + reader.join(5) + if not held: + raise subprocess.TimeoutExpired(cmd, 5) + if len(held[0]) > _MAX_CONFIG_BYTES: + raise ValueError("more settings than are read") + p.wait(5) + except RuntimeError as e: # no thread to be had + raise OSError(str(e)) from e + finally: + if p.poll() is None: + p.kill() + p.wait() + if not reader.is_alive(): + p.stdout.close() + err.seek(0) + return subprocess.CompletedProcess(cmd, p.returncode, held[0], err.read(4096)) + + +def _own_settings(path): + """[(key, value)] from the repository's own config, with the files it + includes and a worktree's own config, without the user's or the machine's.""" + # GIT_CONFIG is read by `git config` and by no other git command. + env = {k: v for k, v in os.environ.items() if k.upper() != "GIT_CONFIG"} + env["LC_ALL"] = "C" + + def run(*args): + return _list_config([*GIT_CMD, "config", "-z", *args], cwd=path, env=env) + + def pairs(items): + return [(k, v) for k, _, v in (i.partition(b"\n") for i in items if i)] + + r = run("--show-scope", "--list") + if r.returncode == 0: + fields = r.stdout.split(b"\0") + return pairs(item for scope, item in zip(fields[::2], fields[1::2]) + if scope in (b"local", b"worktree")) + # git before 2.26 has no --show-scope. + out = [] + for where in ("--local", "--worktree"): + if where == "--worktree" and not any(k == b"extensions.worktreeconfig" for k, _ in out): + continue + r = run(where, "--includes", "--list") + if r.returncode != 0 and b"No such file" not in r.stderr: + raise ValueError(f"git config exited {r.returncode}") + out += pairs(r.stdout.split(b"\0")) + return out + + +def _names_commands(settings): + for key, value in settings: + if _COMMAND_SETTING.match(key) and value and not ( + key.startswith(b"filter.lfs.") and _LFS_VALUE.match(value)): + return True + if _LFS_IN_VALUE.search(value): + return True + promisors = {value if key.startswith(b"extensions.") else key[len(b"remote."):].rpartition(b".")[0] + for key, value in settings if _PARTIAL_CLONE.match(key)} + if not promisors: + return False + # git passes the name on to `git fetch`, which would read an option there. + if not all(re.match(rb"\w[\w./-]*\Z", name) for name in promisors): + return True + if any(_TRANSPORT_SETTING.match(key) for key, _ in settings): + return True + # A remote without a URL here can be a path or a URL itself, or come from + # a file outside the config. + urls = {} + for key, value in settings: + if key.startswith(b"remote.") and key.endswith(b".url"): + urls.setdefault(key[len(b"remote."):-len(b".url")], []).append(value) + return not all(urls.get(name) and all(_PLAIN_URL.match(u) for u in urls[name]) + for name in promisors) + + +def _reads_as_text(path): + """True when a path that git printed is the same after decoding it.""" + try: + path.encode("utf-8") + except UnicodeEncodeError: + return False + return path == path.strip() and "\n" not in path and "\r" not in path + + +_other_repos = {} # folder with the `.git` -> (True to stay out of it, when that was found) +# A repository that was fine is looked at again after this: a hook can wait +# minutes for a review while the session goes on. +_OTHER_REPO_FINE_S = 5 +_other_repos_lock = threading.Lock() + + +def _project_folder(): + """The folder the session was started in, or None.""" + project = (os.environ.get("CLAUDE_PROJECT_DIR") or "").strip() + return project if os.path.isabs(project) else None + + +def _other_repo_with_commands(cwd): + """True when `cwd` is in a repository other than the one the session was + started in, and that repository's own config names commands for git to + run. The session's repository is the user's choice; one that came with + the files is not. It is left out of the review like one nested in the + session's work tree (see IGNORE_SUBMODULES).""" + project = _project_folder() + git_dir = os.environ.get("GIT_DIR") or "" + # An absolute GIT_DIR is one repository for every folder, the user's choice. + if not cwd or not project or os.path.isabs(git_dir): + return False + if git_dir and os.path.normpath(git_dir) != ".git": + return not _same_dir(cwd, project) + try: + top = _folder_with_git_entry(cwd) + except (OSError, ValueError): + return True + if top is None: + return False + with _other_repos_lock: + out, found = _other_repos.get(top, (False, None)) + if not out and (found is None or time.monotonic() - found > _OTHER_REPO_FINE_S): + try: + out = _is_other_repo_with_commands(top, project) + why = "settings of its own that start commands or lead elsewhere" + except (subprocess.TimeoutExpired, OSError, ValueError) as e: + out, why = True, f"{type(e).__name__}: {e}" + _other_repos[top] = (out, time.monotonic()) + if out: + debug_log(f"Staying out of {top!r}: {why}") + return out + + +def _is_other_repo_with_commands(top, project): + if _is_session_checkout(top, project): + return False + try: + settings = _own_settings(top) + except ValueError: + if not _git_paths(top, "rev-parse", "--absolute-git-dir"): + return False # as below + raise + out = _names_commands(settings) or not _reads_as_text(top) + named = _git_paths(top, "rev-parse", "--show-toplevel") + if not named: + if not _git_paths(top, "rev-parse", "--absolute-git-dir"): + return False # git finds no repository and runs nothing + out = True + elif not _same_dir(named[0], top): + # A `.git` here that is no repository does not stop git: the folder + # belongs to the repository around it. A core.worktree that leads + # somewhere else (a submodule's leads back) is another matter. + if any(key == b"core.worktree" for key, _ in settings) or not _is_below(top, [named[0]]): + out = True + else: + return not _reads_as_text(top) or _is_other_repo_with_commands(named[0], project) + return out and not _is_session_repo(top, project) + + +def repos_left_alone(): + return sum(out for out, _ in list(_other_repos.values())) + + +# What session_repo_commands() found, added up. 256: the settings could not be read. +_FILTER_BITS = ((b"nbstripout", 1), (b"git-crypt", 2), (b"annex", 4), (b"lfs", 8)) + + +def session_repo_commands(): + """Which kinds of command the own config of the session's repository + names, as bits. Only counted: the hooks use that repository all the same.""" + project = _project_folder() + if not project or os.environ.get("GIT_DIR"): + return 0 + try: + top = _folder_with_git_entry(project) + if top is None or _found_through_bare_folder(project): + return 0 + settings = _own_settings(top) + except (subprocess.TimeoutExpired, OSError, ValueError): + return 256 + bits = 0 + others = [] + for key, value in settings: + if _PARTIAL_CLONE.match(key) or not _names_commands([(key, value)]): + others.append((key, value)) + elif key.startswith(b"filter."): + name = key[len(b"filter."):].rpartition(b".")[0].lower() + bits |= next((bit for known, bit in _FILTER_BITS if name.startswith(known)), 16) + elif key.startswith(b"hook."): + bits |= 32 + elif key.startswith(b"core."): + bits |= 64 + else: + bits |= 8 # lfs.extension, or a value git-lfs reads as one + # What is left: a partial clone that says how to download. + return bits | (128 if _names_commands(others) else 0) + + +def session_repo_instead_of(cwd): + """The session's own repository when `cwd` is in one the hooks stay out + of, so a turn spent there does not lose the edits made in it.""" + project = _project_folder() + return _git_toplevel(project) if project and in_left_out_repo(cwd) else None + + +def in_left_out_repo(cwd): + """True in a repository the hooks stay out of, other than the folder the + session was started in (a bare repository that holds its worktrees).""" + if not cwd or not _stay_out(cwd): + return False + project = _project_folder() + return not (project and _same_dir(cwd, project)) + + +def _stay_out(cwd): + """True when the hooks should not run git on the files in `cwd`.""" + return _found_through_bare_folder(cwd) or _other_repo_with_commands(cwd) def _git_toplevel(cwd): @@ -1027,8 +1578,8 @@ def _git_toplevel(cwd): ) if r.returncode != 0: return None - path = r.stdout.decode("utf-8", errors="replace").strip() - return path if path and not _found_through_bare_folder(cwd) else None + path = r.stdout.decode("utf-8", errors="replace").rstrip("\r\n") + return path if path and not _stay_out(cwd) and not _stay_out(path) else None except (subprocess.TimeoutExpired, FileNotFoundError, OSError): return None @@ -1046,9 +1597,9 @@ def _git_toplevel_and_head(cwd): if r.returncode != 0: return None, None # See #2099: the first line is a PATH, so decode the bytes manually. - top, _, head = r.stdout.decode("utf-8", errors="replace").strip().rpartition("\n") - top = top.strip() - if not top or _found_through_bare_folder(cwd): + top, _, head = r.stdout.decode("utf-8", errors="replace").rstrip("\r\n").rpartition("\n") + top = top.rstrip("\r") + if not top or _stay_out(cwd) or _stay_out(top): return None, None return top, (head.strip() or None) @@ -1081,7 +1632,7 @@ def _git_dir(repo_root): if a different worktree later pushes it. Returns None on failure so callers can degrade (push-sweep state is best-effort). """ - if _found_through_bare_folder(repo_root): + if _stay_out(repo_root): return None try: # See #2099: stdout is a PATH (shared gitdir), may be non-ASCII. diff --git a/plugins/security-guidance/hooks/reporesolve.py b/plugins/security-guidance/hooks/reporesolve.py index c00a752c..a64b913e 100644 --- a/plugins/security-guidance/hooks/reporesolve.py +++ b/plugins/security-guidance/hooks/reporesolve.py @@ -4,7 +4,7 @@ import shlex import subprocess import time -from gitutil import GIT_CMD, _git_toplevel +from gitutil import GIT_CMD, _git_toplevel, _other_repo_with_commands, _stay_out, no_download_env from session_state import with_locked_state @@ -178,6 +178,14 @@ def toplevel_from_command(command, cwd, subcommands=None, cwd_root=None): return None +def command_in_left_out_repo(command, cwd, subcommands): + """True when a folder the command names is in a repository the hooks stay + out of: what it did there is not another repository's. For a command that + toplevel_from_command finds no repository for.""" + return any(os.path.isdir(d) and _other_repo_with_commands(d) + for d in dirs_from_command(command, cwd, subcommands)) + + def scan_roots(cwd): roots = [] for r in (cwd, os.environ.get("CLAUDE_PROJECT_DIR")): @@ -231,13 +239,18 @@ def repo_containing_commit(sha, roots, budget_s=None): for repo in iter_git_repos(roots, deadline=deadline): try: r = subprocess.run( - [*GIT_CMD, "cat-file", "-e", f"{sha}^{{commit}}"], - cwd=repo, capture_output=True, timeout=3, + # Unlike cat-file, this never downloads what a partial clone + # left out, in any git version. + [*GIT_CMD, "rev-list", "--missing=allow-any", "--no-walk", "--max-count=1", + f"{sha}^{{commit}}"], + cwd=repo, capture_output=True, timeout=3, env=no_download_env(), ) except (subprocess.TimeoutExpired, FileNotFoundError, OSError): continue if r.returncode == 0: - return _git_toplevel(repo) or repo + top = _git_toplevel(repo) + if top or not _stay_out(repo): + return top or repo return None @@ -311,6 +324,10 @@ def resolve_repo_root(cwd, command=None, subcommands=None, sha=None, top = toplevel_from_command(command, cwd, subcommands, cwd_root) if top and top != cwd_root: return top, RES_COMMAND + if not top and command_in_left_out_repo(command, cwd, subcommands): + # What it printed can still name a commit of another one. + top = repo_containing_commit(sha, scan_roots(cwd_root or cwd)) if sha else None + return (top, RES_SHA_SCAN) if top else (None, RES_NONE) if cwd_root: return cwd_root, RES_CWD if touched_paths: diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index d31307de..ea41f547 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -138,7 +138,7 @@ from diffstate import ( # noqa: E402,F401 from reporesolve import ( # noqa: E402,F401 RES_NONE, RES_CWD, RES_COMMAND, RES_SHA_SCAN, RES_TOUCHED_PATHS, RES_HINT, COMMIT_SUBCOMMANDS, PUSH_SUBCOMMANDS, - toplevel_from_command, repo_containing_commit, scan_roots, + toplevel_from_command, command_in_left_out_repo, repo_containing_commit, scan_roots, resolve_repo_root, save_repo_hint, load_repo_hint, ) import diffstate # noqa: E402 module ref for last_baseline_fail @@ -261,7 +261,7 @@ _TAIL_METRICS: Dict[str, Any] = {} # Of those, the ones that go after the others whenever they were set: a row # over CC's key cap loses them before any older review key. Only the launcher # keys below come later; those are also on every smaller row of the session. -_LAST_METRIC_KEYS = ("diff_seen", "cr_pack", "renamed_in") +_LAST_METRIC_KEYS = ("diff_seen", "cr_pack", "renamed_in", "mode", "repos_left_alone") # CC keeps this many keys of a row. _METRIC_KEY_CAP = 20 @@ -469,6 +469,9 @@ def emit_metrics( # managed-mcp.json) and the review was started again without it. tail["strict_mcp_off"] = 1 tail.update(_TAIL_METRICS) + left_alone = gitutil.repos_left_alone() + if left_alone: + tail["repos_left_alone"] = left_alone for k in _LAST_METRIC_KEYS: if k in tail: tail[k] = tail.pop(k) @@ -931,7 +934,7 @@ def handle_user_prompt_submit(input_data): # come back empty. no_repo = False if not sha and not _git_toplevel(cwd): - hint = load_repo_hint(session_id) + hint = gitutil.session_repo_instead_of(cwd) or load_repo_hint(session_id) if hint: debug_log(f"UPS: cwd is not a git repo; using repo hint {hint!r}") cwd = hint @@ -1258,6 +1261,7 @@ def _bash_hook_sentinel(input_data, route="once"): _bash_hook_git_dir[call] = _git_dir( _git_toplevel(cwd) or toplevel_from_command(command, cwd, COMMIT_SUBCOMMANDS | PUSH_SUBCOMMANDS) + or gitutil.session_repo_instead_of(cwd) or cwd ) gd = _bash_hook_git_dir[call] @@ -1802,8 +1806,9 @@ def _left_out_files_notice(session_id, repo_root, untracked, diff_output): Each set of files gets one notice a session: the wake ends in another Stop that fails the same way, and a notice there would wake Claude for ever. So - no notice unless the mark is saved. Paths are JSON quoted in ASCII: a file - name can hold a line break. + no notice unless the mark is saved. A repository whose index cannot be + copied without its nested repositories fails at every Stop and gets one. + Paths are JSON quoted in ASCII: a file name can hold a line break. """ in_diff = set(extract_file_paths_from_diff(diff_output or "")) @@ -1817,8 +1822,9 @@ def _left_out_files_notice(session_id, repo_root, untracked, diff_output): if p not in in_diff and _is_reviewable_source(p) and has_content(p)) if not names: return None + which = [repo_root] if gitutil._add_failed["code"] // 100000 == 5 else names key = "new_files_left_out:" + hashlib.sha256( - "\0".join(names).encode("utf-8", "replace")).hexdigest()[:16] + "\0".join(which).encode("utf-8", "replace")).hexdigest()[:16] if not atomic_check_and_mark_warning(session_id, key): return None if key not in load_state(session_id).get("shown_warnings", ()): @@ -1988,6 +1994,11 @@ def handle_commit_review_posttooluse(input_data): _cmd_root = toplevel_from_command(command, cwd, COMMIT_SUBCOMMANDS, cwd_root) if _cmd_root and _cmd_root != cwd_root: repo_root, repo_res = _cmd_root, RES_COMMAND + # A commit made where the hooks stay out is not one of this repository, or + # of the one an earlier turn worked in. + _hint = not gitutil.in_left_out_repo(cwd) + if cwd and not _cmd_root and command_in_left_out_repo(command, cwd, COMMIT_SUBCOMMANDS): + repo_root, repo_res, _hint = None, RES_NONE, False if not cwd_root: _base["cwd_is_repo"] = False if repo_res != RES_CWD: @@ -2011,7 +2022,7 @@ def handle_commit_review_posttooluse(input_data): _skip_21_sub = 0 if not commit_succeeded and not interrupted and cwd: if not repo_root: - repo_root = load_repo_hint(session_id) + repo_root = load_repo_hint(session_id) if _hint else None if repo_root: repo_res = RES_HINT _base["repo_resolution"] = repo_res @@ -2075,11 +2086,12 @@ def handle_commit_review_posttooluse(input_data): sys.exit(0) if not repo_root and named: - repo_root = repo_containing_commit(named, scan_roots(cwd)) + # cwd is in a repository here only when the command named a left-out one. + repo_root = repo_containing_commit(named, scan_roots(cwd_root or cwd)) if repo_root: repo_res = RES_SHA_SCAN if not repo_root: - repo_root = load_repo_hint(session_id) + repo_root = load_repo_hint(session_id) if _hint else None if repo_root: repo_res = RES_HINT if repo_res != RES_CWD: @@ -2581,6 +2593,9 @@ def handle_push_sweep_posttooluse(input_data): sha=_tip_m.group(2) if _tip_m else None, session_id=session_id, cwd_root=cwd_root, ) + if repo_res == RES_HINT and gitutil.in_left_out_repo(cwd): + # Not a push of the repository an earlier turn worked in. + repo_root, repo_res = None, RES_NONE if not cwd_root: _base["cwd_is_repo"] = False if repo_res != RES_CWD: @@ -3149,8 +3164,18 @@ def handle_stop_hook(input_data): debug_log("Stop hook: no cwd") _skip(4) + _own_cmds = gitutil.session_repo_commands() if not is_subagent else 0 + if _own_cmds: + # Bits, see session_repo_commands(). No other row sends this key. + _TAIL_METRICS["mode"] = _own_cmds + repo_cwd, repo_res = resolve_repo_root( cwd, touched_paths=touched_paths, session_id=session_id) + if repo_res in (RES_NONE, RES_HINT): + # The prompt hook took its baseline there. + _session_repo = gitutil.session_repo_instead_of(cwd) + if _session_repo: + repo_cwd, repo_res = _session_repo, RES_HINT res_metrics = ({} if repo_res == RES_CWD else {"cwd_is_repo": False, "repo_resolution": repo_res}) if is_subagent and repo_cwd: diff --git a/plugins/security-guidance/tests/conftest.py b/plugins/security-guidance/tests/conftest.py index 3987e1de..c152084c 100644 --- a/plugins/security-guidance/tests/conftest.py +++ b/plugins/security-guidance/tests/conftest.py @@ -69,6 +69,12 @@ VULN_PY = ( @pytest.fixture(autouse=True) +def _no_project_folder(monkeypatch): + """The suite can itself be run by a session, which says where it started.""" + monkeypatch.delenv("CLAUDE_PROJECT_DIR", raising=False) + + +@pytest.fixture(autouse=True) def _no_net_err_from_an_earlier_test(): """A hook is one process per event; tests that call it in-process share the module's record of the last failed call.""" diff --git a/plugins/security-guidance/tests/test_bare_repo_toplevel.py b/plugins/security-guidance/tests/test_bare_repo_toplevel.py index 158f1054..c339039e 100644 --- a/plugins/security-guidance/tests/test_bare_repo_toplevel.py +++ b/plugins/security-guidance/tests/test_bare_repo_toplevel.py @@ -1,6 +1,7 @@ """The plugin only treats ordinary checkouts as repositories to run git in.""" import os import subprocess +import time import pytest @@ -146,11 +147,14 @@ def test_head_that_is_a_named_pipe_is_not_opened(tmp_path): (d / "objects").mkdir(parents=True) (d / "refs").mkdir() os.mkfifo(d / "HEAD") - assert not gitutil._looks_like_git_dir(str(d)) + started = time.monotonic() + assert gitutil._found_through_bare_folder(str(d)) + assert time.monotonic() - started < 2 def test_commondir_layout(carried): - (carried / "commondir").write_text("../store\n") + (carried / "commondir").write_text("..\n") + (carried / "config").rename(carried.parent / "config") (carried / "objects").rename(carried.parent / "objects") (carried / "refs").rename(carried.parent / "refs") assert gitutil._found_through_bare_folder(str(carried)) diff --git a/plugins/security-guidance/tests/test_other_repo_config.py b/plugins/security-guidance/tests/test_other_repo_config.py new file mode 100644 index 00000000..8992e463 --- /dev/null +++ b/plugins/security-guidance/tests/test_other_repo_config.py @@ -0,0 +1,1508 @@ +"""The hooks run no git in a repository other than the session's own whose config +names commands for git to run. The session's repository, its subfolders and its +linked worktrees are not affected, and neither are other repositories without +such settings.""" +import hashlib +import json +import os +import re +import struct +import subprocess +import sys +import time +import zlib + +import pytest + +import conftest +import diffstate +import gitutil +import reporesolve + +HOOKS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "hooks") +GIT_VERSION = tuple(int(x) for x in re.findall( + r"\d+", subprocess.run(["git", "--version"], capture_output=True, text=True).stdout)[:2]) +config_hooks = pytest.mark.skipif(GIT_VERSION < (2, 54), reason="git without hooks in the config") + + +@pytest.fixture(autouse=True) +def clean_env(tmp_path, monkeypatch): + """No settings but the test's own, for git here and in the hooks.""" + for k in list(os.environ): + if k.startswith(("GIT_", "CLAUDE_", "SECURITY_")): + monkeypatch.delenv(k) + home = tmp_path / "home" + home.mkdir() + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") + for who in ("AUTHOR", "COMMITTER"): + monkeypatch.setenv(f"GIT_{who}_NAME", "t") + monkeypatch.setenv(f"GIT_{who}_EMAIL", "t@example.com") + monkeypatch.setattr(gitutil, "_other_repos", {}) + monkeypatch.setattr(gitutil, "_bare_folders", {}) + return home + + +@pytest.fixture +def marker(tmp_path): + return tmp_path / "ran" + + +@pytest.fixture +def project(tmp_path, monkeypatch): + p = new_repo(tmp_path / "proj") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(p)) + return p + + +def git(cwd, *args): + return subprocess.run(["git", *args], cwd=cwd, check=True, + capture_output=True, text=True).stdout.strip() + + +def new_repo(path, attributes=None): + path.mkdir(parents=True, exist_ok=True) + git(path, "init", "-q") + (path / "lib.py").write_text("print('one')\n") + if attributes: + (path / ".gitattributes").write_text(attributes) + git(path, "add", "-A") + git(path, "commit", "-q", "-m", f"init {path.name}") + return path + + +def edit(path): + (path / "lib.py").write_text("print('two')\n") + index = path / ".git" / "index" + if index.exists(): + old = os.stat(index).st_mtime - 3600 + os.utime(index, (old, old)) + + +def note(marker): + return f"echo x >> '{marker.as_posix()}'" + + +def add_config(path, text): + with open(path / ".git" / "config", "ab") as f: + f.write(text if isinstance(text, bytes) else text.encode()) + + +def nested(path, marker, kind): + new_repo(path, "* filter=probe\n" if kind in ("clean", "process") else None) + if kind == "clean": + git(path, "config", "filter.probe.clean", f"{note(marker)}; cat") + elif kind == "process": + git(path, "config", "filter.probe.process", note(marker)) + elif kind == "download": + blob = git(path, "rev-parse", "HEAD:lib.py") + obj = path / ".git" / "objects" / blob[:2] / blob[2:] + os.chmod(obj, 0o600) + obj.unlink() + git(path, "config", "core.repositoryformatversion", "1") + git(path, "config", "extensions.partialClone", "origin") + git(path, "config", "remote.origin.url", path.as_posix()) + git(path, "config", "remote.origin.promisor", "true") + git(path, "config", "remote.origin.uploadpack", f"{note(marker)}; :") + elif kind == "hook": + git(path, "config", "hook.probe.event", "post-index-change") + git(path, "config", "hook.probe.command", note(marker)) + edit(path) + marker.unlink(missing_ok=True) + return path + + +def prompt_hook(cwd, tmp_path): + r = subprocess.run( + [sys.executable, os.path.join(HOOKS, "security_reminder_hook.py")], cwd=cwd, + env={**os.environ, "SECURITY_WARNINGS_STATE_DIR": str(tmp_path / "state")}, + input=json.dumps({"hook_event_name": "UserPromptSubmit", "session_id": "s", + "cwd": str(cwd), "prompt": "x"}), + capture_output=True, text=True, timeout=120) + assert r.returncode == 0, r.stderr + files = list((tmp_path / "state").glob("security_warnings_state_*.json")) + return (json.loads(files[0].read_text()) if files else {}).get("baseline_sha") + + +KINDS = ["clean", "process", pytest.param("hook", marks=config_hooks)] + + +@pytest.mark.parametrize("kind", KINDS) +@pytest.mark.parametrize("project_is_repo", [True, False]) +def test_prompt_in_another_repo(tmp_path, monkeypatch, marker, kind, project_is_repo): + project = tmp_path / "work" + project.mkdir() + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + if project_is_repo: + new_repo(project) + other = nested(project / "vendor" / "lib", marker, kind) + sha = prompt_hook(other, tmp_path) + assert not marker.exists() + # The turn's baseline is the session repository's, when there is one. + assert sha == (git(project, "rev-parse", "HEAD") if project_is_repo else None) + + +@pytest.mark.parametrize("kind", KINDS) +def test_every_way_into_another_repo(project, marker, kind): + other = str(nested(project / "lib", marker, kind)) + sha = git(other, "rev-parse", "HEAD") + assert gitutil._git_toplevel(other) is None + assert gitutil._git_toplevel_and_head(other) == (None, None) + assert gitutil._git_dir(other) is None + assert diffstate.capture_git_baseline(other) is None + assert diffstate._list_untracked(other) == {} + assert diffstate.compute_v2_review_set(other, None, None)[0] == [] + assert reporesolve.repos_from_paths([os.path.join(other, "lib.py")]) == [] + assert reporesolve.repo_containing_commit(sha, [str(project)]) is None + assert not marker.exists() + assert gitutil.repos_left_alone() == 1 + # ... and the session's own repository is reviewed all the same. + assert os.path.samefile(gitutil._git_toplevel(str(project)), project) + assert diffstate.capture_git_baseline(str(project)) + + +@pytest.mark.parametrize("section, attribute", [ + (b'[filter "it\'s!"]', b"it's!"), (b'[filter ""]', b""), (b'[filter "a=b"]', b"a=b"), + (b'[filter "\xff"]', b"\xff"), ('[filter "ünï"]'.encode(), "ünï".encode()), + (b'[FILTER "p"]', b"p"), +]) +def test_any_driver_name(project, marker, section, attribute, monkeypatch): + other = new_repo(project / "lib") + (other / ".gitattributes").write_bytes(b"* filter=" + attribute + b"\n") + add_config(other, section + f'\n\tclean = "{note(marker)}; cat"\n'.encode()) + edit(other) + for locale in ("C", "C.UTF-8"): + monkeypatch.setenv("LC_ALL", locale) + monkeypatch.setattr(gitutil, "_other_repos", {}) + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +@pytest.mark.parametrize("how", ["include", "includeIf-gitdir", "includeIf-branch", "home", "worktree"]) +def test_settings_in_another_file(project, marker, how, clean_env): + other = new_repo(project / "lib", "* filter=probe\n") + driver = f'[filter "probe"]\n\tclean = "{note(marker)}; cat"\n' + extra = other / ".git" / "extra" + if how == "worktree": + git(other, "config", "core.repositoryformatversion", "1") + git(other, "config", "extensions.worktreeConfig", "true") + extra = other / ".git" / "config.worktree" + elif how == "home": + extra = clean_env / "extra" + add_config(other, "[include]\n\tpath = ~/extra\n") + else: + branch = git(other, "symbolic-ref", "--short", "HEAD") + section = {"include": "include", "includeIf-gitdir": 'includeIf "gitdir:lib/"', + "includeIf-branch": f'includeIf "onbranch:{branch}"'}[how] + add_config(other, f"[{section}]\n\tpath = extra\n") + extra.write_text(driver) + edit(other) + marker.unlink(missing_ok=True) + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_a_driver_the_user_set_up_is_kept(project, marker, clean_env): + (clean_env / ".gitconfig").write_text(f'[filter "probe"]\n\tclean = "{note(marker)}; cat"\n') + other = new_repo(project / "lib", "* filter=probe\n") + edit(other) + marker.unlink(missing_ok=True) + assert diffstate.capture_git_baseline(str(other)) + assert marker.exists() + + +@pytest.mark.parametrize("settings, kept", [ + ('[filter "lfs"]\n\tclean = git-lfs clean -- %f\n\tsmudge = git-lfs smudge -- %f\n' + "\tprocess = git-lfs filter-process\n\trequired = true\n", True), + ('[filter "lfs"]\n\tclean = "git-lfs clean -- %f; true"\n', False), + ('[filter "lfs"]\n\tclean = "git-lfs clean -- %f\\n true"\n', False), + ('[filter "lfs"]\n\tclean = git-lfs clean %f\n\tsmudge = git-lfs smudge --skip %f\n', True), + ('[filter "lfs"]\n\tsmudge =\n\tprocess =\n\trequired = false\n', True), + ('[filter "x"]\n\tsmudge = true\n', True), + ('[filter "x"]\n\tclean = true\n[filter "x"]\n\tclean =\n', False), + ('[lfs "extension.x"]\n\tclean = true\n', False), + ('[lfs "extension.x.clean=true "]\n\ty = z\n', False), + ('[foo]\n\tbar = "x\\nlfs.extension.evil.clean=true"\n', False), + ('[alias]\n\tx = "!a\\nb"\n', True), + ('[remote "o"]\n\tpromisor = true\n\turl = https://example.com/x.git\n\tfetch = +a:b\n', True), + ('[remote "o"]\n\tpromisor = true\n\turl = git@example.com:x/y.git\n', True), + ('[remote "o"]\n\tpartialclonefilter = blob:none\n\turl = https://e.com/x\n\tuploadpack = u\n', False), + ('[extensions]\n\tpartialClone = ../src\n', False), + ('[extensions]\n\tpartialClone = o\n[remote "o"]\n\turl = https://e.com/x\n', True), + ('[extensions]\n\tpartialClone = o\n[remote "p"]\n\turl = https://e.com/x\n', False), + ('[remote "../src"]\n\tpromisor = true\n', False), + ('[remote "--upload-pack=x;"]\n\tpromisor = true\n\turl = https://e.com/x\n', False), + ('[extensions]\n\tpartialClone = --upload-pack=x\n[remote "--upload-pack=x"]\n\turl = https://e.com/x\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = work:o/r.git\n[user]\n\tname = x\n', True), + ('[remote "o"]\n\tpromisor = true\n\turl = org-1@ssh.dev:o/r\n[credential "https://e.com"]\n' + "\tuseHttpPath = true\n[branch \"main\"]\n\tremote = o\n", True), + ('[remote "o"]\n\tpromisor = true\n\turl = https://e.com\n[credential "https://e.com"]\n\thelper = x\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = https://e.com\n[url "ext::x"]\n\tinsteadOf = https://e.com\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = C:/x\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = ext://run\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = file:///x\n', False), + ('[core]\n\tvirtualFilesystem = true\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = https://e.com/x\n[http]\n\tcookieFile = x\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = https://e.com/x\n[fetch]\n\tbundleURI = x\n', False), + # what `scalar clone`, a sparse index, git-lfs and Android's repo tool write + ('[remote "o"]\n\tpromisor = true\n\turl = https://e.com/x\n\tprojectname = p\n[am]\n\tkeepCR = true\n' + "[commitGraph]\n\tchangedPaths = true\n[index]\n\tsparse = true\n[lfs]\n\trepositoryformatversion = 0\n" + "[alias]\n\tst = status\n[merge]\n\tstat = false\n", True), + ('[core]\n\tuseGvfsHelper = true\n', False), + ('[http]\n\tcookieFile = x\n[fetch]\n\tbundleURI = x\n', True), + ('[lfs "EXTENSION.x"]\n\tclean = true\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = ext::sh -c x\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = none::x\n[alias]\n\tremote-none = !x\n', False), + ('[remote "o"]\n\tpromisor = true\n\turl = ../x\n', False), + ('[remote "o"]\n\turl = ../x\n\tuploadpack = u\n[core]\n\tsshCommand = ssh -i k\n', True), + ('[core]\n\tautocrlf = input\n[filter "x"]\n\trequired = false\n[user]\n\tname = x\n', True), +]) +def test_which_settings_count(project, settings, kept): + other = new_repo(project / "lib") + add_config(other, settings) + assert (gitutil._git_toplevel(str(other)) is not None) is kept + + +@pytest.mark.parametrize("settings, bits", [ + ("", 0), + ('[filter "lfs"]\n\tclean = git-lfs clean -- %f\n\tprocess = git-lfs filter-process\n', 0), + ('[core]\n\tsshCommand = ssh -i k\n\thooksPath = .husky\n[remote "o"]\n\turl = git@example.com:x\n', 0), + ('[extensions]\n\tpartialClone = o\n[remote "o"]\n\tpromisor = true\n\turl = https://example.com/x\n', 0), + ('[filter "nbstripout"]\n\tclean = nbstripout\n\tsmudge = cat\n', 1), + ('[filter "git-crypt-team"]\n\tclean = git-crypt clean\n\trequired = true\n', 2), + ('[filter "annex"]\n\tprocess = git-annex filter-process\n', 4), + ('[filter "lfs"]\n\tclean = mine\n', 8), + ('[lfs "extension.x"]\n\tclean = mine\n', 8), + ('[filter "jupytext"]\n\tclean = a\n[filter "nbstripout"]\n\tclean = b\n', 17), + ('[hook "x"]\n\tcommand = a\n\tevent = post-index-change\n', 32), + ('[core]\n\tvirtualFilesystem = a\n', 64), + ('[remote "o"]\n\tpromisor = true\n\turl = https://example.com/x\n\tuploadpack = u\n', 128), +]) +def test_commands_in_the_session_repo_are_counted(project, settings, bits): + add_config(project, settings) + assert gitutil.session_repo_commands() == bits + assert gitutil._git_toplevel(str(project)) # and nothing else changes + + +def test_only_the_session_repo_is_counted(project, tmp_path, marker, monkeypatch): + nested(project / "lib", marker, "clean") + assert gitutil.session_repo_commands() == 0 + (project / "sub").mkdir() + git(project, "config", "filter.x.clean", note(marker)) + marker.unlink(missing_ok=True) + for start in (project, project / "sub"): + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(start)) + assert gitutil.session_repo_commands() == 16 + assert not marker.exists() + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(tmp_path)) + assert gitutil.session_repo_commands() == 0 + monkeypatch.delenv("CLAUDE_PROJECT_DIR") + assert gitutil.session_repo_commands() == 0 + + +def test_the_count_is_on_the_stop_row(session, marker, hook_env, stub_api): + project, _, _ = session + git(project, "config", "filter.x.clean", "cat") + env = {**hook_env, "CLAUDE_PROJECT_DIR": str(project)} + for payload, sent in ((conftest.stop_payload(project), 16), + ({**conftest.stop_payload(project), "hook_event_name": "SubagentStop"}, None)): + conftest.run_hook(conftest.ups_payload(project), env) + (project / "lib.py").write_text(conftest.VULN_PY) + m = conftest.metrics_of(conftest.run_hook(payload, env)[1]) + assert m.get("mode") == sent, m + + +def test_settings_of_the_session_repo_that_cannot_be_read(project): + add_config(project, "[broken\n") + assert gitutil.session_repo_commands() == 256 + + +def test_partial_clone_with_a_command_to_download_by(project, marker): + other = nested(project / "lib", marker, "download") + assert gitutil._git_toplevel(str(other)) is None + assert not marker.exists() + + +def test_looking_for_a_commit_downloads_nothing(project, marker): + other = nested(project / "lib", marker, "download") + git(other, "config", "remote.origin.url", "none::x") + git(other, "config", "alias.remote-none", f"!{note(marker)}; :") + reporesolve.repo_containing_commit("0" * 39 + "1", [str(project)]) + assert not marker.exists() + + +@pytest.mark.parametrize("where", ["top", "subfolder", "stray-git", "worktree", "from-worktree"]) +def test_session_repo_keeps_its_config(tmp_path, monkeypatch, marker, where): + project = nested(tmp_path / "proj", marker, "clean") + cwd = home = project + if where in ("subfolder", "stray-git"): + cwd = project / "sub" + cwd.mkdir() + if where == "stray-git": + (cwd / ".git").mkdir() + elif where.endswith("worktree"): + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + (wt / "lib.py").write_text("print('three')\n") + cwd, home = (wt, project) if where == "worktree" else (project, wt) + marker.unlink(missing_ok=True) + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(home)) + assert prompt_hook(cwd, tmp_path) + assert marker.exists() + + +@pytest.mark.skipif(os.name == "nt", reason="folder names Windows does not allow") +@pytest.mark.parametrize("name", [b"g ", b" g", b"\xff", b"a\rb"]) +def test_git_file_is_read_as_git_reads_it(project, marker, name): + other = project / "lib" + other.mkdir() + r = subprocess.run([b"git", b"init", b"-q", b"--separate-git-dir", name, b"."], + cwd=other, capture_output=True) + if r.returncode != 0: + pytest.skip("a name this file system does not allow") + (other / ".gitattributes").write_text("* filter=probe\n") + (other / "lib.py").write_text("print('one')\n") + git(other, "add", "-A") + git(other, "commit", "-q", "-m", "init") + git(other, "config", "filter.probe.clean", f"{note(marker)}; cat") + (other / ".git").write_bytes(b"gitdir: " + name + b"\n") + for lookalike in {name.strip(), name.decode("utf-8", "replace").encode(), + name.replace(b"\r", b"\n")} - {name}: + os.symlink(os.fsencode(project / ".git"), os.path.join(os.fsencode(other), lookalike)) + (other / "lib.py").write_text("print('two')\n") + marker.unlink(missing_ok=True) + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_folder_that_borrows_the_session_repo_is_not_its_worktree(project, marker): + git(project, "config", "core.repositoryformatversion", "1") + git(project, "config", "extensions.worktreeConfig", "true") + fake = project / "vendor" / "lib" + own = fake / "store" + own.mkdir(parents=True) + (own / "HEAD").write_text(git(project, "rev-parse", "HEAD") + "\n") + (own / "commondir").write_text((project / ".git").as_posix() + "\n") + (own / "gitdir").write_text((fake / ".git").as_posix() + "\n") + (own / "config.worktree").write_text(f'[filter "probe"]\n\tclean = "{note(marker)}; cat"\n') + (fake / ".git").write_text(f"gitdir: {own.as_posix()}\n") + (fake / ".gitattributes").write_text("* filter=probe\n") + (fake / "lib.py").write_text("print('one')\n") + git(fake, "reset", "-q") + (fake / "lib.py").write_text("print('two')\n") + marker.unlink(missing_ok=True) + assert diffstate.capture_git_baseline(str(fake)) is None + assert not marker.exists() + + +def test_settings_that_cannot_be_read_leave_out_only_that_repo(project, monkeypatch): + other = new_repo(project / "lib") + looks = [] + + def fail(path): + looks.append(path) + raise subprocess.TimeoutExpired("git", 5) + monkeypatch.setattr(gitutil, "_own_settings", fail) + assert gitutil._git_toplevel(str(other)) is None + assert diffstate.capture_git_baseline(str(other)) is None + assert gitutil._git_toplevel(str(project)) is not None + assert len(looks) == gitutil.repos_left_alone() == 1 + + +def test_settings_are_looked_at_again_after_a_while(project, marker, monkeypatch): + other = new_repo(project / "lib", "* filter=probe\n") + assert diffstate.capture_git_baseline(str(other)) + git(other, "config", "filter.probe.clean", f"{note(marker)}; cat") + edit(other) + marker.unlink(missing_ok=True) + monkeypatch.setattr(gitutil, "_OTHER_REPO_FINE_S", -1) + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_one_look_per_repository(project, monkeypatch): + other = new_repo(project / "lib") + paths = [] + for i in range(5): + (other / f"d{i}").mkdir() + paths.append(str(other / f"d{i}" / "x.py")) + looks = [] + real = gitutil._own_settings + monkeypatch.setattr(gitutil, "_own_settings", lambda p: looks.append(p) or real(p)) + assert len(reporesolve.repos_from_paths(paths)) == 1 + assert len(looks) == 1 + + +def test_without_a_project_folder_nothing_changes(tmp_path, marker): + other = nested(tmp_path / "lib", marker, "clean") + assert prompt_hook(other, tmp_path) + assert marker.exists() + + +def test_work_tree_of_another_repo(project, marker): + outer = nested(project / "vendor", marker, "clean") + inner = new_repo(outer / "a") + git(inner, "config", "core.worktree", "../..") + assert gitutil._git_toplevel(str(inner)) is None + assert diffstate.capture_git_baseline(str(inner)) is None + assert diffstate.compute_v2_review_set(str(inner), None, None)[0] == [] + assert not marker.exists() + + +def test_checked_out_submodule_is_kept(project, tmp_path): + lib = new_repo(tmp_path / "upstream") + git(project, "-c", "protocol.file.allow=always", "submodule", "add", "-q", str(lib), "sub") + assert os.path.samefile(gitutil._git_toplevel(str(project / "sub")), project / "sub") + + +def test_relative_git_dir_names_no_one_repository(project, marker, monkeypatch): + other = nested(project / "lib", marker, "clean") + monkeypatch.setenv("GIT_DIR", ".git") + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_git_config_variable_does_not_hide_the_settings(project, marker, monkeypatch, clean_env): + other = nested(project / "lib", marker, "clean") + (clean_env / "other").write_text("[user]\n\tname = x\n") + monkeypatch.setenv("GIT_CONFIG", str(clean_env / "other")) + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_showing_a_commit_verifies_no_signature(project, marker): + tree = git(project, "rev-parse", "HEAD^{tree}") + body = (f"tree {tree}\nauthor t <t@example.com> 1 +0000\ncommitter t <t@example.com> 1 +0000\n" + "gpgsig -----BEGIN PGP SIGNATURE-----\n \n x\n -----END PGP SIGNATURE-----\n\nm\n") + sha = subprocess.run(["git", "hash-object", "-t", "commit", "-w", "--stdin"], cwd=project, + input=body.encode(), capture_output=True, check=True).stdout.decode().strip() + tool = project / "tool.sh" + tool.write_text(f"#!/bin/sh\n{note(marker)}\n") + tool.chmod(0o755) + git(project, "config", "gpg.program", tool.as_posix()) + git(project, "config", "format.pretty", "%G? %H") + r = subprocess.run([*gitutil.GIT_CMD, "show", "-p", sha, "--"], cwd=project, capture_output=True) + assert r.returncode == 0 and sha.encode() in r.stdout + assert not marker.exists() + + +def test_project_folder_that_is_gone(project, monkeypatch, tmp_path): + plain = new_repo(tmp_path / "plain") + filtered = nested(tmp_path / "filtered", tmp_path / "ran", "clean") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project / "gone")) + assert gitutil._git_toplevel(str(plain)) is not None + assert gitutil._git_toplevel(str(filtered)) is None + + +def test_worktree_settings_without_their_file(project): + other = new_repo(project / "lib") + git(other, "config", "core.repositoryformatversion", "1") + git(other, "config", "extensions.worktreeConfig", "true") + assert gitutil._git_toplevel(str(other)) is not None + + +@pytest.mark.parametrize("how", ["GIT_DIR", "GIT_CEILING_DIRECTORIES"]) +def test_a_look_from_a_subfolder_counts_for_the_repository(project, marker, monkeypatch, how): + other = nested(project / "lib", marker, "clean") + (other / "sub").mkdir() + monkeypatch.setenv(how, ".git" if how == "GIT_DIR" else str(other)) + assert diffstate.capture_git_baseline(str(other / "sub")) is None + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_long_value_is_read_quickly(project): + started = time.monotonic() + gitutil._names_commands([(b"x.y", b"\n" * 300_000)]) + assert time.monotonic() - started < 2 + + +def test_session_started_in_a_bare_repo_that_holds_its_worktrees(tmp_path, monkeypatch, marker): + src = new_repo(tmp_path / "src") + hub = tmp_path / "hub.git" + git(tmp_path, "clone", "-q", "--bare", str(src), str(hub)) + git(hub, "worktree", "add", "-q", "-b", "side", str(hub / "side")) + git(hub, "config", "filter.probe.clean", f"{note(marker)}; cat") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(hub)) + for k, v in gitutil.git_config_env(gitutil.SAFE_GIT_CONFIG).items(): + monkeypatch.setenv(k, v) + assert gitutil._git_toplevel(str(hub / "side")) is not None + + +@pytest.fixture +def session(project, marker, hook_env, tmp_path): + """A session in `project`, with an earlier turn's hint at a third + repository, and a left-out repository `lib` in the project folder.""" + other = nested(project / "lib", marker, "clean") + third = new_repo(tmp_path / "third") + hook_env["CLAUDE_PROJECT_DIR"] = str(project) + conftest.run_hook(conftest.edit_payload(tmp_path, third / "a.py"), hook_env) + conftest.run_hook(conftest.ups_payload(tmp_path), hook_env) + (third / "a.py").write_text("x = 1\n") + conftest.run_hook(conftest.stop_payload(tmp_path), hook_env) + return project, other, third + + +def test_turn_in_a_left_out_repo_keeps_the_session_repos_review(session, marker, hook_env, stub_api): + project, other, _ = session + stub_api.bodies.clear() + conftest.run_hook(conftest.ups_payload(other), hook_env) + (project / "lib.py").write_text(conftest.VULN_PY) # as a Bash command would + m = conftest.metrics_of(conftest.run_hook(conftest.stop_payload(other), hook_env)[1]) + assert m.get("skip_reason") is None and m["files_reviewed"] == 1, m + assert m["repos_left_alone"] == 1 + assert any("subprocess.call" in b for b in stub_api.bodies) + assert not marker.exists() + + +@pytest.mark.parametrize("command, output", [ + ("git commit -q -m x", ""), ("git commit -m x", "[main 1234567] x\n 1 file changed\n"), + ("git push -u origin main", "To x\n * [new branch] main -> main\n"), + ("git push", "Everything up-to-date\n"), +]) +def test_commit_or_push_in_a_left_out_repo_is_not_another_ones( + session, marker, hook_env, stub_api, command, output): + _, other, third = session + before = len(stub_api.calls) + m = conftest.metrics_of(conftest.run_hook(conftest.bash_payload(other, command, output), hook_env)[1]) + assert m["skip_reason"] in (21, 26), m + assert "repo_resolution" not in m or m["repo_resolution"] == reporesolve.RES_NONE + assert len(stub_api.calls) == before + assert not marker.exists() + + +NEW_BRANCH = "To x\n * [new branch] main -> main\n" + + +@pytest.mark.parametrize("where", ["session", "beside"]) +@pytest.mark.parametrize("command, output", [ + ("git -C {} push", NEW_BRANCH), ("cd {} && git push", "Everything up-to-date\n"), + ("git -C {} commit -q -m x", ""), ("cd {} && git commit -q -m x", ""), + ("cd {} && git commit -m x", "[main 1234567] x\n 1 file changed\n"), + ("cd {} && git commit -q -m x && git push", NEW_BRANCH), +]) +def test_command_that_names_a_left_out_repo_is_not_another_ones( + session, tmp_path, marker, hook_env, stub_api, where, command, output): + """Neither the session's repository nor the one an earlier turn worked in, + which both have a commit that was not pushed.""" + project, other, third = session + conftest.commit_file(project, "b.py", conftest.VULN_PY) + conftest.commit_file(third, "b.py", conftest.VULN_PY) + cwd = project + if where == "beside": + cwd = tmp_path / "beside" + cwd.mkdir() + before = len(stub_api.calls) + for start in range(2): # one for each entry in hooks.json + m = conftest.metrics_of(conftest.run_hook(conftest.bash_payload( + cwd, command.format(other.as_posix()), output, tool_use_id="toolu_1"), hook_env)[1]) + if where == "beside" or not start or "commit" in command and "push" in command: + assert m["skip_reason"] in (21, 26) and m["repo_resolution"] == reporesolve.RES_NONE, m + else: + assert "skip_reason" not in m and "files_reviewed" not in m, m + assert len(stub_api.calls) == before + assert not (project / ".git" / "sg-reviewed-shas").exists() + assert not marker.exists() + + +@pytest.mark.parametrize("below", [False, True]) +def test_commit_the_output_names_after_a_look_into_a_left_out_repo( + session, marker, hook_env, stub_api, below): + """The session's own, though the command reads as one in the other.""" + project, other, _ = session + _, out = conftest.commit_file(project, "src/b.py", conftest.VULN_PY) + cwd = project / "src" if below else project + m = conftest.metrics_of(conftest.run_hook(conftest.bash_payload( + cwd, f"(cd {other.as_posix()} && make); git commit -m x", out), hook_env)[1]) + assert m.get("skip_reason") is None and m["files_reviewed"] == 1, m + assert m["repo_resolution"] == reporesolve.RES_SHA_SCAN + assert not marker.exists() + + +def test_command_in_a_left_out_repo_and_another(session, tmp_path, marker, hook_env, stub_api): + project, other, _ = session + _, out = conftest.commit_file(project, "b.py", conftest.VULN_PY) + m = conftest.metrics_of(conftest.run_hook(conftest.bash_payload( + tmp_path, f"git -C {other.as_posix()} commit -q -m x; git -C {project.as_posix()} commit -m x", out), + hook_env)[1]) + assert m.get("skip_reason") is None and m["files_reviewed"] == 1, m + assert m["repo_resolution"] == reporesolve.RES_COMMAND + assert not marker.exists() + + +def test_commit_made_elsewhere_from_a_left_out_repo_is_found(session, marker, hook_env, stub_api): + project, other, _ = session + _, out = conftest.commit_file(project, "b.py", conftest.VULN_PY) + m = conftest.metrics_of(conftest.run_hook( + conftest.bash_payload(other, 'cd "$REPO" && git commit -m change', out), + {**hook_env, "CLAUDE_PROJECT_DIR": str(project)})[1]) + assert m.get("skip_reason") is None and m["files_reviewed"] == 1, m + assert m["repo_resolution"] == reporesolve.RES_SHA_SCAN + assert not marker.exists() + + +def test_turn_in_an_embedded_repository_keeps_the_session_repos_review( + project, marker, hook_env, stub_api): + inside = embedded(project / "pkg" / "x", marker) + (inside / "lib.py").write_text("print('two')\n") + marker.unlink(missing_ok=True) + hook_env["CLAUDE_PROJECT_DIR"] = str(project) + conftest.run_hook(conftest.ups_payload(inside), hook_env) + (project / "lib.py").write_text(conftest.VULN_PY) + conftest.run_hook(conftest.stop_payload(inside), hook_env) + assert any("subprocess.call" in b for b in stub_api.bodies) + assert not marker.exists() + + +def test_alias_in_a_partial_clone(project): + repo = new_repo(project / "lib") + for k, v in (("core.repositoryformatversion", "1"), ("extensions.partialClone", "origin"), + ("remote.origin.url", "https://example.com/x.git"), ("remote.origin.promisor", "true")): + git(repo, "config", k, v) + assert gitutil._git_toplevel(str(repo)) + git(repo, "config", "alias.credential-some-helper", "!echo") + gitutil._other_repos.clear() + assert gitutil._git_toplevel(str(repo)) is None + + +def embedded(inside, marker): + """A bare repository laid out in a work tree's own folder, whose filter git + before 2.38 would start from there.""" + inside.mkdir(parents=True) + git(inside, "init", "-q", "--bare", ".") + git(inside, "config", "core.bare", "false") + git(inside, "config", "core.worktree", ".") + (inside / ".gitattributes").write_text("* filter=probe\n") + (inside / "lib.py").write_text("print('one')\n") + git(inside, "add", ".gitattributes", "lib.py") + git(inside, "commit", "-q", "-m", "x") + git(inside, "config", "filter.probe.clean", f"{note(marker)}; cat") + return inside + + +HEAD_LAYOUTS = ["padded-head", "tab-head", pytest.param("newline", marks=pytest.mark.skipif( + os.name == "nt", reason="a folder name Windows does not allow")), pytest.param("head-link", marks=pytest.mark.skipif( + os.name == "nt", reason="symbolic links"))] + + +@pytest.mark.parametrize("layout", ["plain", "commondir", "empty-git", "bogus-commondir", "bogus-gitdir"] + + HEAD_LAYOUTS) +@pytest.mark.parametrize("project_dir", [True, False]) +def test_embedded_repository_git_before_2_38_would_take(project, marker, layout, project_dir, monkeypatch): + if not project_dir: + monkeypatch.delenv("CLAUDE_PROJECT_DIR") + inside = embedded(project / "pkg" / ("x\ny" if layout == "newline" else "x"), marker) + sha, branch = git(inside, "rev-parse", "HEAD"), git(inside, "symbolic-ref", "HEAD") + if layout == "head-link": + git(inside, "pack-refs", "--all") + (inside / "HEAD").unlink() + os.symlink(branch, inside / "HEAD") + elif layout == "padded-head": + (inside / "HEAD").write_text("ref:" + " " * 120 + branch + "\n") + elif layout == "tab-head": + (inside / "HEAD").write_text("ref:\t" + branch + "\n") + elif layout == "commondir": + store = project / "store" + store.mkdir() + for name in ("objects", "refs", "config"): + os.rename(inside / name, store / name) + (inside / "commondir").write_text("../../store\n") + elif layout not in ("plain", "newline"): + dot = inside / ".git" + dot.mkdir() + if layout == "bogus-commondir": + (dot / "HEAD").write_text("ref: refs/heads/main\n") + (dot / "commondir").write_text("nowhere\n") + elif layout == "bogus-gitdir": + (dot / "HEAD").write_text("ref: refs/heads/main\n") + (dot / "refs").mkdir() + (inside / "lib.py").write_text("print('two')\n") + marker.unlink(missing_ok=True) + (inside / "sub").mkdir() + for d in (inside, inside / "sub"): + assert diffstate.capture_git_baseline(str(d)) is None + assert diffstate.compute_v2_review_set(str(d), None, None)[0] == [] + assert gitutil._git_toplevel(str(d)) is None + assert reporesolve.repo_containing_commit(sha, [str(project)]) is None + assert not marker.exists() + + +def test_ordinary_checkouts_beside_a_head_file(project, tmp_path): + (project / "HEAD").write_text("ref: refs/heads/main\n") + (project / "sub").mkdir() + assert gitutil._git_toplevel(str(project / "sub")) + git(project, "worktree", "add", "-q", str(tmp_path / "wt")) + (tmp_path / "wt" / "HEAD").write_text("x") + assert gitutil._git_toplevel(str(tmp_path / "wt")) + hub = tmp_path / "hub.git" + git(tmp_path, "clone", "-q", "--bare", str(project), str(hub)) + git(hub, "worktree", "add", "-q", "-b", "side", str(hub / "side")) + assert gitutil._git_toplevel(str(hub / "side")) + outer = new_repo(tmp_path / "outer") + git(outer, "-c", "protocol.file.allow=always", "submodule", "add", "-q", str(project), "sub") + (outer / "sub" / "HEAD").write_text("x") + assert gitutil._git_toplevel(str(outer / "sub")) + + +def test_git_is_asked_once_about_a_folder_below_a_head_file(project, monkeypatch): + (project / "HEAD").write_text("x") + asked = [] + real = gitutil._git_paths + monkeypatch.setattr(gitutil, "_git_paths", lambda *a: asked.append(a) or real(*a)) + for _ in range(3): + assert not gitutil._found_through_bare_folder(str(project)) + assert len(asked) == 1 + + +@pytest.mark.skipif(os.name == "nt", reason="a folder name cannot end in a space") +def test_folder_without_a_repository_of_its_own_below_another(project, marker): + nested(project / "b", marker, "clean") + spaced = new_repo(project / "b ") + (spaced / "sub" / ".git").mkdir(parents=True) + assert gitutil._git_toplevel(str(spaced / "sub")) is None + assert diffstate.compute_v2_review_set(str(spaced / "sub"), None, None)[0] == [] + assert not marker.exists() + + +def test_folder_that_points_at_the_session_repo_is_not_its_work_tree(project, marker): + (project / "tools").mkdir() + git(project, "config", "filter.tidy.clean", "sh tools/c.sh") + fake = project / "vendor" / "lib" + (fake / "tools").mkdir(parents=True) + (fake / "tools" / "c.sh").write_text(f"{note(marker)}; cat\n") + (fake / ".git").write_text("gitdir: ../../.git\n") + (fake / ".gitattributes").write_text("*.py filter=tidy\n") + (fake / "lib.py").write_text("print('two')\n") + assert diffstate.capture_git_baseline(str(fake)) is None + git(project, "config", "--unset", "filter.tidy.clean") + git(project, "worktree", "add", "-q", str(project / "wt")) + git(project, "config", "filter.tidy.clean", "sh tools/c.sh") + (fake / ".git").write_text("gitdir: ../../.git/worktrees/wt\n") + gitutil._other_repos.clear() + assert diffstate.capture_git_baseline(str(fake)) is None + assert not marker.exists() + assert gitutil._git_toplevel(str(project / "wt")) + + +def test_bare_layout_in_a_session_subfolder_is_not_the_session_repo(project, marker, monkeypatch): + start = project / "pkg" + hub = embedded(start, marker) + git(hub, "worktree", "add", "-q", "-b", "side", str(hub / "x")) + (hub / "x" / "lib.py").write_text("print('two')\n") + marker.unlink(missing_ok=True) + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(start)) + assert diffstate.capture_git_baseline(str(hub / "x")) is None + assert not marker.exists() + + +def test_baseline_content_is_not_read_in_an_embedded_repository(project, marker, monkeypatch): + inside = embedded(project / "pkg" / "x", marker) + sha = git(inside, "rev-parse", "HEAD") + monkeypatch.setattr(diffstate, "load_baseline_sha", lambda session_id: sha) + assert diffstate.get_baseline_file_content("s", str(inside / "lib.py"), str(inside)) is None + assert diffstate.get_baseline_file_content( + "s", str(project / "lib.py"), str(project)) is None # not a commit there + head = git(project, "rev-parse", "HEAD") + monkeypatch.setattr(diffstate, "load_baseline_sha", lambda session_id: head) + assert diffstate.get_baseline_file_content("s", str(project / "lib.py"), str(project)) + + +@pytest.mark.skipif(os.name == "nt", reason="named pipes") +def test_named_pipe_above_a_checkout_does_not_matter(project, tmp_path): + os.mkfifo(tmp_path / "HEAD") + assert gitutil._git_toplevel(str(project)) + + +def test_bare_repo_the_session_was_started_in_keeps_its_hint(tmp_path, monkeypatch): + src = new_repo(tmp_path / "src") + hub = tmp_path / "hub.git" + git(tmp_path, "clone", "-q", "--bare", str(src), str(hub)) + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(hub)) + assert not gitutil.in_left_out_repo(str(hub)) + + +@pytest.mark.skipif(os.name == "nt", reason="symbolic links") +@pytest.mark.parametrize("session_in", ["main", "worktree"]) +def test_git_entry_linked_to_the_session_repo_is_not_its_work_tree(project, marker, monkeypatch, session_in, tmp_path): + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + git(project, "config", "filter.tidy.clean", "sh tools/c.sh") + fake = tmp_path / "fake" + (fake / "tools").mkdir(parents=True) + (fake / "tools" / "c.sh").write_text(f"{note(marker)}; cat\n") + (fake / ".gitattributes").write_text("*.py filter=tidy\n") + (fake / "lib.py").write_text("print('two')\n") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project if session_in == "main" else wt)) + for target in (wt / ".git", project / ".git"): + os.symlink(target, fake / ".git") + gitutil._other_repos.clear() + assert diffstate.capture_git_baseline(str(fake)) is None + (fake / ".git").unlink() + assert not marker.exists() + assert gitutil._git_toplevel(str(wt)) and gitutil._git_toplevel(str(project)) + + +def test_submodule_of_the_session_repo_keeps_its_config(project, marker, tmp_path, monkeypatch): + lib = new_repo(tmp_path / "upstream", "* filter=probe\n") + git(project, "-c", "protocol.file.allow=always", "submodule", "add", "-q", str(lib), "sub") + git(project, "commit", "-q", "-m", "sub") + git(project / "sub", "config", "filter.probe.clean", f"{note(marker)}; cat") + assert gitutil._git_toplevel(str(project / "sub")) + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(wt)) + gitutil._other_repos.clear() + assert gitutil._git_toplevel(str(project / "sub")) + fake = project / "fake" + fake.mkdir() + (fake / "lib.py").write_text("print('two')\n") + git(project / "sub", "worktree", "add", "-q", str(tmp_path / "subwt")) + for target in ("sub", "sub/worktrees/subwt"): + (fake / ".git").write_text(f"gitdir: ../.git/modules/{target}\n") + gitutil._other_repos.clear() + assert gitutil._git_toplevel(str(fake)) is None + # Seen from the other checkouts of the session's repository, and from the submodule. + git(wt, "-c", "protocol.file.allow=always", "submodule", "update", "-q", "--init") + git(wt / "sub", "config", "filter.probe.clean", f"{note(marker)}; cat") + git(project, "config", "filter.probe.clean", f"{note(marker)}; cat") + for start, at in ((project, wt / "sub"), (project, tmp_path / "subwt"), (project / "sub", project)): + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(start)) + gitutil._other_repos.clear() + assert gitutil._git_toplevel(str(at)), (start, at) + + +def test_stray_git_folder_in_another_repo_or_worktree(project, marker, tmp_path): + other = new_repo(project / "plain") + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + for top in (other, wt): + (top / "fix" / ".git").mkdir(parents=True) + assert os.path.samefile(gitutil._git_toplevel(str(top / "fix")), top) + filtered = nested(project / "filtered", marker, "clean") + (filtered / "fix" / ".git").mkdir(parents=True) + assert gitutil._git_toplevel(str(filtered / "fix")) is None + assert diffstate.capture_git_baseline(str(filtered / "fix")) is None + assert not marker.exists() + + +def test_commit_lookup_costs_one_git_start_per_repository(tmp_path, monkeypatch): + work = tmp_path / "work" + work.mkdir() + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(work)) + for i in range(4): + new_repo(work / f"r{i}") + runs = [] + real = subprocess.run + monkeypatch.setattr(subprocess, "run", lambda *a, **k: runs.append(a) or real(*a, **k)) + assert reporesolve.repo_containing_commit("0" * 39 + "1", [str(work)]) is None + assert len(runs) == 4 + + +@pytest.mark.parametrize("start", ["hub", "side"]) +def test_repository_inside_a_worktree_of_a_bare_hub(tmp_path, monkeypatch, marker, start): + src = new_repo(tmp_path / "src") + hub = tmp_path / "hub.git" + git(tmp_path, "clone", "-q", "--bare", str(src), str(hub)) + git(hub, "worktree", "add", "-q", "-b", "side", str(hub / "side")) + other = nested(hub / "side" / "vendor" / "lib", marker, "clean") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(hub if start == "hub" else hub / "side")) + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + assert gitutil._git_toplevel(str(hub / "side")) + + +def test_bare_layout_around_the_start_folder_vouches_for_nothing(tmp_path, monkeypatch, marker): + root = tmp_path / "unpacked" + for name in ("refs", "objects", "app"): + (root / name).mkdir(parents=True) + (root / "HEAD").write_text("ref: refs/heads/main\n") + (root / "config").write_text("[core]\n\tbare = true\n") + other = nested(root / "vendor" / "lib", marker, "clean") + for start in (root, root / "app"): + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(start)) + gitutil._other_repos.clear() + assert diffstate.capture_git_baseline(str(other)) is None + assert not marker.exists() + + +def test_git_directory_that_is_the_session_work_tree(project, marker): + for name in ("refs", "objects"): + (project / name).mkdir() + (project / "HEAD").write_text("ref: refs/heads/main\n") + (project / "config").write_text( + f'[core]\n\trepositoryformatversion = 0\n\tbare = false\n\tworktree = vendor/x\n' + f'[filter "probe"]\n\tclean = "{note(marker)}; cat"\n') + inside = project / "vendor" / "x" + inside.mkdir(parents=True) + (inside / ".git").write_text("gitdir: ../..\n") + (inside / ".gitattributes").write_text("* filter=probe\n") + (inside / "lib.py").write_text("print('two')\n") + assert gitutil._git_toplevel(str(inside)) is None + assert diffstate.capture_git_baseline(str(inside)) is None + assert diffstate.compute_v2_review_set(str(inside), None, None)[0] == [] + assert not marker.exists() + + +def holds_a_repository(where, marker, recorded="sub", on_disk="sub", again=False): + """A repository at `where` without settings of its own, that holds another + one, whose config names a command, and records it under the name `recorded`. + `again`: the repository is there already.""" + outer = where if again else new_repo(where) + (outer / ".gitmodules").write_text( + f'[submodule "sub"]\n\tpath = {on_disk}\n\turl = ./x\n\tignore = none\n') + inner = nested(outer / os.path.normpath(on_disk), marker, "clean") + git(outer, "add", ".gitmodules") + git(outer, "commit", "-q", "-m", "modules") + id_len = len(git(outer, "rev-parse", "HEAD")) + git(outer, "update-index", "--add", "--cacheinfo", + f"160000,{git(inner, 'rev-parse', 'HEAD').ljust(id_len, '1')},{recorded}") + (outer / "lib.py").write_text("print('three')\n") + marker.unlink(missing_ok=True) + return outer + + +def rewrite_index(repo, old, new): + """Change the index file itself: git would not write it like that.""" + index = repo / ".git" / "index" + data = index.read_bytes()[:-20] + assert old in data + data = data.replace(old, new) + index.write_bytes(data + hashlib.sha1(data).digest()) + os.utime(index, (1e9, 1e9)) + + +@pytest.mark.parametrize("mode", [0o160000, 0o160644, 0o160755, 0o167777, 0o360000]) +def test_any_mode_that_git_takes_for_a_repository(project, marker, mode): + outer = holds_a_repository(project / "vendor", marker) + rewrite_index(outer, b"\x00\x00\xe0\x00", struct.pack(">I", mode)) + sha = diffstate.capture_git_baseline(str(outer)) + assert sha and sha != git(outer, "rev-parse", "HEAD") + assert not marker.exists() + + +@pytest.mark.parametrize("recorded,on_disk", [ + (".!sub", "./sub"), ("sub!", "sub/"), ("a!/sub", "a//sub"), ("ab/!!/sub", "ab/../sub")]) +def test_a_repository_recorded_under_another_spelling(project, marker, recorded, on_disk): + outer = holds_a_repository(project / "vendor", marker, recorded, on_disk) + (outer / "ab").mkdir() + rewrite_index(outer, recorded.encode(), on_disk.encode()) + sha = diffstate.capture_git_baseline(str(outer)) + assert sha and sha != git(outer, "rev-parse", "HEAD") + assert not marker.exists() + + +sparse_index = pytest.mark.skipif(GIT_VERSION < (2, 35), reason="git without a sparse index") + + +def sparse_repo(path): + """A repository whose index holds `out` as one entry.""" + repo = new_repo(path) + for d in ("in", "out"): + (repo / d).mkdir() + (repo / d / "a.py").write_text("x = 1\n") + git(repo, "add", "-A") + git(repo, "commit", "-q", "-m", "dirs") + git(repo, "sparse-checkout", "init", "--cone", "--sparse-index") + git(repo, "sparse-checkout", "set", "in") + return repo + + +def tree(repo, entries): + """A tree object with the entries in the order given.""" + raw = b"".join(f"{mode} {name}".encode() + b"\0" + bytes.fromhex(sha) for mode, name, sha in entries) + raw = b"tree %d\0" % len(raw) + raw + sha = hashlib.sha1(raw).hexdigest() + (repo / ".git" / "objects" / sha[:2]).mkdir(exist_ok=True) + (repo / ".git" / "objects" / sha[:2] / sha[2:]).write_bytes(zlib.compress(raw)) + return sha + + +def pull_a_repository(repo, tmp_path, marker, modules=None, in_order=True): + """What a pull brings into `out`, which stays collapsed, and then a + repository appears there.""" + inner = nested(tmp_path / "inner", marker, "clean") + side = tmp_path / "side" + git(tmp_path, "clone", "-q", str(repo), str(side)) + files = [("100644", f"f{i}.py", git(side, "rev-parse", "HEAD:out/a.py")) for i in range(10)] + link = [("160000", "sub", git(inner, "rev-parse", "HEAD"))] + out = tree(side, files + link if in_order else link + files) + git(side, "rm", "-q", "-r", "--cached", "out") + if modules: + (side / ".gitmodules").write_text(modules) + git(side, "add", ".gitmodules") + top = git(side, "write-tree") + entries = [line.split() for line in git(side, "ls-tree", top).splitlines()] + entries = [(mode.lstrip("0"), name, sha) for mode, _, sha, name in entries] + [("40000", "out", out)] + commit = git(side, "commit-tree", tree(side, sorted(entries, key=lambda e: e[1])), "-p", "HEAD", "-m", "sub") + git(side, "update-ref", "HEAD", commit) + git(repo, "pull", "-q", str(side), "HEAD") + assert "040000" in git(repo, "ls-files", "-s", "--sparse") + (repo / "out").mkdir(exist_ok=True) + os.rename(inner, repo / "out" / "sub") + (repo / "lib.py").write_text("print('three')\n") + marker.unlink(missing_ok=True) + + +@sparse_index +@pytest.mark.parametrize("modules", [ + None, '[submodule "sub"]\n\tpath = out/sub\n\turl = ./x\n', + '[submodule "sub"]\n\tpath = out/sub\n\turl = ./x\n\tignore = none\n']) +def test_a_repository_inside_a_collapsed_directory(tmp_path, monkeypatch, marker, modules): + project = sparse_repo(tmp_path / "proj") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + pull_a_repository(project, tmp_path, marker, modules) + sha = diffstate.capture_git_baseline(str(project)) + assert sha and sha != git(project, "rev-parse", "HEAD") + assert not marker.exists() + + +@sparse_index +def test_collapsed_directory_that_is_not_in_order(tmp_path, monkeypatch, marker): + project = sparse_repo(tmp_path / "proj") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + pull_a_repository(project, tmp_path, marker, in_order=False) + assert diffstate.capture_git_baseline(str(project)) + assert not marker.exists() + + +@sparse_index +def test_collapsed_directory_that_appears_later(tmp_path, monkeypatch, marker): + project = sparse_repo(tmp_path / "proj") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + pull_a_repository(project, tmp_path, marker) + os.rename(project / "out", tmp_path / "out") + real = gitutil._drop_gitlinks + + def then_appears(*args): + done = real(*args) + os.rename(tmp_path / "out", project / "out") + return done + + monkeypatch.setattr(gitutil, "_drop_gitlinks", then_appears) + sha = diffstate.capture_git_baseline(str(project)) + assert sha and sha != git(project, "rev-parse", "HEAD") + assert not marker.exists() + + +OTHER_INDEXES = [pytest.param("sparse", marks=sparse_index), "split", + pytest.param("sha256", marks=pytest.mark.skipif( + GIT_VERSION < (2, 42), reason="git with only one kind of object id"))] + + +@pytest.fixture(params=OTHER_INDEXES) +def kind_of_index(request): + return request.param + + +def other_index(path, kind): + """A repository with a changed file, and an index of that kind.""" + if kind == "sparse": + repo = sparse_repo(path) + else: + path.mkdir(parents=True) + git(path, "init", "-q", *(["--object-format=sha256"] if kind == "sha256" else [])) + repo = new_repo(path) + if kind == "split": + git(repo, "update-index", "--split-index") + (repo / "lib.py").write_text("print('three')\n") + return repo + + +def test_baseline_of_another_repository_with_another_kind_of_index(project, kind_of_index): + repo = other_index(project / "vendor", kind_of_index) + git_dir = repo / ".git" + before, files, names = (git_dir / "index").read_bytes(), set(os.listdir(git_dir)), git(repo, "ls-files") + sha = diffstate.capture_git_baseline(str(repo)) + (repo / "new.py").write_text("x = 1\n") + with gitutil._temp_index(str(repo), ["new.py"]) as env: + if kind_of_index == "sha256": + assert git(repo, "diff", "--name-only", "HEAD", sha) == "lib.py" + listed = subprocess.run(["git", "ls-files"], cwd=repo, env=env, check=True, + capture_output=True, text=True).stdout.split("\n") + assert listed == sorted([*names.split("\n"), "new.py"]) + [""] + else: + assert sha == git(repo, "rev-parse", "HEAD") + assert env is None + assert (git_dir / "index").read_bytes() == before + assert set(os.listdir(git_dir)) == files + + +def test_turns_in_another_repository_with_another_kind_of_index( + tmp_path, hook_env, stub_api, kind_of_index): + """The session was started in a folder that only holds it. New files that + cannot be added are told about once.""" + hook_env["CLAUDE_PROJECT_DIR"] = str(tmp_path) + repo = other_index(tmp_path / "vendor", kind_of_index) + for turn in range(3): + conftest.run_hook(conftest.ups_payload(repo), hook_env) + (repo / "lib.py").write_text(conftest.VULN_PY.replace("def run", f"def turn{turn}")) + (repo / f"new{turn}.py").write_text(conftest.VULN_PY.replace("def run", f"def added{turn}")) + code, out, _ = conftest.run_hook(conftest.stop_payload(repo), hook_env) + m = conftest.metrics_of(out) + sent = "".join(stub_api.bodies) + assert f"def turn{turn}" in sent + if kind_of_index == "sha256": + assert code == 0 and "add_fail" not in m and f"def added{turn}" in sent, m + else: + assert code == (0 if turn else 2) and m["add_fail"] // 100000 == 5, m + + +def test_split_index_set_for_all_repositories_leaves_nothing_behind(project, clean_env): + repo = new_repo(project / "vendor") + (clean_env / ".gitconfig").write_text("[core]\n\tsplitIndex = true\n") + git(repo, "update-index", "--split-index") + files = set(os.listdir(repo / ".git")) + for n in range(3): + (repo / "lib.py").write_text(f"x = {n}\n") + diffstate.capture_git_baseline(str(repo)) + (repo / f"new{n}.py").write_text("x = 1\n") + with gitutil._temp_index(str(repo), [f"new{n}.py"]): + pass + assert set(os.listdir(repo / ".git")) == files + + +@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="named pipes") +@pytest.mark.parametrize("kind", ["settings", "split"]) +def test_named_pipe_in_a_worktree_of_the_session_repo(project, tmp_path, kind): + wt = project / "wt" + git(project, "worktree", "add", "-q", str(wt)) + if kind == "settings": + git(project, "config", "filter.probe.clean", "cat") + else: + git(wt, "update-index", "--split-index") + back = project / ".git" / "worktrees" / "wt" / "gitdir" + back.unlink() + os.mkfifo(back) + start = time.monotonic() + prompt_hook(wt, tmp_path) + assert time.monotonic() - start < 30 + + +@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="named pipes") +def test_named_pipe_in_the_settings_below_a_git_that_is_a_script(project, tmp_path, monkeypatch): + """Killing the script leaves git itself with the pipe open.""" + repo = new_repo(project / "vendor") + os.mkfifo(repo / "pipe") + add_config(repo, "[include]\n\tpath = ../pipe\n") + shim = tmp_path / "shim" + shim.write_text('#!/bin/sh\ngit "$@"\nexit $?\n') + shim.chmod(0o755) + monkeypatch.setattr(gitutil, "GIT_CMD", [str(shim), *gitutil.GIT_CMD[1:]]) + start = time.monotonic() + assert gitutil._stay_out(str(repo)) + assert time.monotonic() - start < 20 + with open(os.open(repo / "pipe", os.O_WRONLY | os.O_NONBLOCK), "wb"): + pass # lets what is left of git end + + +def test_settings_are_read_without_a_folder_to_write_in(project, tmp_path, monkeypatch): + repo = new_repo(project / "vendor") + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + + def none(*args, **kw): + raise FileNotFoundError("No usable temporary directory found") + + monkeypatch.setattr(gitutil.tempfile, "TemporaryFile", none) + assert gitutil._git_toplevel(str(repo)) and gitutil._git_toplevel(str(wt)) + + +@pytest.mark.skipif(os.name == "nt", reason="symbolic links and devices") +@pytest.mark.parametrize("where", ["other", "session", "file"]) +def test_index_that_is_a_link(project, where): + """To something without an end, or to an ordinary file.""" + repo = project if where == "session" else new_repo(project / "vendor") + index = repo / ".git" / "index" + edit(repo) + if where == "file": + index.rename(repo / ".git" / "real") + index.symlink_to("real") + else: + index.unlink() + index.symlink_to("/dev/zero") + start = time.monotonic() + copy = repo / ".git" / "copy" + copy.symlink_to(index) + assert gitutil._index_holds(copy, b"nothing") is (where != "file") + assert (gitutil._write_file_entries(str(repo), str(copy)) is None) is (where != "file") + diffstate.capture_git_baseline(str(repo)) + assert time.monotonic() - start < 20 + + +def test_index_that_grows_past_the_limit(project, monkeypatch): + repo = new_repo(project / "vendor") + index = repo / ".git" / "index" + monkeypatch.setattr(gitutil, "_MAX_INDEX_BYTES", os.path.getsize(index) - 1) + assert gitutil._write_file_entries(str(repo), str(index)) is None + + +@pytest.mark.parametrize("empty", ["folder", "file"]) +def test_folder_in_no_repository_below_an_entry_that_is_none(project, tmp_path, hook_env, empty): + home = tmp_path / "some" + (home / "notes").mkdir(parents=True) + if empty == "folder": + (home / ".git").mkdir() + else: + (home / ".git").write_text("") + cwd = str(home / "notes") + assert not gitutil.in_left_out_repo(cwd) + assert gitutil.session_repo_instead_of(cwd) is None + assert gitutil._git_toplevel(cwd) is None + assert gitutil.repos_left_alone() == 0 + + +@pytest.mark.parametrize("what", ["worktree", "other", "session"]) +def test_config_that_lists_too_much(project, tmp_path, monkeypatch, what): + repo = project + if what == "other": + repo = new_repo(project / "vendor") + add_config(repo, "[a]\n" + f"\tb = {'x' * 1000}\n" * 200) + cwd = repo + if what == "worktree": + cwd = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(cwd)) + assert gitutil._git_toplevel(str(cwd)) and gitutil.session_repo_commands() == 0 + monkeypatch.setattr(gitutil, "_MAX_CONFIG_BYTES", 100_000) + gitutil._other_repos.clear() + assert bool(gitutil._git_toplevel(str(cwd))) is (what == "session") + assert gitutil.session_repo_commands() == (256 if what != "other" else 0) + + +@pytest.mark.parametrize("mode", [0o160000, 0o160644]) +def test_repository_in_another_ones_index_of_another_kind(project, marker, kind_of_index, mode): + outer = holds_a_repository(other_index(project / "vendor", kind_of_index), marker, again=True) + if mode != 0o160000: + n = 32 if kind_of_index == "sha256" else 20 + index, swap = outer / ".git" / "index", {b"\x00\x00\xe0\x00": struct.pack(">I", mode)} + # The index names the file it shares by its checksum. + for f in [*(outer / ".git").glob("sharedindex.*"), index]: + data = f.read_bytes() + f.chmod(0o600) + old, data = data[-n:], data[:-n] + for a, b in swap.items(): + data = data.replace(a, b) + new = gitutil._ID_HASHES[n](data).digest() + f.write_bytes(data + new) + if f != index: + swap[old] = new + f.rename(f.with_name("sharedindex." + new.hex())) + os.utime(index, (1e9, 1e9)) + assert b"%o" % mode in subprocess.run(["git", "ls-files", "-s"], cwd=outer, capture_output=True).stdout + assert diffstate.capture_git_baseline(str(outer)) + (outer / "new.py").write_text("x = 1\n") + with gitutil._temp_index(str(outer), ["new.py"]) as env: + assert bool(env) is (kind_of_index == "sha256") + assert not marker.exists() + + +@sparse_index +@pytest.mark.parametrize("in_order", [True, False]) +def test_repository_in_a_collapsed_directory_of_another_one(project, tmp_path, marker, in_order): + outer = sparse_repo(tmp_path / "outer") + pull_a_repository(outer, tmp_path, marker, '[submodule "sub"]\n\tpath = out/sub\n\turl = ./x\n\tignore = none\n', + in_order=in_order) + assert diffstate.capture_git_baseline(str(outer)) == git(outer, "rev-parse", "HEAD") + (outer / "new.py").write_text("x = 1\n") + with gitutil._temp_index(str(outer), ["new.py"]) as env: + assert env is None + assert not marker.exists() + + +@sparse_index +def test_worktree_of_a_session_repo_with_a_sparse_index(tmp_path, monkeypatch): + """It keeps its index, whole or not.""" + project = sparse_repo(tmp_path / "proj") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + git(wt, "sparse-checkout", "init", "--cone", "--sparse-index") + git(wt, "sparse-checkout", "set", "in") + assert gitutil.may_be_sparse(project / ".git" / "worktrees" / "wt" / "index") + (wt / "lib.py").write_text("print('three')\n") + sha = diffstate.capture_git_baseline(str(wt)) + assert git(wt, "diff", "--name-only", "HEAD", sha) == "lib.py" + + +def entries(index): + env = {**os.environ, "GIT_INDEX_FILE": str(index)} + run = lambda *a: subprocess.run(["git", *a], cwd=index.parent, env=env, check=True, + capture_output=True).stdout + return run("ls-files", "-s", "-v", "-z"), re.sub(rb"(?m)^ ", b"", run("ls-files", "--debug")) + + +@pytest.mark.parametrize("version", ["2", "4"]) +def test_copy_with_only_the_files(tmp_path, version): + repo = new_repo(tmp_path / "r") + for n in range(1, 20): + f = repo / "d" / ("x" * n) + f.parent.mkdir(exist_ok=True) + f.write_text(str(n)) + (repo / "d" / "run.sh").write_text("x") + git(repo, "add", "-A") + git(repo, "update-index", "--chmod=+x", "d/run.sh") + blob = git(repo, "rev-parse", "HEAD:lib.py") + for mode, name in (("120000", "lnk"), ("100644", "y" * 300), ("100644", "z" * 4200)): + git(repo, "update-index", "--add", "--cacheinfo", f"{mode},{blob},{name}") + git(repo, "update-index", "--assume-unchanged", "d/x") + git(repo, "update-index", "--skip-worktree", "d/xx") + (repo / "new.py").write_text("x") + git(repo, "add", "-N", "new.py") + git(repo, "update-index", "--index-version", version) + index = repo / ".git" / "index" + before = index.read_bytes() + new, left_out = gitutil._file_entries(before) + copy = repo / ".git" / "copy" + copy.write_bytes(new) + assert left_out == 0 + assert entries(copy) == entries(index) + git(repo, "update-index", "--add", "--cacheinfo", f"160000,{'12' * 20},d/xxxy") + new, left_out = gitutil._file_entries(index.read_bytes()) + copy.write_bytes(new) + assert left_out == 1 + assert b"160000" in entries(index)[0] and b"160000" not in entries(copy)[0] + assert entries(copy)[0].count(b"\0") == entries(index)[0].count(b"\0") - 1 + + +@pytest.mark.parametrize("change", [ + lambda d: d[:-21] + d[-20:], lambda d: d[:40], lambda d: b"", lambda d: b"X" + d[1:], + lambda d: d[:8] + b"\xff\xff\xff\xff" + d[12:], lambda d: d[:7] + b"\x09" + d[8:], + lambda d: d[:-20] + b"link\0\0\0\0" + d[-20:], lambda d: d[:-20] + b"sdir\0\0\0\0" + d[-20:], + lambda d: d[:-20] + b"AAAA\0\0\0\x09" + d[-20:]]) +def test_index_that_is_not_read_as_git_wrote_it(tmp_path, change): + data = (new_repo(tmp_path / "r") / ".git" / "index").read_bytes() + gitutil._file_entries(data) + with pytest.raises((ValueError, IndexError, struct.error)): + gitutil._file_entries(change(data)) + + +def test_index_that_asks_to_shorten_a_name_by_more_than_it_has(tmp_path): + entry = bytearray(62) + struct.pack_into(">I", entry, 24, 0o100644) + struct.pack_into(">H", entry, 60, 1) + data = b"DIRC" + struct.pack(">II", 4, 1) + bytes(entry) + b"\xff" * 4_000_000 + b"\x01a\0" + b"\0" * 20 + start = time.monotonic() + with pytest.raises(ValueError): + gitutil._file_entries(data) + assert time.monotonic() - start < 1 + + +@pytest.mark.skipif(GIT_VERSION < (2, 42), reason="git with only one kind of object id") +def test_index_with_other_object_ids(project): + repo = project / "vendor" + repo.mkdir() + git(repo, "init", "-q", "--object-format=sha256") + new_repo(repo) + ids = (new_repo(project / "other") / ".git" / "index").read_bytes() + index = repo / ".git" / "index" + index.write_bytes(ids) # one that reads well with the usual ids + assert gitutil._write_file_entries(str(repo), str(index)) is None + assert index.read_bytes() == ids + + +def test_new_files_left_out_with_the_copy_are_noted(project, monkeypatch): + repo = new_repo(project / "vendor") + (repo / "new.py").write_text("x = 1\n") + with gitutil._temp_index(str(repo), ["new.py"]) as env: + assert env + monkeypatch.setattr(gitutil, "_add_failed", {"code": 0, "left_out": False}) + monkeypatch.setattr(gitutil, "_MAX_INDEX_BYTES", 10) + with gitutil._temp_index(str(repo), ["new.py"]) as env: + assert env is None + assert gitutil._add_failed == {"code": 500001, "left_out": True} + + +@pytest.mark.parametrize("big", [False, True]) +def test_worktree_of_the_session_repo_keeps_its_index(project, tmp_path, monkeypatch, big): + wt = tmp_path / "wt" + git(project, "worktree", "add", "-q", str(wt)) + (wt / "lib.py").write_text("print('three')\n") + if big: + monkeypatch.setattr(gitutil, "_BIG_INDEX_BYTES", 0) + rewritten = [] + real = gitutil._write_file_entries + monkeypatch.setattr(gitutil, "_write_file_entries", lambda *a: rewritten.append(a) or real(*a)) + sha = diffstate.capture_git_baseline(str(wt)) + assert sha and sha != git(wt, "rev-parse", "HEAD") + assert bool(rewritten) is not big + + +def test_copy_of_another_repository(project, monkeypatch): + repo = new_repo(project / "vendor") + index = repo / ".git" / "index" + os.utime(index, ns=(10**18, 10**18)) + before = index.read_bytes() + folder = None + with gitutil._temp_index(str(repo), []) as env: + copy = env["GIT_INDEX_FILE"] + folder = os.path.dirname(copy) + assert os.stat(copy).st_mtime_ns == 10**18 + assert not os.path.samefile(copy, index) + assert index.read_bytes() == before + assert not [n for n in os.listdir(folder) if n.startswith(os.path.basename(copy))] + + +def test_whole_tree_add_in_a_repository_with_link_in_a_name(project): + (project / "linked.py").write_text("x") + git(project, "add", "linked.py") + (project / "new.py").write_text("x") + with gitutil._temp_index(str(project)) as env: + assert env + + +@sparse_index +def test_new_file_next_to_a_collapsed_directory(tmp_path, monkeypatch, marker): + project = sparse_repo(tmp_path / "proj") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + pull_a_repository(project, tmp_path, marker) + (project / "in" / "new.py").write_text("x = 1\n") + with gitutil._temp_index(str(project), ["in/new.py"]) as env: + listed = subprocess.run([*gitutil.GIT_CMD, *gitutil.SPARSE_AS_IT_IS, "ls-files", "-s", "--sparse"], + cwd=project, env=env, capture_output=True, text=True).stdout + assert "in/new.py" in listed and "160000" not in listed + assert not marker.exists() + + +def test_repository_the_user_named_is_kept(project, tmp_path, monkeypatch): + home = tmp_path / "dots" + home.mkdir() + (home / "lib.py").write_text("print('one')\n") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(home)) + monkeypatch.setenv("GIT_DIR", str(tmp_path / "store")) + monkeypatch.setenv("GIT_WORK_TREE", str(home)) + git(home, "init", "-q") + git(home, "add", "-A") + git(home, "commit", "-q", "-m", "i") + git(home, "update-index", "--split-index") + (home / "lib.py").write_text("print('three')\n") + sha = diffstate.capture_git_baseline(str(home)) + assert sha and sha != git(home, "rev-parse", "HEAD") + + +def test_ordinary_repository_that_holds_another_keeps_its_baseline(project, tmp_path, marker): + outer = new_repo(project / "vendor") + inner = new_repo(tmp_path / "inner") + git(outer, "update-index", "--add", "--cacheinfo", + f"160000,{git(inner, 'rev-parse', 'HEAD')},sub") + git(outer, "commit", "-q", "-m", "sub") + os.rename(inner, outer / "sub") + (outer / "lib.py").write_text("print('three')\n") + sha = diffstate.capture_git_baseline(str(outer)) + assert sha and sha != git(outer, "rev-parse", "HEAD") + + +@pytest.mark.parametrize("start_there", [True, False]) +def test_index_of_the_session_repo_that_is_not_in_order(project, tmp_path, monkeypatch, marker, start_there): + """git does not find an entry that is out of order to remove it.""" + outer = holds_a_repository(tmp_path / "own", marker, "!ub", "sub") + rewrite_index(outer, b"!ub", b"sub") + if start_there: + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(outer)) + else: + monkeypatch.delenv("CLAUDE_PROJECT_DIR") + assert diffstate.capture_git_baseline(str(outer)) == git(outer, "rev-parse", "HEAD") + (outer / "new.py").write_text("x = 1\n") + with gitutil._temp_index(str(outer), ["new.py"]) as env: + assert env is None + assert gitutil._add_failed["left_out"] + assert not marker.exists() |
