Skip to content

The agent that grows with you

fix(check): blob-size exempts untracked copies and reads --head's allowlist (review)

Tekniuma0a49926f44c
diff --git a/scripts/ci/check_blob_size.py b/scripts/ci/check_blob_size.py
index 81c4b669bee..531114ab37f 100644
--- a/scripts/ci/check_blob_size.py
+++ b/scripts/ci/check_blob_size.py
@@ -155,17 +155,34 @@ def _sizes(repo: Path, blobs: list[str]) -> dict[str, int]:
return sizes
-def read_allowlist(path: Path) -> set[str]:
- if not path.is_file():
- return set()
+def _hash_file(repo: Path, rel: str) -> str:
+ """The blob sha ``git add`` would store for a working-tree file (clean filters applied)."""
+ return _git(repo, "hash-object", "--", rel).strip()
+
+
+def parse_allowlist(text: str) -> set[str]:
entries = set()
- for line in path.read_text(encoding="utf-8-sig").splitlines():
+ for line in text.lstrip("\ufeff").splitlines():
line = line.strip()
if line and not line.startswith("#"):
entries.add(line)
return entries
+def read_allowlist(path: Path) -> set[str]:
+ if not path.is_file():
+ return set()
+ return parse_allowlist(path.read_text(encoding="utf-8-sig"))
+
+
+def allowlist_at(repo: Path, rev: str) -> set[str]:
+ """The allowlist as revision ``rev`` holds it (empty when ``rev`` has none)."""
+ if not _git(repo, "rev-parse", "--verify", "--quiet", f"{rev}:{ALLOWLIST}", check=False).strip():
+ _git(repo, "rev-parse", "--verify", f"{rev}^{{tree}}") # a bad revision is a usage error
+ return set()
+ return parse_allowlist(_git(repo, "cat-file", "blob", f"{rev}:{ALLOWLIST}"))
+
+
def find_offenders(repo: Path, base: str, head: str | None, limit: int = MAX_BYTES,
allowlist: Collection[str] = frozenset()) -> list[Offender]:
"""Changed paths base -> head (None = working tree + untracked files) over ``limit``."""
@@ -190,7 +207,7 @@ def find_offenders(repo: Path, base: str, head: str | None, limit: int = MAX_BYT
if rel in allowlist or not disk.is_file() or disk.is_symlink():
continue
size = disk.stat().st_size
- if size > limit:
+ if size > limit and _hash_file(repo, rel) not in known: # an untracked copy adds nothing
kind = "binary" if _sniff_binary(disk) else "new file"
offenders.append(Offender(rel, size, kind, "A"))
return sorted(offenders, key=lambda o: (-o.size, o.path))
@@ -236,7 +253,8 @@ def main(argv: list[str] | None = None) -> int:
repo = args.repo.resolve()
try:
base = args.base or default_base(repo, args.head)
- allowlist = read_allowlist(repo / ALLOWLIST)
+ # The judged side's own exceptions: the revision's list, or the working tree's.
+ allowlist = allowlist_at(repo, args.head) if args.head else read_allowlist(repo / ALLOWLIST)
offenders = find_offenders(repo, base, args.head, args.limit, allowlist)
except GitError as exc:
print(f"check_blob_size: {exc}", file=sys.stderr)
diff --git a/tests/ci/test_check_blob_size.py b/tests/ci/test_check_blob_size.py
index 0230d8ab650..8c5bd3d12f5 100644
--- a/tests/ci/test_check_blob_size.py
+++ b/tests/ci/test_check_blob_size.py
@@ -141,6 +141,43 @@ def test_working_tree_mode_sees_unstaged_and_untracked_files(repo):
]
+def test_untracked_copy_of_base_content_adds_no_bytes(repo):
+ # Staging the copy would pass (its blob is already in the base); so must leaving it untracked.
+ base = _git(repo, "rev-parse", "HEAD")
+ (repo / "copies").mkdir()
+ (repo / "copies/icon.icns").write_bytes((repo / "assets/icon.icns").read_bytes())
+ (repo / "copies/big.lock").write_bytes((repo / "big.lock").read_bytes())
+ (repo / "copies/icon-edited.icns").write_bytes(_binary(LIMIT * 2, 11))
+ assert _offenders(repo, base, None) == [("copies/icon-edited.icns", "binary", "A")]
+ _git(repo, "add", "--", "copies/icon.icns", "copies/big.lock")
+ assert _offenders(repo, base, None) == [("copies/icon-edited.icns", "binary", "A")]
+
+
+def test_cli_with_head_reads_the_allowlist_from_head_not_the_checkout(repo):
+ base = _git(repo, "rev-parse", "HEAD")
+ head = _commit(repo, {".github/blob-size-allowlist.txt": b"pr-assets/kept.png\n",
+ "pr-assets/kept.png": _binary(LIMIT * 2, 1), "pr-assets/shot.png": _binary(LIMIT * 2, 2)})
+ # The checkout now exempts the other file and not the one head exempts.
+ _git(repo, "checkout", "-q", base)
+ (repo / ".github").mkdir(exist_ok=True)
+ (repo / ".github/blob-size-allowlist.txt").write_text("pr-assets/shot.png\n", encoding="utf-8")
+
+ def run(*extra: str) -> subprocess.CompletedProcess:
+ return subprocess.run([sys.executable, str(SCRIPT), "--base", base, *extra, "--limit", str(LIMIT)],
+ cwd=repo, env=_env(), capture_output=True, text=True, encoding="utf-8",
+ timeout=60, check=False)
+
+ proc = run("--head", head)
+ assert proc.returncode == 1, proc.stdout + proc.stderr
+ assert "::error file=pr-assets/shot.png::" in proc.stdout
+ assert "pr-assets/kept.png" not in proc.stdout
+ # A head without the list exempts nothing, whatever the checkout says.
+ bare = _commit(repo, {"pr-assets/shot.png": _binary(LIMIT * 2, 2)})
+ assert run("--head", bare).returncode == 1
+ # Working-tree mode keeps reading the list from disk.
+ assert run().returncode == 0
+
+
def test_cli_fails_with_fix_text_and_writes_step_summary(repo, tmp_path):
base = _git(repo, "rev-parse", "HEAD")
(repo / ".github").mkdir()