summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-11-04 19:36:21 +0000
committerClaude <noreply@anthropic.com>2026-11-04 20:06:27 +0000
commit2dd1569edd65f7f41cb04790dec1780cbc1e5511 (patch)
tree9c58f2fa03c48b0d02275ab2552e1939d468e2b2 /plugins
parentfa4eb54397ee2faccb6940512a19c017cb1337f3 (diff)
security-guidance: drop every gitlink from the index copy
Whether git starts something in a gitlink's directory is decided when git runs, so the check for a .git there beforehand can go stale. Remove all of them from the copy. On git older than 2.34 the baseline for a submodule that is not checked out falls back to HEAD. Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py16
-rw-r--r--plugins/security-guidance/tests/test_nested_repos.py18
2 files changed, 21 insertions, 13 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index ec51e3c6..1a0a65e2 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -800,20 +800,20 @@ def _drop_gitlinks(top, env, tmp_index):
anything else runs on the copy. A gitlink marks a directory that is, or
can be, another repository (see diff.ignoreSubmodules in GIT_CMD):
`git stash create` takes no flag like IGNORE_SUBMODULES, and `git add`
- reads the gitlinks its paths cover. An entry whose directory has no
- `.git` stays: git starts nothing there, and without the entry an older
- `git stash create` fails on the empty directory. `top` is the work
- tree's top directory. Returns True on success, False on any failure; the
- copy must not be used then."""
+ reads the gitlinks its paths cover. Every gitlink goes, including one
+ whose directory has no `.git` now: whether git starts something there is
+ decided when git runs, not here. Without the entry, `git stash create` on
+ git older than 2.34 fails on a submodule that is not checked out, and the
+ baseline falls back to HEAD. `top` is the work tree's top directory.
+ Returns True on success, False on any failure; the copy must not be used
+ then."""
try:
if not _may_have_gitlinks(tmp_index):
return True
gitlinks = _gitlink_paths(top, env)
if gitlinks is None:
return False
- top_b = os.fsencode(top)
- nested = sorted(p for p in gitlinks
- if os.path.lexists(os.path.join(top_b, p, b".git")))
+ nested = sorted(gitlinks)
if not nested:
return True
mtime_ns = os.stat(tmp_index).st_mtime_ns
diff --git a/plugins/security-guidance/tests/test_nested_repos.py b/plugins/security-guidance/tests/test_nested_repos.py
index 3c93a580..addf9c2e 100644
--- a/plugins/security-guidance/tests/test_nested_repos.py
+++ b/plugins/security-guidance/tests/test_nested_repos.py
@@ -318,8 +318,9 @@ def test_untracked_file_name_is_not_read_as_a_pattern(outer, git_env):
assert name in copy and "other.py" not in copy
-def test_gitlink_without_a_repository_stays_in_the_copy(outer, marker, git_env):
- """A submodule that is not checked out: an empty directory."""
+def test_gitlink_without_a_repository_is_removed_from_the_copy(outer, marker, git_env):
+ """A submodule that is not checked out: an empty directory. Its entry goes
+ too, since a `.git` can appear there before git runs."""
nested = _record_nested_repo(outer, marker)
# Moved, not deleted: git's object files are read-only on Windows.
nested.rename(outer.parent / "moved-away")
@@ -330,9 +331,16 @@ def test_gitlink_without_a_repository_stays_in_the_copy(outer, marker, git_env):
copy = _index_entries(outer, env)
baseline = diffstate.capture_git_baseline(str(outer))
- assert "160000" in copy
- assert baseline and baseline != git(outer, "rev-parse", "HEAD").strip()
- assert diffstate.last_baseline_fail == 0
+ assert "160000" not in copy
+ assert "160000" in git(outer, "ls-files", "-s")
+ head = git(outer, "rev-parse", "HEAD").strip()
+ version = tuple(int(n) for n in git(outer, "--version").split()[2].split(".")[:2])
+ if version >= (2, 34):
+ assert baseline and baseline != head
+ assert diffstate.last_baseline_fail == 0
+ else: # `git stash create` fails on the empty directory
+ assert baseline == head
+ assert diffstate.last_baseline_fail == 1
def test_gitlinks_listed_by_a_git_without_sparse_listing(outer, marker, git_env, monkeypatch):