adding patch property to Commit class by dduraipandian · Pull Request #1416 · gitpython-developers/GitPython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS
12 changes: 12 additions & 0 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ def stats(self) -> Stats:
text = self.repo.git.diff(self.parents[0].hexsha, self.hexsha, '--', numstat=True)
return Stats._list_from_string(self.repo, text)

@property
def patch(self) -> str:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It probably would be worth describing that this method can fail if the processed bytes aren't in the encoding that python expects (as it returns str and not bytes). Hence this method can throw an exception and from my experience it's more than likely that this happens to some.

This also means that at some point in the future and if there is demand one could provide a variant of this method that returns bytes instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for this note. I will check to see if i can return bytes than string and leave the choice to users to convert them to string. Is that okay?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That would be okay, yes. If it doesn't work with returning bytes right now then the possibility of decoding errors could be highlighted in the documentation specifically.

"""Get a git patch from changes between this commit and its first parent
or from all changes done if this is the very first commit.

:return: String"""
if not self.parents:
text = self.repo.git.diff(self.hexsha)
else:
text = self.repo.git.diff(self.parents[0].hexsha, self.hexsha, '--', numstat=False)
return text

@property
def trailers(self) -> Dict:
"""Get the trailers of the message as dictionary
Expand Down
75 changes: 75 additions & 0 deletions test/test_commit.py