From b99e362a8c759f355678e417732d1f152455abf4 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 11 Nov 2026 14:26:49 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=202:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=202=20=F0=9F=9F=A1=20(0=20misread);=20test=20the?= =?UTF-8?q?=20submodule=20filter,=20skip=20the=20git=20call=20for=20an=20u?= =?UTF-8?q?ntracked=20file,=20merge=20a=20repeated=20breach,=20cap=20the?= =?UTF-8?q?=20staged=20blob?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/rule_hook/committed_files.py | 8 +++++--- bin/rule_hook/test_rule_hook.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/bin/rule_hook/committed_files.py b/bin/rule_hook/committed_files.py index 1dd89ca6..b30ec7ef 100644 --- a/bin/rule_hook/committed_files.py +++ b/bin/rule_hook/committed_files.py @@ -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: diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 2be74551..9e3f597c 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -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])