Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(ingestion): honor .git-blame-ignore-revs in blame ownership (#3078)

Aman7cf5493b4f8b
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.py
index cc6924920..66dc413d5 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
@@ -46,6 +46,10 @@ __all__ = [
# per-function mod counts cannot exceed file mod counts, and noise dominates.
_MIN_COMMITS_FOR_BLAME = 5
+# GitHub's and git's own convention: a repo-root file listing bulk/formatting
+# commits whose lines blame should skip past to the real prior author (#3056).
+_BLAME_IGNORE_REVS_FILENAME = ".git-blame-ignore-revs"
+
@dataclass
class BlameIndex:
@@ -236,22 +240,61 @@ def build_blame_index(
return BlameIndex()
except OSError:
return BlameIndex()
+ ignore_revs_args = _blame_ignore_revs_args(repo, repo_path)
+ 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)
+
+
+def _run_blame(repo: Any, file_path: str, ignore_revs_args: list[str]) -> str | None:
+ """Run ``git blame --line-porcelain``, retrying once without
+ *ignore_revs_args* on failure.
+
+ A malformed ``.git-blame-ignore-revs`` line that is not a valid object
+ name makes git exit 128 (#3056); retrying once without the flag lets a
+ bad ignore-revs file degrade to ordinary blame instead of losing the
+ signal entirely. Returns ``None`` (never raises) on any subprocess
+ error, same as every other ``repo.git`` call site in this module.
+ """
try:
# ``repo.git.blame`` invokes the git binary directly (shell=False).
# ``--line-porcelain`` repeats headers on every line which makes the
# parser robust to out-of-order line emission from --incremental.
- raw = repo.git.blame("--line-porcelain", "HEAD", "--", file_path)
+ return repo.git.blame("--line-porcelain", *ignore_revs_args, "HEAD", "--", file_path)
except Exception as exc:
- logger.debug(
- "blame_index_failed",
- path=file_path,
- error=str(exc),
- )
- return BlameIndex()
- if not raw:
- return BlameIndex()
- lines, authors = _parse_porcelain(raw)
- return BlameIndex(lines=lines, authors=authors)
+ if not ignore_revs_args:
+ logger.debug("blame_index_failed", path=file_path, error=str(exc))
+ return None
+ try:
+ return repo.git.blame("--line-porcelain", "HEAD", "--", file_path)
+ except Exception as retry_exc:
+ logger.debug("blame_index_failed", path=file_path, error=str(retry_exc))
+ return None
+
+
+def _blame_ignore_revs_args(repo: Any, repo_path: Path | None) -> list[str]:
+ """``["--ignore-revs-file", path]`` when a ``.git-blame-ignore-revs`` file
+ exists at the repo root and nothing already configures one, else ``[]``.
+
+ A repo whose ``blame.ignoreRevsFile`` is already set in git config applies
+ it automatically on every blame call; adding our own flag on top of that
+ would be redundant, not wrong, but checking first avoids the extra
+ subprocess work being pointless.
+ """
+ if repo_path is None:
+ return []
+ ignore_file = repo_path / _BLAME_IGNORE_REVS_FILENAME
+ if not ignore_file.is_file():
+ return []
+ try:
+ configured = repo.git.config("--get", "blame.ignoreRevsFile")
+ except Exception:
+ configured = ""
+ if configured.strip():
+ return []
+ return ["--ignore-revs-file", str(ignore_file)]
def distinct_commits_in_range(idx: BlameIndex, start_line: int, end_line: int) -> set[str]:
diff --git a/tests/unit/health/test_function_blame.py b/tests/unit/health/test_function_blame.py
index 6854dd78c..cedb823da 100644
--- a/tests/unit/health/test_function_blame.py
+++ b/tests/unit/health/test_function_blame.py
@@ -7,6 +7,7 @@ from types import SimpleNamespace
from repowise.core.ingestion.git_indexer.function_blame import (
BlameIndex,
+ _blame_ignore_revs_args,
_parse_porcelain,
build_blame_index,
distinct_commits_in_range,
@@ -147,3 +148,71 @@ def test_build_blame_index_happy_path(tmp_path: Path):
commit_count_total=50,
)
assert set(idx.lines.keys()) == {1, 2, 3, 4}
+
+
+def test_blame_ignore_revs_args_empty_when_file_absent(tmp_path: Path):
+ repo = SimpleNamespace(git=SimpleNamespace(config=lambda *a, **kw: ""))
+ assert _blame_ignore_revs_args(repo, tmp_path) == []
+
+
+def test_blame_ignore_revs_args_empty_without_repo_path():
+ repo = SimpleNamespace(git=SimpleNamespace(config=lambda *a, **kw: ""))
+ assert _blame_ignore_revs_args(repo, None) == []
+
+
+def test_blame_ignore_revs_args_added_when_file_present_and_unconfigured(tmp_path: Path):
+ (tmp_path / ".git-blame-ignore-revs").write_text("abc123\n", encoding="utf-8")
+
+ def _raise_unset(*_a, **_kw):
+ raise RuntimeError("key not found")
+
+ repo = SimpleNamespace(git=SimpleNamespace(config=_raise_unset))
+ args = _blame_ignore_revs_args(repo, tmp_path)
+ assert args == ["--ignore-revs-file", str(tmp_path / ".git-blame-ignore-revs")]
+
+
+def test_blame_ignore_revs_args_empty_when_already_configured(tmp_path: Path):
+ (tmp_path / ".git-blame-ignore-revs").write_text("abc123\n", encoding="utf-8")
+ repo = SimpleNamespace(git=SimpleNamespace(config=lambda *a, **kw: "some/other/path\n"))
+ assert _blame_ignore_revs_args(repo, tmp_path) == []
+
+
+def test_build_blame_index_passes_the_ignore_revs_flag(tmp_path: Path):
+ (tmp_path / "foo.py").write_text("def alpha():\n pass\n", encoding="utf-8")
+ (tmp_path / ".git-blame-ignore-revs").write_text("abc123\n", encoding="utf-8")
+ calls: list[tuple] = []
+
+ def _blame(*a, **kw):
+ calls.append(a)
+ return _PORCELAIN
+
+ def _raise_unset(*_a, **_kw):
+ raise RuntimeError("key not found")
+
+ repo = SimpleNamespace(git=SimpleNamespace(blame=_blame, config=_raise_unset))
+ idx = build_blame_index(repo, "foo.py", repo_path=tmp_path, commit_count_total=50)
+ assert idx.lines
+ assert "--ignore-revs-file" in calls[0]
+
+
+def test_build_blame_index_retries_without_the_flag_on_a_malformed_ignore_revs_file(
+ tmp_path: Path,
+):
+ (tmp_path / "foo.py").write_text("def alpha():\n pass\n", encoding="utf-8")
+ (tmp_path / ".git-blame-ignore-revs").write_text("not-a-valid-sha\n", encoding="utf-8")
+ calls: list[tuple] = []
+
+ def _blame(*a, **kw):
+ calls.append(a)
+ if "--ignore-revs-file" in a:
+ raise RuntimeError("fatal: could not resolve 'not-a-valid-sha'")
+ return _PORCELAIN
+
+ def _raise_unset(*_a, **_kw):
+ raise RuntimeError("key not found")
+
+ repo = SimpleNamespace(git=SimpleNamespace(blame=_blame, config=_raise_unset))
+ idx = build_blame_index(repo, "foo.py", repo_path=tmp_path, commit_count_total=50)
+ # Degrades to ordinary blame rather than losing the signal entirely.
+ assert idx.lines
+ assert len(calls) == 2