Skip to content

Add Commit.patch property to expose commit-to-parent diff text#2118

Open
huyhoang171106 wants to merge 1 commit intogitpython-developers:mainfrom
huyhoang171106:feat/add-commit-patch-property-to-expose-comm
Open

Add Commit.patch property to expose commit-to-parent diff text#2118
huyhoang171106 wants to merge 1 commit intogitpython-developers:mainfrom
huyhoang171106:feat/add-commit-patch-property-to-expose-comm

Conversation

@huyhoang171106
Copy link

Summary

The issue is a missing API feature, not a runtime defect: Commit exposes structured diff stats (commit.stats) but does not expose the raw patch text for the same comparison. Users currently must manually call lower-level git commands for each commit. The Commit class should be extended with a patch property (or method) that returns unified diff text against its parent, with correct handling for merge/root commits.

Files changed

  • git/objects/commit.py (modified)

Testing

  • Not run in this environment.

Closes #1413

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 26, 2026 16:00
Copy link
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

Adds a Commit.patch property to GitPython’s Commit object to expose the unified diff text for a commit relative to its first parent (and handles root commits), addressing the API gap noted in #1413.

Changes:

  • Introduces Commit.patch as a new property returning textual patch output.
  • Uses git diff for typical commits and git diff-tree --root for root commits.

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

def patch(self) -> str:
"""Textual patch comparing this commit against its first parent."""
if not self.parents:
return self.repo.git.diff_tree(self.hexsha, root=True, p=True)
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

For root commits, git diff-tree -p includes a leading commit <sha> header line (as evidenced by stats slicing off the first line). Returning that raw output here makes Commit.patch inconsistent with the non-root case (which resembles git diff and has no commit header). Consider suppressing the commit header (e.g., --no-commit-id) or trimming the first line so patch is consistently unified-diff text.

Suggested change
return self.repo.git.diff_tree(self.hexsha, root=True, p=True)
return self.repo.git.diff_tree(self.hexsha, root=True, p=True, no_commit_id=True)

Copilot uses AI. Check for mistakes.
Comment on lines +199 to +204
@property
def patch(self) -> str:
"""Textual patch comparing this commit against its first parent."""
if not self.parents:
return self.repo.git.diff_tree(self.hexsha, root=True, p=True)
return self.repo.git.diff("%s..%s" % (self.parents[0].hexsha, self.hexsha), p=True)
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

This new Commit.patch API doesn’t appear to have test coverage. There are existing tests for commit.stats in test/test_commit.py; adding analogous assertions for commit.patch (including root commits and merge commits/first-parent behavior) would help prevent regressions and clarify the intended output format.

Copilot uses AI. Check for mistakes.
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.

Get patch text from Commit class like (commit.stats)

2 participants