Support index diffs against the empty tree#2155
Open
puneetdixit200 wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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 handleNULL_TREEas theotherdiff 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.
| if other is self.INDEX: | ||
| return git_diff.DiffIndex() | ||
|
|
||
| if other is git_diff.NULL_TREE: |
Comment on lines
+1515
to
+1520
| args: List[Union[PathLike, str]] = [ | ||
| "--cached", | ||
| "4b825dc642cb6eb9a060e54bf8d69288fbee4904", | ||
| "--abbrev=40", | ||
| "--full-index", | ||
| ] |
| ) | ||
| index = diff_method(self.repo, proc) | ||
|
|
||
| proc.wait() |
| index.write() | ||
|
|
||
| index = IndexFile(repo) | ||
| assert not index.diff(None) |
Assisted-by: OpenAI GPT-5
4508774 to
3895682
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related to #2025.
IndexFile.diff()is documented as accepting the same targets asDiffable.diff(), includingNULL_TREE, but the index-specific implementation rejectedNULL_TREEbefore 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 usinggit diff --cachedagainst the empty tree object. I keptIndexFile.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 thatindex.diff(NULL_TREE)reports the staged addition whileindex.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.pyAI assistance disclosure: OpenAI GPT-5 helped draft and check this change; I reviewed the implementation and tests before submitting.