Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c6d102

Browse files
DuraiDurai
authored andcommittedFeb 26, 2022
adding patch property to Commit class
1 parent c074057 commit 3c6d102

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
 

‎AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Contributors are:
4545
-Alba Mendez <me _at_ alba.sh>
4646
-Robert Westman <robert _at_ byteflux.io>
4747
-Hugo van Kemenade
48+
-Durai Pandian <dduraipandian _at_ gmail.com>
4849
Portions derived from other open source works and are clearly marked.

‎git/objects/commit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ def stats(self) -> Stats:
315315
text = self.repo.git.diff(self.parents[0].hexsha, self.hexsha, '--', numstat=True)
316316
return Stats._list_from_string(self.repo, text)
317317

318+
@property
319+
def patch(self) -> str:
320+
"""Get a git patch from changes between this commit and its first parent
321+
or from all changes done if this is the very first commit.
322+
323+
:return: String"""
324+
if not self.parents:
325+
text = self.repo.git.diff(self.hexsha)
326+
else:
327+
text = self.repo.git.diff(self.parents[0].hexsha, self.hexsha, '--', numstat=False)
328+
return text
329+
318330
@property
319331
def trailers(self) -> Dict:
320332
"""Get the trailers of the message as dictionary

‎test/test_commit.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,81 @@ def check_entries(d):
149149
self.assertEqual(commit.committer_tz_offset, 14400, commit.committer_tz_offset)
150150
self.assertEqual(commit.message, "initial project\n")
151151

152+
def test_patch_with_parent(self):
153+
expected_path_result = """diff --git a/doc/source/tutorial.rst b/doc/source/tutorial.rst
154+
index bc386e7c..fcbc18bf 100644
155+
--- a/doc/source/tutorial.rst
156+
+++ b/doc/source/tutorial.rst
157+
@@ -66,7 +66,7 @@ Archive the repository contents to a tar file.
158+
Advanced Repo Usage
159+
===================
160+
161+
-And of course, there is much more you can do with this type, most of the following will be explained in greater detail in specific tutorials. Don't worry if you don't understand some of these examples right away, as they may require a thorough understanding of gits inner workings.
162+
+And of course, there is much more you can do with this type, most of the following will be explained in greater detail in specific tutorials. Don't worry if you don't understand some of these examples right away, as they may require a thorough understanding of git's inner workings.
163+
164+
Query relevant repository paths ...
165+
166+
@@ -363,7 +363,7 @@ Handling Remotes
167+
:start-after: # [25-test_references_and_objects]
168+
:end-before: # ![25-test_references_and_objects]
169+
170+
-You can easily access configuration information for a remote by accessing options as if they where attributes. The modification of remote configuration is more explicit though.
171+
+You can easily access configuration information for a remote by accessing options as if they were attributes. The modification of remote configuration is more explicit though.
172+
173+
.. literalinclude:: ../../test/test_docs.py
174+
:language: python
175+
@@ -391,7 +391,7 @@ Here's an example executable that can be used in place of the `ssh_executable` a
176+
ID_RSA=/var/lib/openshift/5562b947ecdd5ce939000038/app-deployments/id_rsa
177+
exec /usr/bin/ssh -o StrictHostKeyChecking=no -i $ID_RSA "$@"
178+
179+
-Please note that the script must be executable (i.e. `chomd +x script.sh`). `StrictHostKeyChecking=no` is used to avoid prompts asking to save the hosts key to `~/.ssh/known_hosts`, which happens in case you run this as daemon.
180+
+Please note that the script must be executable (i.e. `chmod +x script.sh`). `StrictHostKeyChecking=no` is used to avoid prompts asking to save the hosts key to `~/.ssh/known_hosts`, which happens in case you run this as daemon.
181+
182+
You might also have a look at `Git.update_environment(...)` in case you want to setup a changed environment more permanently.
183+
184+
@@ -509,14 +509,14 @@ The type of the database determines certain performance characteristics, such as
185+
186+
GitDB
187+
=====
188+
-The GitDB is a pure-python implementation of the git object database. It is the default database to use in GitPython 0.3. Its uses less memory when handling huge files, but will be 2 to 5 times slower when extracting large quantities small of objects from densely packed repositories::
189+
+The GitDB is a pure-python implementation of the git object database. It is the default database to use in GitPython 0.3. It uses less memory when handling huge files, but will be 2 to 5 times slower when extracting large quantities of small objects from densely packed repositories::
190+
191+
repo = Repo("path/to/repo", odbt=GitDB)
192+
193+
194+
GitCmdObjectDB
195+
==============
196+
-The git command database uses persistent git-cat-file instances to read repository information. These operate very fast under all conditions, but will consume additional memory for the process itself. When extracting large files, memory usage will be much higher than the one of the ``GitDB``::
197+
+The git command database uses persistent git-cat-file instances to read repository information. These operate very fast under all conditions, but will consume additional memory for the process itself. When extracting large files, memory usage will be much higher than ``GitDB``::
198+
199+
repo = Repo("path/to/repo", odbt=GitCmdObjectDB)
200+
"""
201+
commit = self.rorepo.commit('c0740570b31f0f0fe499bf4fc5abbf89feb1757d')
202+
patch = commit.patch
203+
self.assertEqual(patch[:200], expected_path_result[:200])
204+
self.assertEqual(patch[200:400], expected_path_result[200:400])
205+
self.assertEqual(patch[400:600], expected_path_result[400:600])
206+
207+
def test_patch_with_no_parent(self):
208+
expected_path_result = """diff --git a/.flake8 b/.flake8
209+
new file mode 100644
210+
index 00000000..c55fe35d
211+
--- /dev/null
212+
+++ b/.flake8
213+
@@ -0,0 +1,35 @@
214+
+[flake8]
215+
+show-source = True
216+
+count= True
217+
+statistics = True
218+
+# E265 = comment blocks like @{ section, which it can't handle
219+
+# E266 = too many leading '#' for block comment
220+
+# E731 = do not assign a lambda expression, use a def
221+
+# W293 = Blank line contains whitespace
222+
+# W504"""
223+
commit = self.rorepo.commit('33ebe7acec14b25c5f84f35a664803fcab2f7781')
224+
patch = commit.patch
225+
self.assertEqual(patch[:200], expected_path_result[:200])
226+
152227
def test_unicode_actor(self):
153228
# assure we can parse unicode actors correctly
154229
name = "Üäöß ÄußÉ"

0 commit comments

Comments
 (0)
Please sign in to comment.