Skip to content

Support index diffs against the empty tree#2155

Open
puneetdixit200 wants to merge 1 commit into
gitpython-developers:mainfrom
puneetdixit200:fix-initial-index-diff
Open

Support index diffs against the empty tree#2155
puneetdixit200 wants to merge 1 commit into
gitpython-developers:mainfrom
puneetdixit200:fix-initial-index-diff

Conversation

@puneetdixit200
Copy link
Copy Markdown

Related to #2025.

IndexFile.diff() is documented as accepting the same targets as Diffable.diff(), including NULL_TREE, but the index-specific implementation rejected NULL_TREE before it could produce a diff. That leaves no direct API path for inspecting staged entries for an initial commit against the empty tree.

This adds explicit IndexFile.diff(NULL_TREE) handling using git diff --cached against the empty tree object. I kept IndexFile.diff(None) as the existing index-vs-working-tree comparison, since that behavior is documented in the quick docs and existing tests.

The regression test creates a new repository, stages a file, writes the index, constructs a fresh IndexFile, and verifies that index.diff(NULL_TREE) reports the staged addition while index.diff(None) remains empty because the working tree matches the index.

Validation:

  • .venv/bin/python -m pytest --no-cov test/test_index.py::TestIndex::test_index_file_diff_null_tree_with_initial_index -q
  • .venv/bin/python -m ruff check git/index/base.py test/test_index.py

AI assistance disclosure: OpenAI GPT-5 helped draft and check this change; I reviewed the implementation and tests before submitting.

Copilot AI review requested due to automatic review settings May 21, 2026 07:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds IndexFile.diff(NULL_TREE) support so callers can diff the index against an “empty tree” (useful for initial index contents), and introduces a regression test to validate expected diff output.

Changes:

  • Extends IndexFile.diff() to accept and handle NULL_TREE as the other diff target.
  • Implements a custom git diff --cached <empty-tree> invocation to produce raw/patch output for the NULL_TREE case.
  • Adds a test covering initial index diffing against NULL_TREE, including path filtering and patch creation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
test/test_index.py Adds coverage for diffing an initial index against NULL_TREE and verifying raw + patch output.
git/index/base.py Implements IndexFile.diff(NULL_TREE) by invoking git diff --cached against the empty-tree SHA.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread git/index/base.py
if other is self.INDEX:
return git_diff.DiffIndex()

if other is git_diff.NULL_TREE:
Comment thread git/index/base.py
Comment on lines +1515 to +1520
args: List[Union[PathLike, str]] = [
"--cached",
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
"--abbrev=40",
"--full-index",
]
Comment thread git/index/base.py
)
index = diff_method(self.repo, proc)

proc.wait()
Comment thread test/test_index.py
index.write()

index = IndexFile(repo)
assert not index.diff(None)
Assisted-by: OpenAI GPT-5
@puneetdixit200 puneetdixit200 force-pushed the fix-initial-index-diff branch from 4508774 to 3895682 Compare May 21, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants