Skip to content

Commit fe4b66d

Browse files
fix: use shared empty tree sha for index diffs
Assisted-by: ChatGPT
1 parent 3895682 commit fe4b66d

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

git/diff.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6-
__all__ = ["DiffConstants", "NULL_TREE", "INDEX", "Diffable", "DiffIndex", "Diff"]
6+
__all__ = ["DiffConstants", "NULL_TREE", "NULL_TREE_SHA", "INDEX", "Diffable", "DiffIndex", "Diff"]
77

88
import enum
99
import re
@@ -84,6 +84,9 @@ class DiffConstants(enum.Enum):
8484
:const:`git.NULL_TREE` and :const:`Diffable.NULL_TREE`.
8585
"""
8686

87+
NULL_TREE_SHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
88+
"""SHA of Git's canonical empty tree object."""
89+
8790
INDEX: Literal[DiffConstants.INDEX] = DiffConstants.INDEX
8891
"""Stand-in indicating you want to diff against the index.
8992

git/index/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,10 @@ def diff(
15111511
if other is self.INDEX:
15121512
return git_diff.DiffIndex()
15131513

1514-
if other is git_diff.NULL_TREE:
1514+
if other == git_diff.NULL_TREE or other == git_diff.NULL_TREE_SHA:
15151515
args: List[Union[PathLike, str]] = [
15161516
"--cached",
1517-
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
1517+
git_diff.NULL_TREE_SHA,
15181518
"--abbrev=40",
15191519
"--full-index",
15201520
]

test/test_index.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import pytest
2525

2626
from git import BlobFilter, Diff, Git, IndexFile, NULL_TREE, Object, Repo, Tree
27+
from git.diff import NULL_TREE_SHA
2728
from git.exc import (
2829
CheckoutError,
2930
GitCommandError,
@@ -568,7 +569,7 @@ def test_index_file_diff_null_tree_with_initial_index(self, rw_dir):
568569
index.write()
569570

570571
index = IndexFile(repo)
571-
assert not index.diff(None)
572+
self.assertEqual(len(index.diff(None)), 0)
572573

573574
diff = index.diff(NULL_TREE)
574575
self.assertEqual(len(diff), 1)
@@ -577,6 +578,7 @@ def test_index_file_diff_null_tree_with_initial_index(self, rw_dir):
577578
self.assertEqual(diff[0].b_path, filename)
578579

579580
self.assertEqual(len(index.diff(NULL_TREE, paths=filename)), 1)
581+
self.assertEqual(len(index.diff(NULL_TREE_SHA, paths=filename)), 1)
580582
self.assertEqual(len(index.diff(NULL_TREE, paths="missing")), 0)
581583

582584
patch = index.diff(NULL_TREE, create_patch=True)

0 commit comments

Comments
 (0)