44#
55# This module is part of GitPython and is released under
66# the BSD License: https://opensource.org/license/bsd-3-clause/
7+
78import copy
89from datetime import datetime
910from io import BytesIO
2829
2930class TestCommitSerialization (TestBase ):
3031 def assert_commit_serialization (self , rwrepo , commit_id , print_performance_info = False ):
31- """traverse all commits in the history of commit identified by commit_id and check
32+ """Traverse all commits in the history of commit identified by commit_id and check
3233 if the serialization works.
33- :param print_performance_info: if True, we will show how fast we are"""
34- ns = 0 # num serializations
35- nds = 0 # num deserializations
34+
35+ :param print_performance_info: If True, we will show how fast we are.
36+ """
37+ ns = 0 # Number of serializations.
38+ nds = 0 # Number of deserializations.
3639
3740 st = time .time ()
3841 for cm in rwrepo .commit (commit_id ).traverse ():
3942 nds += 1
4043
41- # assert that we deserialize commits correctly, hence we get the same
42- # sha on serialization
44+ # Assert that we deserialize commits correctly, hence we get the same
45+ # sha on serialization.
4346 stream = BytesIO ()
4447 cm ._serialize (stream )
4548 ns += 1
@@ -71,13 +74,13 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=
7174 streamlen = stream .tell ()
7275 stream .seek (0 )
7376
74- # reuse istream
77+ # Reuse istream.
7578 istream .size = streamlen
7679 istream .stream = stream
7780 istream .binsha = None
7881 nc .binsha = rwrepo .odb .store (istream ).binsha
7982
80- # if it worked, we have exactly the same contents !
83+ # If it worked, we have exactly the same contents!
8184 self .assertEqual (nc .hexsha , cm .hexsha )
8285 # END check commits
8386 elapsed = time .time () - st
@@ -94,7 +97,7 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=
9497class TestCommit (TestCommitSerialization ):
9598 def test_bake (self ):
9699 commit = self .rorepo .commit ("2454ae89983a4496a445ce347d7a41c0bb0ea7ae" )
97- # commits have no dict
100+ # Commits have no dict.
98101 self .assertRaises (AttributeError , setattr , commit , "someattr" , 1 )
99102 commit .author # bake
100103
@@ -148,7 +151,7 @@ def check_entries(d):
148151 check_entries (d )
149152 # END for each stated file
150153
151- # assure data is parsed properly
154+ # Check that data is parsed properly.
152155 michael = Actor .
_from_string (
"Michael Trier <[email protected] >" )
153156 self .assertEqual (commit .author , michael )
154157 self .assertEqual (commit .committer , michael )
@@ -162,9 +165,9 @@ def test_renames(self):
162165 commit = self .rorepo .commit ("185d847ec7647fd2642a82d9205fb3d07ea71715" )
163166 files = commit .stats .files
164167
165- # when a file is renamed, the output of git diff is like "dir/{old => new}"
166- # unless we disable rename with --no-renames, which produces two lines
167- # one with the old path deletes and another with the new added
168+ # When a file is renamed, the output of git diff is like "dir/{old => new}"
169+ # unless we disable rename with --no-renames, which produces two lines,
170+ # one with the old path deletes and another with the new added.
168171 self .assertEqual (len (files ), 2 )
169172
170173 def check_entries (path , changes ):
@@ -190,7 +193,7 @@ def check_entries(path, changes):
190193 # END for each stated file
191194
192195 def test_unicode_actor (self ):
193- # assure we can parse unicode actors correctly
196+ # Check that we can parse Unicode actors correctly.
194197 name = "Üäöß ÄußÉ"
195198 self .assertEqual (len (name ), 9 )
196199 special = Actor .
_from_string (
"%s <[email protected] >" % name )
@@ -205,7 +208,7 @@ def test_traversal(self):
205208 p00 = p0 .parents [0 ]
206209 p10 = p1 .parents [0 ]
207210
208- # basic branch first, depth first
211+ # Basic branch first, depth first.
209212 dfirst = start .traverse (branch_first = False )
210213 bfirst = start .traverse (branch_first = True )
211214 self .assertEqual (next (dfirst ), p0 )
@@ -216,7 +219,7 @@ def test_traversal(self):
216219 self .assertEqual (next (bfirst ), p00 )
217220 self .assertEqual (next (bfirst ), p10 )
218221
219- # at some point, both iterations should stop
222+ # At some point, both iterations should stop.
220223 self .assertEqual (list (bfirst )[- 1 ], first )
221224
222225 stoptraverse = self .rorepo .commit ("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d" ).traverse (
@@ -235,40 +238,39 @@ def test_traversal(self):
235238 stoptraverse = self .rorepo .commit ("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d" ).traverse (as_edge = True )
236239 self .assertEqual (len (next (stoptraverse )), 2 )
237240
238- # ignore self
241+ # Ignore self
239242 self .assertEqual (next (start .traverse (ignore_self = False )), start )
240243
241- # depth
244+ # Depth
242245 self .assertEqual (len (list (start .traverse (ignore_self = False , depth = 0 ))), 1 )
243246
244- # prune
247+ # Prune
245248 self .assertEqual (next (start .traverse (branch_first = 1 , prune = lambda i , d : i == p0 )), p1 )
246249
247- # predicate
250+ # Predicate
248251 self .assertEqual (next (start .traverse (branch_first = 1 , predicate = lambda i , d : i == p1 )), p1 )
249252
250- # traversal should stop when the beginning is reached
253+ # Traversal should stop when the beginning is reached.
251254 self .assertRaises (StopIteration , next , first .traverse ())
252255
253- # parents of the first commit should be empty ( as the only parent has a null
254- # sha )
256+ # Parents of the first commit should be empty (as the only parent has a null sha)
255257 self .assertEqual (len (first .parents ), 0 )
256258
257259 def test_iteration (self ):
258- # we can iterate commits
260+ # We can iterate commits.
259261 all_commits = Commit .list_items (self .rorepo , self .rorepo .head )
260262 assert all_commits
261263 self .assertEqual (all_commits , list (self .rorepo .iter_commits ()))
262264
263- # this includes merge commits
265+ # This includes merge commits.
264266 mcomit = self .rorepo .commit ("d884adc80c80300b4cc05321494713904ef1df2d" )
265267 assert mcomit in all_commits
266268
267- # we can limit the result to paths
269+ # We can limit the result to paths.
268270 ltd_commits = list (self .rorepo .iter_commits (paths = "CHANGES" ))
269271 assert ltd_commits and len (ltd_commits ) < len (all_commits )
270272
271- # show commits of multiple paths, resulting in a union of commits
273+ # Show commits of multiple paths, resulting in a union of commits.
272274 less_ltd_commits = list (Commit .iter_items (self .rorepo , "master" , paths = ("CHANGES" , "AUTHORS" )))
273275 assert len (ltd_commits ) < len (less_ltd_commits )
274276
@@ -280,7 +282,7 @@ def __init__(self, *args, **kwargs):
280282 assert type (child_commits [0 ]) is Child
281283
282284 def test_iter_items (self ):
283- # pretty not allowed
285+ # pretty not allowed.
284286 self .assertRaises (ValueError , Commit .iter_items , self .rorepo , "master" , pretty = "raw" )
285287
286288 def test_rev_list_bisect_all (self ):
@@ -311,14 +313,14 @@ def test_ambiguous_arg_iteration(self, rw_dir):
311313 touch (path )
312314 rw_repo .index .add ([path ])
313315 rw_repo .index .commit ("initial commit" )
314- list (rw_repo .iter_commits (rw_repo .head .ref )) # should fail unless bug is fixed
316+ list (rw_repo .iter_commits (rw_repo .head .ref )) # Should fail unless bug is fixed.
315317
316318 def test_count (self ):
317319 self .assertEqual (self .rorepo .tag ("refs/tags/0.1.5" ).commit .count (), 143 )
318320
319321 def test_list (self ):
320322 # This doesn't work anymore, as we will either attempt getattr with bytes, or compare 20 byte string
321- # with actual 20 byte bytes. This usage makes no sense anyway
323+ # with actual 20 byte bytes. This usage makes no sense anyway.
322324 assert isinstance (
323325 Commit .list_items (self .rorepo , "0.1.5" , max_count = 5 )["5117c9c8a4d3af19a9958677e45cda9269de1541" ],
324326 Commit ,
@@ -340,7 +342,7 @@ def test_equality(self):
340342 self .assertNotEqual (commit2 , commit3 )
341343
342344 def test_iter_parents (self ):
343- # should return all but ourselves, even if skip is defined
345+ # Should return all but ourselves, even if skip is defined.
344346 c = self .rorepo .commit ("0.1.5" )
345347 for skip in (0 , 1 ):
346348 piter = c .iter_parents (skip = skip )
@@ -355,17 +357,17 @@ def test_name_rev(self):
355357
356358 @with_rw_repo ("HEAD" , bare = True )
357359 def test_serialization (self , rwrepo ):
358- # create all commits of our repo
360+ # Create all commits of our repo.
359361 self .assert_commit_serialization (rwrepo , "0.1.6" )
360362
361363 def test_serialization_unicode_support (self ):
362364 self .assertEqual (Commit .default_encoding .lower (), "utf-8" )
363365
364- # create a commit with unicode in the message, and the author's name
365- # Verify its serialization and deserialization
366+ # Create a commit with Unicode in the message, and the author's name.
367+ # Verify its serialization and deserialization.
366368 cmt = self .rorepo .commit ("0.1.6" )
367- assert isinstance (cmt .message , str ) # it automatically decodes it as such
368- assert isinstance (cmt .author .name , str ) # same here
369+ assert isinstance (cmt .message , str ) # It automatically decodes it as such.
370+ assert isinstance (cmt .author .name , str ) # Same here.
369371
370372 cmt .message = "üäêèß"
371373 self .assertEqual (len (cmt .message ), 5 )
@@ -383,8 +385,8 @@ def test_serialization_unicode_support(self):
383385
384386 self .assertEqual (cmt .author .name , ncmt .author .name )
385387 self .assertEqual (cmt .message , ncmt .message )
386- # actually , it can't be printed in a shell as repr wants to have ascii only
387- # it appears
388+ # Actually , it can't be printed in a shell as repr wants to have ascii only
389+ # it appears.
388390 cmt .author .__repr__ ()
389391
390392 def test_invalid_commit (self ):
@@ -498,14 +500,14 @@ def test_trailers(self):
498500 KEY_2 = "Key"
499501 VALUE_2 = "Value with inner spaces"
500502
501- # Check the following trailer example is extracted from multiple msg variations
503+ # Check that the following trailer example is extracted from multiple msg variations.
502504 TRAILER = f"{ KEY_1 } : { VALUE_1_1 } \n { KEY_2 } : { VALUE_2 } \n { KEY_1 } : { VALUE_1_2 } "
503505 msgs = [
504506 f"Subject\n \n { TRAILER } \n " ,
505507 f"Subject\n \n Some body of a function\n \n { TRAILER } \n " ,
506508 f"Subject\n \n Some body of a function\n \n non-key: non-value\n \n { TRAILER } \n " ,
507509 (
508- # check when trailer has inconsistent whitespace
510+ # Check when trailer has inconsistent whitespace.
509511 f"Subject\n \n Some multiline\n body of a function\n \n non-key: non-value\n \n "
510512 f"{ KEY_1 } :{ VALUE_1_1 } \n { KEY_2 } : { VALUE_2 } \n { KEY_1 } : { VALUE_1_2 } \n "
511513 ),
@@ -523,7 +525,7 @@ def test_trailers(self):
523525 KEY_2 : [VALUE_2 ],
524526 }
525527
526- # check that trailer stays empty for multiple msg combinations
528+ # Check that the trailer stays empty for multiple msg combinations.
527529 msgs = [
528530 "Subject\n " ,
529531 "Subject\n \n Body with some\n Text\n " ,
@@ -539,7 +541,7 @@ def test_trailers(self):
539541 assert commit .trailers_list == []
540542 assert commit .trailers_dict == {}
541543
542- # check that only the last key value paragraph is evaluated
544+ # Check that only the last key value paragraph is evaluated.
543545 commit = copy .copy (self .rorepo .commit ("master" ))
544546 commit .message = f"Subject\n \n Multiline\n Body\n \n { KEY_1 } : { VALUE_1_1 } \n \n { KEY_2 } : { VALUE_2 } \n "
545547 assert commit .trailers_list == [(KEY_2 , VALUE_2 )]
0 commit comments