Answer stability round 2: 0 🔴, 2 🟡 (0 misread); test the submodule filter, skip the git call for an untracked file, merge a repeated breach, cap the staged blob
CI / tests (pull_request) Successful in 2m4s

This commit is contained in:
2026-11-11 14:26:49 +01:00
parent 89bd9e86cc
commit b99e362a8c
2 changed files with 20 additions and 3 deletions
+5 -3
View File
@@ -23,12 +23,14 @@ def committed_file_breaches(directory: str) -> list[Breach]:
breaches: list[Breach] = []
for name in sorted(tracked | (set() if isinstance(untracked, Failure) else names_in(untracked))):
path = posixpath.join(top, name)
before = blob(top, "HEAD" if name in tracked else "", name)
# An untracked file has neither a version in HEAD nor one in the index. A tracked file costs two git calls, about 4 ms each: a commit of 3000 changed files uses 13 of the hook's 30 seconds.
before = blob(top, "HEAD", name) if name in tracked else ""
for after in dict.fromkeys([read(path) if size(path) <= MAX_BYTES else "", blob(top, "", name) if name in tracked else ""]):
known = set(before.splitlines())
added = "\n".join(line for line in after.splitlines() if line not in known)
breaches += [dataclasses.replace(breach, marked=added) for breach in file_breaches(path, before, added, after)]
return list(dict.fromkeys(breaches))
# The index copy and the work-tree copy of a file can breach the same way; the first stands.
return list({(breach.rule, breach.detail): breach for breach in reversed(breaches)}.values())[::-1]
def tracked_names(top: str) -> set[str]:
@@ -48,7 +50,7 @@ def names_in(listing: str) -> set[str]:
def blob(top: str, commit: str, name: str) -> str:
"""name's text in the commit, or in the index where commit is empty; "" where there is none."""
shown = git_repo.git("show", f"{commit}:{name}", cwd=top, guarded=True)
return "" if isinstance(shown, Failure) else shown
return "" if isinstance(shown, Failure) or len(shown) > MAX_BYTES else shown
def size(path: str) -> int:
+15
View File
@@ -982,6 +982,21 @@ class CommittedFiles(unittest.TestCase):
self.assertEqual(self.commit("git commit -m x").returncode, 0)
self.assertFalse(pathlib.Path(self.repo, "ran").exists())
def test_a_submodule_runs_no_clean_filter(self):
inner = tempfile.mkdtemp()
self.addCleanup(subprocess.run, ["rm", "-rf", inner])
git(inner, "init", "-q", "-b", "main")
pathlib.Path(inner, "b.txt").write_text("b\n")
pathlib.Path(inner, ".gitattributes").write_text("b.txt filter=planted\n")
git(inner, "add", "-A")
git(inner, "commit", "-qm", "x", "--no-verify")
git(self.repo, "-c", "protocol.file.allow=always", "submodule", "add", "-q", inner, "sub")
git(self.repo, "commit", "-qm", "x", "--no-verify")
git(f"{self.repo}/sub", "config", "filter.planted.clean", f"touch {self.repo}/ran; cat")
os.utime(pathlib.Path(self.repo, "sub", "b.txt"), (1, 1))
self.assertEqual(self.commit("git commit -m x").returncode, 0)
self.assertFalse(pathlib.Path(self.repo, "ran").exists())
def test_a_checkout_with_no_commit_checks_its_index_and_untracked_files(self):
fresh = tempfile.mkdtemp()
self.addCleanup(subprocess.run, ["rm", "-rf", fresh])