Skip to content

Commit 7641ef9

Browse files
codexByron
authored andcommitted
fix(index): disable text conversion in empty-tree diffs
`IndexFile.diff()` compares the index to the empty tree through `git diff --cached`. Git enables text conversion by default in porcelain `git diff`, as documented in `Documentation/diff-options.adoc`. The direct empty-tree path lacked the flag used by `Diffable.diff`. Pass `--no-textconv` for raw and patch output so this path follows GitPython's existing diff safety behavior. The command-argument regression failed for both empty-tree spellings and both output modes before the fix. The index, diff and CLI safety suites passed with 187 tests and two skips on each backend. Agent-Model: unknown
1 parent 029964a commit 7641ef9

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎git/index/base.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ def diff(
16651665
self.repo._empty_tree_hexsha,
16661666
"--no-abbrev",
16671667
"--full-index",
1668+
"--no-textconv",
16681669
]
16691670

16701671
if not any(x in kwargs for x in ("find_renames", "no_renames", "M")):

‎test/test_cli_safety.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from gitdb import IStream
1010
from gitdb.db import LooseObjectDB
1111

12-
from git import Git, Repo
12+
from git import NULL_TREE, Git, Repo
13+
from git.diff import NULL_TREE_SHA
1314
from git.exc import GitCommandError, UnsafeOptionError, UnsupportedOperation
1415
from test.lib.fixtures import fixture_repo, writable_fixture
1516

@@ -118,6 +119,16 @@ def test_managed_commands_disable_implicit_execution(plain_repo):
118119
assert execute.call_args.kwargs["env"]["GIT_NO_LAZY_FETCH"] == "1"
119120

120121

122+
@pytest.mark.parametrize("other", [NULL_TREE, NULL_TREE_SHA])
123+
@pytest.mark.parametrize("create_patch", [False, True])
124+
def test_index_diff_against_empty_tree_disables_textconv(plain_repo, other, create_patch):
125+
with patch.object(Git, "_call_process_safe", autospec=True, side_effect=Git._call_process_safe) as run:
126+
assert plain_repo.index.diff(other, create_patch=create_patch) == []
127+
128+
assert run.call_args.args[1] == "diff"
129+
assert "--no-textconv" in run.call_args.args[2:]
130+
131+
121132
def test_blame_and_archive_do_not_run_configured_commands(repo):
122133
marker = Path(repo.working_tree_dir) / "executed"
123134
commit = repo.head.commit

0 commit comments

Comments
 (0)