Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ def __init__(
if gpgsig is not None:
self.gpgsig = gpgsig

@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)
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.
return self.repo.git.diff("%s..%s" % (self.parents[0].hexsha, self.hexsha), p=True)
Comment on lines +199 to +204
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.

@classmethod
def _get_intermediate_items(cls, commit: "Commit") -> Tuple["Commit", ...]:
return tuple(commit.parents)
Expand Down
Loading