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 ):
3031def 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
3740st = time .time ()
3841for cm in rwrepo .commit (commit_id ).traverse ():
3942nds += 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.
4346stream = BytesIO ()
4447cm ._serialize (stream )
4548ns += 1
@@ -71,13 +74,13 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=
7174streamlen = stream .tell ()
7275stream .seek (0 )
7376
74- #reuse istream
77+ #Reuse istream.
7578istream .size = streamlen
7679istream .stream = stream
7780istream .binsha = None
7881nc .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!
8184self .assertEqual (nc .hexsha ,cm .hexsha )
8285# END check commits
8386elapsed = time .time ()- st
@@ -94,7 +97,7 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=
9497class TestCommit (TestCommitSerialization ):
9598def test_bake (self ):
9699commit = self .rorepo .commit ("2454ae89983a4496a445ce347d7a41c0bb0ea7ae" )
97- #commits have no dict
100+ #Commits have no dict.
98101self .assertRaises (AttributeError ,setattr ,commit ,"someattr" ,1 )
99102commit .author # bake
100103
@@ -148,7 +151,7 @@ def check_entries(d):
148151check_entries (d )
149152# END for each stated file
150153
151- #assure data is parsed properly
154+ #Check that data is parsed properly.
152155michael = Actor ._from_string ("Michael Trier <mtrier@gmail.com>" )
153156self .assertEqual (commit .author ,michael )
154157self .assertEqual (commit .committer ,michael )
@@ -162,9 +165,9 @@ def test_renames(self):
162165commit = self .rorepo .commit ("185d847ec7647fd2642a82d9205fb3d07ea71715" )
163166files = 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.
168171self .assertEqual (len (files ),2 )
169172
170173def check_entries (path ,changes ):
@@ -190,7 +193,7 @@ def check_entries(path, changes):
190193# END for each stated file
191194
192195def test_unicode_actor (self ):
193- #assure we can parseunicode actors correctly
196+ #Check that we can parseUnicode actors correctly.
194197name = "Üäöß ÄußÉ"
195198self .assertEqual (len (name ),9 )
196199special = Actor ._from_string ("%s <something@this.com>" % name )
@@ -205,7 +208,7 @@ def test_traversal(self):
205208p00 = p0 .parents [0 ]
206209p10 = p1 .parents [0 ]
207210
208- #basic branch first, depth first
211+ #Basic branch first, depth first.
209212dfirst = start .traverse (branch_first = False )
210213bfirst = start .traverse (branch_first = True )
211214self .assertEqual (next (dfirst ),p0 )
@@ -216,7 +219,7 @@ def test_traversal(self):
216219self .assertEqual (next (bfirst ),p00 )
217220self .assertEqual (next (bfirst ),p10 )
218221
219- #at some point, both iterations should stop
222+ #At some point, both iterations should stop.
220223self .assertEqual (list (bfirst )[- 1 ],first )
221224
222225stoptraverse = self .rorepo .commit ("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d" ).traverse (
@@ -235,40 +238,39 @@ def test_traversal(self):
235238stoptraverse = self .rorepo .commit ("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d" ).traverse (as_edge = True )
236239self .assertEqual (len (next (stoptraverse )),2 )
237240
238- #ignore self
241+ #Ignore self
239242self .assertEqual (next (start .traverse (ignore_self = False )),start )
240243
241- #depth
244+ #Depth
242245self .assertEqual (len (list (start .traverse (ignore_self = False ,depth = 0 ))),1 )
243246
244- #prune
247+ #Prune
245248self .assertEqual (next (start .traverse (branch_first = 1 ,prune = lambda i ,d :i == p0 )),p1 )
246249
247- #predicate
250+ #Predicate
248251self .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.
251254self .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)
255257self .assertEqual (len (first .parents ),0 )
256258
257259def test_iteration (self ):
258- #we can iterate commits
260+ #We can iterate commits.
259261all_commits = Commit .list_items (self .rorepo ,self .rorepo .head )
260262assert all_commits
261263self .assertEqual (all_commits ,list (self .rorepo .iter_commits ()))
262264
263- #this includes merge commits
265+ #This includes merge commits.
264266mcomit = self .rorepo .commit ("d884adc80c80300b4cc05321494713904ef1df2d" )
265267assert mcomit in all_commits
266268
267- #we can limit the result to paths
269+ #We can limit the result to paths.
268270ltd_commits = list (self .rorepo .iter_commits (paths = "CHANGES" ))
269271assert 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.
272274less_ltd_commits = list (Commit .iter_items (self .rorepo ,"master" ,paths = ("CHANGES" ,"AUTHORS" )))
273275assert len (ltd_commits )< len (less_ltd_commits )
274276
@@ -280,7 +282,7 @@ def __init__(self, *args, **kwargs):
280282assert type (child_commits [0 ])is Child
281283
282284def test_iter_items (self ):
283- # pretty not allowed
285+ # pretty not allowed.
284286self .assertRaises (ValueError ,Commit .iter_items ,self .rorepo ,"master" ,pretty = "raw" )
285287
286288def test_rev_list_bisect_all (self ):
@@ -311,14 +313,14 @@ def test_ambiguous_arg_iteration(self, rw_dir):
311313touch (path )
312314rw_repo .index .add ([path ])
313315rw_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
316318def test_count (self ):
317319self .assertEqual (self .rorepo .tag ("refs/tags/0.1.5" ).commit .count (),143 )
318320
319321def 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.
322324assert isinstance (
323325Commit .list_items (self .rorepo ,"0.1.5" ,max_count = 5 )["5117c9c8a4d3af19a9958677e45cda9269de1541" ],
324326Commit ,
@@ -340,7 +342,7 @@ def test_equality(self):
340342self .assertNotEqual (commit2 ,commit3 )
341343
342344def 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.
344346c = self .rorepo .commit ("0.1.5" )
345347for skip in (0 ,1 ):
346348piter = c .iter_parents (skip = skip )
@@ -355,17 +357,17 @@ def test_name_rev(self):
355357
356358@with_rw_repo ("HEAD" ,bare = True )
357359def test_serialization (self ,rwrepo ):
358- #create all commits of our repo
360+ #Create all commits of our repo.
359361self .assert_commit_serialization (rwrepo ,"0.1.6" )
360362
361363def test_serialization_unicode_support (self ):
362364self .assertEqual (Commit .default_encoding .lower (),"utf-8" )
363365
364- #create a commit withunicode in the message, and the author's name
365- # Verify its serialization and deserialization
366+ #Create a commit withUnicode in the message, and the author's name.
367+ # Verify its serialization and deserialization.
366368cmt = 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
370372cmt .message = "üäêèß"
371373self .assertEqual (len (cmt .message ),5 )
@@ -383,8 +385,8 @@ def test_serialization_unicode_support(self):
383385
384386self .assertEqual (cmt .author .name ,ncmt .author .name )
385387self .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.
388390cmt .author .__repr__ ()
389391
390392def test_invalid_commit (self ):
@@ -498,14 +500,14 @@ def test_trailers(self):
498500KEY_2 = "Key"
499501VALUE_2 = "Value with inner spaces"
500502
501- # Check the following trailer example is extracted from multiple msg variations
503+ # Checkthat the following trailer example is extracted from multiple msg variations.
502504TRAILER = f"{ KEY_1 } :{ VALUE_1_1 } \n { KEY_2 } :{ VALUE_2 } \n { KEY_1 } :{ VALUE_1_2 } "
503505msgs = [
504506f"Subject\n \n { TRAILER } \n " ,
505507f"Subject\n \n Some body of a function\n \n { TRAILER } \n " ,
506508f"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.
509511f"Subject\n \n Some multiline\n body of a function\n \n non-key: non-value\n \n "
510512f"{ 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):
523525KEY_2 : [VALUE_2 ],
524526 }
525527
526- #check that trailer stays empty for multiple msg combinations
528+ #Check thatthe trailer stays empty for multiple msg combinations.
527529msgs = [
528530"Subject\n " ,
529531"Subject\n \n Body with some\n Text\n " ,
@@ -539,7 +541,7 @@ def test_trailers(self):
539541assert commit .trailers_list == []
540542assert 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.
543545commit = copy .copy (self .rorepo .commit ("master" ))
544546commit .message = f"Subject\n \n Multiline\n Body\n \n { KEY_1 } :{ VALUE_1_1 } \n \n { KEY_2 } :{ VALUE_2 } \n "
545547assert commit .trailers_list == [(KEY_2 ,VALUE_2 )]