Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(ingestion): exclude shallow-clone boundary lines from blame ownership (#3073)
Amanbc49217fdb65
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/function_blame.py b/packages/core/src/repowise/core/ingestion/git_indexer/function_blame.pyindex 66dc413d5..4413a151b 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/function_blame.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/function_blame.py@@ -69,6 +69,15 @@ class BlameIndex:# The indexer's history anchor (unix seconds), so line ages are measured# from the indexed commit like every other git window.as_of_ts: int | None = None+ # Shas git marks ``boundary`` in the porcelain output: the oldest commit+ # it can see on each line's history. On a full clone this is the true+ # root commit; on a shallow clone it is the fetch boundary, and every+ # line older than it is misattributed to whoever happened to touch it+ # last before the cutoff (#3055). Ownership excludes these shas only+ # when ``is_shallow`` is set, since a full clone's true root author is a+ # real signal, not an artifact.+ boundary_shas: frozenset[str] = field(default_factory=frozenset)+ is_shallow: bool = Falsedef blame_as_of(idx: BlameIndex) -> int:@@ -81,14 +90,18 @@ def ownership_from_blame(idx: BlameIndex) -> tuple[str | None, str | None, floatReturns ``(name, email, share)`` where ``share`` is the fraction ofblamed lines authored by the top author. ``None``s when the index is- empty (no signal).+ empty, or when every blamed line is a shallow-clone boundary line (no+ attributable signal) — the caller falls back to the commit-based owner."""if not idx.lines:return None, None, Nonefrom collections import Counter+ excluded = idx.boundary_shas if idx.is_shallow else frozenset()counts: Counter[str] = Counter()for sha, _ts in idx.lines.values():+ if sha in excluded:+ continuename = idx.authors.get(sha, ("unknown", ""))[0]counts[name] += 1if not counts:@@ -117,10 +130,11 @@ def owner_in_range(return None, None, Nonefrom collections import Counter+ excluded = idx.boundary_shas if idx.is_shallow else frozenset()counts: Counter[str] = Counter()for ln in range(start_line, end_line + 1):entry = idx.lines.get(ln)- if entry is None:+ if entry is None or entry[0] in excluded:continuename = idx.authors.get(entry[0], ("unknown", ""))[0]counts[name] += 1@@ -138,7 +152,7 @@ def owner_in_range(def _parse_porcelain(raw: str,-) -> tuple[dict[int, tuple[str, int]], dict[str, tuple[str, str]]]:+) -> tuple[dict[int, tuple[str, int]], dict[str, tuple[str, str]], frozenset[str]]:"""Parse ``git blame --line-porcelain`` output into the line index.Porcelain format per line block::@@ -146,11 +160,17 @@ def _parse_porcelain(<sha> <orig-line> <final-line> [<num-lines>]author Nameauthor-time 1700000000+ boundary... other headers ...\t<source line>++ ``boundary`` marks the oldest commit git can see for that line — the true+ root on a full clone, the fetch cutoff on a shallow one. It is a plain+ keyword line with no value, emitted once per sha like the other headers."""out: dict[int, tuple[str, int]] = {}authors: dict[str, tuple[str, str]] = {}+ boundary_shas: set[str] = set()current_sha: str | None = Nonecurrent_final: int | None = Nonecurrent_author_time: int = 0@@ -197,6 +217,10 @@ def _parse_porcelain(if line.startswith("author-mail "):current_author_email = line[len("author-mail ") :].strip()continue+ if line == "boundary":+ if current_sha is not None:+ boundary_shas.add(current_sha)+ continue# Header lines are space-separated; only the very first header of a# block starts with a 40-char hex sha. Other headers start with# alphabetic keywords (author, committer, summary, previous, filename).@@ -210,7 +234,7 @@ def _parse_porcelain(except ValueError:current_final = Nonecurrent_author_time = sha_author_time.get(current_sha, 0)- return out, authors+ return out, authors, frozenset(boundary_shas)def build_blame_index(@@ -244,8 +268,21 @@ def build_blame_index(raw = _run_blame(repo, file_path, ignore_revs_args)if not raw:return BlameIndex()- lines, authors = _parse_porcelain(raw)- return BlameIndex(lines=lines, authors=authors)+ lines, authors, boundary_shas = _parse_porcelain(raw)+ shallow = False+ if boundary_shas and repo_path is not None:+ from repowise.core.git_refs import is_shallow++ shallow = is_shallow(str(repo_path))+ if shallow:+ logger.info(+ "blame_shallow_clone_boundary_excluded",+ path=file_path,+ boundary_commits=len(boundary_shas),+ )+ return BlameIndex(+ lines=lines, authors=authors, boundary_shas=boundary_shas, is_shallow=shallow+ )def _run_blame(repo: Any, file_path: str, ignore_revs_args: list[str]) -> str | None:diff --git a/tests/unit/health/test_function_blame.py b/tests/unit/health/test_function_blame.pyindex cedb823da..db37d6194 100644--- a/tests/unit/health/test_function_blame.py+++ b/tests/unit/health/test_function_blame.py@@ -41,7 +41,8 @@ _PORCELAIN = (def test_parse_porcelain_indexes_each_final_line():- lines, authors = _parse_porcelain(_PORCELAIN)+ lines, authors, boundary_shas = _parse_porcelain(_PORCELAIN)+ assert boundary_shas == frozenset()assert set(lines.keys()) == {1, 2, 3, 4}assert authors["a" * 40][0] == "Alice"assert authors["b" * 40][0] == "Bob"@@ -56,7 +57,7 @@ def test_parse_porcelain_indexes_each_final_line():def test_parse_porcelain_shares_one_entry_per_commit():# Lines from the same commit hold the same tuple, so the index costs one# entry per commit rather than one tuple and sha string per line.- lines, _ = _parse_porcelain(_PORCELAIN)+ lines, _, _ = _parse_porcelain(_PORCELAIN)assert lines[1] is lines[2]assert lines[1] is not lines[3]diff --git a/tests/unit/ingestion/test_shallow_clone_blame.py b/tests/unit/ingestion/test_shallow_clone_blame.pynew file mode 100644index 000000000..d80d43b16--- /dev/null+++ b/tests/unit/ingestion/test_shallow_clone_blame.py@@ -0,0 +1,157 @@+"""A shallow clone's boundary commit must not swallow earlier lines' authors (#3055).++``git blame`` marks the oldest commit it can see on a line's history as+``boundary``. On a full clone that is the true root commit; on a shallow+clone it is wherever the fetch was cut off, and every line written before the+cutoff reads as authored by whoever happens to own the boundary commit.+Ownership must exclude boundary lines on a shallow clone, and must NOT+exclude them on a full clone, where the boundary commit's authorship is real.+"""++from __future__ import annotations++import os+import subprocess+from pathlib import Path++from repowise.core.ingestion.git_indexer.file_history import index_file+from repowise.core.ingestion.git_indexer.function_blame import (+ BlameIndex,+ _parse_porcelain,+ owner_in_range,+ ownership_from_blame,+)++_PORCELAIN_WITH_BOUNDARY = (+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 1 1\n"+ "author Boundary\n"+ "author-time 1700000000\n"+ "boundary\n"+ "summary first\n"+ "filename foo.py\n"+ "\tdef alpha():\n"+ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 2 2 1\n"+ "author Carol\n"+ "author-time 1750000000\n"+ "summary second\n"+ "filename foo.py\n"+ "\t return 1\n"+)+++def test_parse_porcelain_captures_the_boundary_sha():+ lines, authors, boundary_shas = _parse_porcelain(_PORCELAIN_WITH_BOUNDARY)+ assert boundary_shas == {"a" * 40}+ assert set(lines.keys()) == {1, 2}+ assert authors["a" * 40][0] == "Boundary"+++def test_ownership_from_blame_excludes_boundary_lines_when_shallow():+ lines, authors, boundary_shas = _parse_porcelain(_PORCELAIN_WITH_BOUNDARY)+ idx = BlameIndex(lines=lines, authors=authors, boundary_shas=boundary_shas, is_shallow=True)+ name, _email, share = ownership_from_blame(idx)+ assert name == "Carol"+ assert share == 1.0+++def test_ownership_from_blame_keeps_boundary_lines_on_a_full_clone():+ """Same data, ``is_shallow=False``: the boundary commit is the true root,+ and its author is a real ownership signal, not an artifact to drop."""+ lines, authors, boundary_shas = _parse_porcelain(_PORCELAIN_WITH_BOUNDARY)+ idx = BlameIndex(lines=lines, authors=authors, boundary_shas=boundary_shas, is_shallow=False)+ name, _email, share = ownership_from_blame(idx)+ assert name in ("Boundary", "Carol") # tie, either is a valid top author+ assert share == 0.5+++def test_ownership_from_blame_falls_back_to_none_when_every_line_is_boundary():+ """All blamed lines are boundary shas on a shallow clone: no attributable+ signal, so the caller falls back to the commit-based owner."""+ lines, authors, boundary_shas = _parse_porcelain(_PORCELAIN_WITH_BOUNDARY)+ only_boundary = {k: v for k, v in lines.items() if v[0] == "a" * 40}+ idx = BlameIndex(+ lines=only_boundary, authors=authors, boundary_shas=boundary_shas, is_shallow=True+ )+ assert ownership_from_blame(idx) == (None, None, None)+++def test_owner_in_range_excludes_boundary_lines_when_shallow():+ lines, authors, boundary_shas = _parse_porcelain(_PORCELAIN_WITH_BOUNDARY)+ idx = BlameIndex(lines=lines, authors=authors, boundary_shas=boundary_shas, is_shallow=True)+ assert owner_in_range(idx, 1, 2) == ("Carol", None, 1.0)+++def _git(args: list[str], cwd: Path) -> str:+ return subprocess.run(+ ["git", *args], cwd=cwd, check=True, capture_output=True, text=True+ ).stdout.strip()+++def _commit(repo: Path, name: str, email: str, when: str, message: str) -> None:+ env = {+ **os.environ,+ "GIT_AUTHOR_NAME": name,+ "GIT_AUTHOR_EMAIL": email,+ "GIT_AUTHOR_DATE": when,+ "GIT_COMMITTER_NAME": name,+ "GIT_COMMITTER_EMAIL": email,+ "GIT_COMMITTER_DATE": when,+ }+ subprocess.run(+ ["git", "commit", "-q", "-m", message], cwd=repo, check=True, env=env+ )+++def test_shallow_clone_blame_ownership_matches_the_full_clone(tmp_path: Path) -> None:+ """The issue's own repro, shrunk: a shallow clone must not hand ownership+ of Alice's lines to Carol, the boundary commit's author.++ Depth 2 keeps only the last two commits (Carol's, then Bob's); Alice's+ three commits fall outside the fetch, so every line she wrote is blamed+ to Carol's commit, the new boundary, unless boundary lines are excluded.+ """+ origin = tmp_path / "origin"+ origin.mkdir()+ subprocess.run(["git", "init", "-q", "-b", "main"], cwd=origin, check=True)++ app = origin / "app.py"+ app.write_text("")+ for i in range(1, 4):+ with app.open("a") as f:+ for j in range(1, 11):+ f.write(f"def f_{i}_{j}(): return {i}{j}\n")+ _git(["add", "app.py"], origin)+ _commit(origin, "Alice", "[email protected]", f"2026-0{i}-01T12:00:00Z", f"feat: alice part {i}")++ with app.open("a") as f:+ f.write("def carol_helper(): return 'carol'\n")+ _git(["add", "app.py"], origin)+ _commit(origin, "Carol", "[email protected]", "2026-06-15T12:00:00Z", "feat: carol helper")++ with app.open("a") as f:+ f.write("def bob_helper(): return 'bob'\n")+ _git(["add", "app.py"], origin)+ _commit(origin, "Bob", "[email protected]", "2026-07-01T12:00:00Z", "feat: bob helper")++ full = tmp_path / "full"+ shallow = tmp_path / "shallow"+ subprocess.run(["git", "clone", "-q", str(origin), str(full)], check=True)+ subprocess.run(["git", "clone", "-q", "--depth", "2", str(origin), str(shallow)], check=True)++ import git as gitpython++ full_meta = index_file(+ gitpython.Repo(full), "app.py", repo_path=full, commit_limit=500,+ follow_renames=False, include_blame=True,+ )+ shallow_meta = index_file(+ gitpython.Repo(shallow), "app.py", repo_path=shallow, commit_limit=500,+ follow_renames=False, include_blame=True,+ )++ assert full_meta["primary_owner_name"] == "Alice"+ assert full_meta["primary_owner_line_pct"] > 0.9+ # Before this fix, the shallow clone named Carol the owner with a+ # near-100% line share (0.989 in the issue's own repro) — the boundary+ # commit's author credited with 90 lines she never wrote.+ assert shallow_meta["primary_owner_name"] != "Carol"