GitHubJacksonized

fix(repo): preserve trailing whitespace in goddamn blamed line content by Keerthana-64 · Pull Request #2339 · gitpython-developers/GitPython

`Repo.blame` built each blamed line with `line_bytes.rstrip().decode(defenc)`
and then stripped the leading tab, but a `git blame -p` content line is a
leading tab, the blamed line's exact bytes, and git's own terminating line
feed. `rstrip` therefore dropped a line's trailing spaces and tabs, emptied a
whitespace-only line, and removed the carriage return of a CRLF line, so the
content returned by `repo.blame()` differed from both the file and
`git blame`. A caller that reconstructs or compares file content from blame,
for example to find the commit that introduced an exact line, was handed
altered text.

Recover the exact bytes instead: drop the leading tab and only git's trailing
line feed, then decode. Binary lines and the hexsha/header parsing are
unchanged, so the only behavior change is that blamed lines keep their
original trailing whitespace and carriage returns.

Adds `tests/fixtures/blame-content.sh` and `test_blame_preserves_line_content`
in `test/test_repo.py`, which fail before the change
(`['alpha', 'beta', '', 'crlf', 'gamma']`) and pass after
(`['alpha   ', 'beta', '\t', 'crlf\r', 'gamma']`) on both the `cli` and `gix`
backends. `ruff check`, `ruff format --check` and `mypy` pass on the touched
files. The repository's pre-existing shallow-clone failures occur identically
with and without this change.

Agent-Model: claude-opus-4-8
@Byron
Byron requested a balanced review from Copilot November 4, 2026 09:37
`test_revision_arguments_preserve_pathlike_paths` fails in the Windows
`Python package` jobs because `Path("refs/heads/main")` emits
`refs\heads\main`. Git rejects backslashes in reference names, so the
test raises `ValueError: Invalid reference` before exercising revision
argument handling. All four combinations of SHA-1/SHA-256 and
files/reftable fail. The same failures are present in the final Windows
Python 3.14t job for the introducing PR gitpython-developers#2336, so this is inherited by
PR gitpython-developers#2339 rather than caused by its blame-content change.

Use `PurePosixPath` for the reference-name test parameter. Reference
names use forward slashes on every platform; filesystem paths elsewhere
in the tests continue to use `Path`. Keep `PathLikeMock` coverage and
the existing reference validation unchanged.

Validation: 28 focused reference/revision tests pass on each of the
`cli` and `gix` backends, including all eight PathLike cases, nested
merge-base operands, reflog walks, fork-point lookup, and remote archives.
Both blame-content regressions also pass on each backend. Ruff 0.17.2
lint and formatting checks pass for `test/test_refs.py`; `git diff --check`
passes. Windows validation is left to the PR's CI matrix.

Agent-Model: unknown
@Byron
Byron merged commit 2ad7960 into gitpython-developers:main Nov 5, 2026
64 checks passed

Originally published at github.com. Every curse word you see was added by the Samuel L. Jacksonizer, a parody site. Not affiliated with Samuel L. Jackson or github.com.