diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-11-06 11:41:47 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-06 11:41:47 -0800 |
| commit | 385e32d91174c6955f5cc52f9a84da314fddb3a0 (patch) | |
| tree | 8631aa6bda52834cc13e5bc3bcbdbadd786718fc /plugins | |
| parent | 83b5049e23ad084de27a63ddec2b9af3a52cdc60 (diff) | |
| parent | 64e2f0cbf3f9c076a2709428e92fdb77e4da6f04 (diff) | |
Merge pull request #7000 from anthropics/mhegazy/sg-index-copy-all-gitlinks
security-guidance: drop every gitlink from the index copy
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 16 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_nested_repos.py | 18 |
2 files changed, 21 insertions, 13 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index c80d0600..4c363ef4 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -922,11 +922,13 @@ 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.""" global last_dropped_gitlinks last_dropped_gitlinks = False try: @@ -935,9 +937,7 @@ def _drop_gitlinks(top, env, tmp_index): 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 8357abf1..6fdcdad6 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): |
