4
4
#
5
5
# This module is part of GitPython and is released under
6
6
# the BSD License: https://opensource.org/license/bsd-3-clause/
7
+
7
8
import copy
8
9
from datetime import datetime
9
10
from io import BytesIO
28
29
29
30
class TestCommitSerialization (TestBase ):
30
31
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
32
33
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.
36
39
37
40
st = time .time ()
38
41
for cm in rwrepo .commit (commit_id ).traverse ():
39
42
nds += 1
40
43
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.
43
46
stream = BytesIO ()
44
47
cm ._serialize (stream )
45
48
ns += 1
@@ -71,13 +74,13 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=
71
74
streamlen = stream .tell ()
72
75
stream .seek (0 )
73
76
74
- # reuse istream
77
+ # Reuse istream.
75
78
istream .size = streamlen
76
79
istream .stream = stream
77
80
istream .binsha = None
78
81
nc .binsha = rwrepo .odb .store (istream ).binsha
79
82
80
- # if it worked, we have exactly the same contents !
83
+ # If it worked, we have exactly the same contents!
81
84
self .assertEqual (nc .hexsha , cm .hexsha )
82
85
# END check commits
83
86
elapsed = time .time () - st
@@ -94,7 +97,7 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=
94
97
class TestCommit (TestCommitSerialization ):
95
98
def test_bake (self ):
96
99
commit = self .rorepo .commit ("2454ae89983a4496a445ce347d7a41c0bb0ea7ae" )
97
- # commits have no dict
100
+ # Commits have no dict.
98
101
self .assertRaises (AttributeError , setattr , commit , "someattr" , 1 )
99
102
commit .author # bake
100
103
@@ -148,7 +151,7 @@ def check_entries(d):
148
151
check_entries (d )
149
152
# END for each stated file
150
153
151
- # assure data is parsed properly
154
+ # Check that data is parsed properly.
152
155
michael = Actor .
_from_string (
"Michael Trier <[email protected] >" )
153
156
self .assertEqual (commit .author , michael )
154
157
self .assertEqual (commit .committer , michael )
@@ -162,9 +165,9 @@ def test_renames(self):
162
165
commit = self .rorepo .commit ("185d847ec7647fd2642a82d9205fb3d07ea71715" )
163
166
files = commit .stats .files
164
167
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.
168
171
self .assertEqual (len (files ), 2 )
169
172
170
173
def check_entries (path , changes ):
@@ -190,7 +193,7 @@ def check_entries(path, changes):
190
193
# END for each stated file
191
194
192
195
def test_unicode_actor (self ):
193
- # assure we can parse unicode actors correctly
196
+ # Check that we can parse Unicode actors correctly.
194
197
name = "Üäöß ÄußÉ"
195
198
self .assertEqual (len (name ), 9 )
196
199
special = Actor .
_from_string (
"%s <[email protected] >" % name )
@@ -205,7 +208,7 @@ def test_traversal(self):
205
208
p00 = p0 .parents [0 ]
206
209
p10 = p1 .parents [0 ]
207
210
208
- # basic branch first, depth first
211
+ # Basic branch first, depth first.
209
212
dfirst = start .traverse (branch_first = False )
210
213
bfirst = start .traverse (branch_first = True )
211
214
self .assertEqual (next (dfirst ), p0 )
@@ -216,7 +219,7 @@ def test_traversal(self):
216
219
self .assertEqual (next (bfirst ), p00 )
217
220
self .assertEqual (next (bfirst ), p10 )
218
221
219
- # at some point, both iterations should stop
222
+ # At some point, both iterations should stop.
220
223
self .assertEqual (list (bfirst )[- 1 ], first )
221
224
222
225
stoptraverse = self .rorepo .commit ("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d" ).traverse (
@@ -235,40 +238,39 @@ def test_traversal(self):
235
238
stoptraverse = self .rorepo .commit ("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d" ).traverse (as_edge = True )
236
239
self .assertEqual (len (next (stoptraverse )), 2 )
237
240
238
- # ignore self
241
+ # Ignore self
239
242
self .assertEqual (next (start .traverse (ignore_self = False )), start )
240
243
241
- # depth
244
+ # Depth
242
245
self .assertEqual (len (list (start .traverse (ignore_self = False , depth = 0 ))), 1 )
243
246
244
- # prune
247
+ # Prune
245
248
self .assertEqual (next (start .traverse (branch_first = 1 , prune = lambda i , d : i == p0 )), p1 )
246
249
247
- # predicate
250
+ # Predicate
248
251
self .assertEqual (next (start .traverse (branch_first = 1 , predicate = lambda i , d : i == p1 )), p1 )
249
252
250
- # traversal should stop when the beginning is reached
253
+ # Traversal should stop when the beginning is reached.
251
254
self .assertRaises (StopIteration , next , first .traverse ())
252
255
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)
255
257
self .assertEqual (len (first .parents ), 0 )
256
258
257
259
def test_iteration (self ):
258
- # we can iterate commits
260
+ # We can iterate commits.
259
261
all_commits = Commit .list_items (self .rorepo , self .rorepo .head )
260
262
assert all_commits
261
263
self .assertEqual (all_commits , list (self .rorepo .iter_commits ()))
262
264
263
- # this includes merge commits
265
+ # This includes merge commits.
264
266
mcomit = self .rorepo .commit ("d884adc80c80300b4cc05321494713904ef1df2d" )
265
267
assert mcomit in all_commits
266
268
267
- # we can limit the result to paths
269
+ # We can limit the result to paths.
268
270
ltd_commits = list (self .rorepo .iter_commits (paths = "CHANGES" ))
269
271
assert ltd_commits and len (ltd_commits ) < len (all_commits )
270
272
271
- # show commits of multiple paths, resulting in a union of commits
273
+ # Show commits of multiple paths, resulting in a union of commits.
272
274
less_ltd_commits = list (Commit .iter_items (self .rorepo , "master" , paths = ("CHANGES" , "AUTHORS" )))
273
275
assert len (ltd_commits ) < len (less_ltd_commits )
274
276
@@ -280,7 +282,7 @@ def __init__(self, *args, **kwargs):
280
282
assert type (child_commits [0 ]) is Child
281
283
282
284
def test_iter_items (self ):
283
- # pretty not allowed
285
+ # pretty not allowed.
284
286
self .assertRaises (ValueError , Commit .iter_items , self .rorepo , "master" , pretty = "raw" )
285
287
286
288
def test_rev_list_bisect_all (self ):
@@ -311,14 +313,14 @@ def test_ambiguous_arg_iteration(self, rw_dir):
311
313
touch (path )
312
314
rw_repo .index .add ([path ])
313
315
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.
315
317
316
318
def test_count (self ):
317
319
self .assertEqual (self .rorepo .tag ("refs/tags/0.1.5" ).commit .count (), 143 )
318
320
319
321
def test_list (self ):
320
322
# 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.
322
324
assert isinstance (
323
325
Commit .list_items (self .rorepo , "0.1.5" , max_count = 5 )["5117c9c8a4d3af19a9958677e45cda9269de1541" ],
324
326
Commit ,
@@ -340,7 +342,7 @@ def test_equality(self):
340
342
self .assertNotEqual (commit2 , commit3 )
341
343
342
344
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.
344
346
c = self .rorepo .commit ("0.1.5" )
345
347
for skip in (0 , 1 ):
346
348
piter = c .iter_parents (skip = skip )
@@ -355,17 +357,17 @@ def test_name_rev(self):
355
357
356
358
@with_rw_repo ("HEAD" , bare = True )
357
359
def test_serialization (self , rwrepo ):
358
- # create all commits of our repo
360
+ # Create all commits of our repo.
359
361
self .assert_commit_serialization (rwrepo , "0.1.6" )
360
362
361
363
def test_serialization_unicode_support (self ):
362
364
self .assertEqual (Commit .default_encoding .lower (), "utf-8" )
363
365
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.
366
368
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.
369
371
370
372
cmt .message = "üäêèß"
371
373
self .assertEqual (len (cmt .message ), 5 )
@@ -383,8 +385,8 @@ def test_serialization_unicode_support(self):
383
385
384
386
self .assertEqual (cmt .author .name , ncmt .author .name )
385
387
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.
388
390
cmt .author .__repr__ ()
389
391
390
392
def test_invalid_commit (self ):
@@ -498,14 +500,14 @@ def test_trailers(self):
498
500
KEY_2 = "Key"
499
501
VALUE_2 = "Value with inner spaces"
500
502
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.
502
504
TRAILER = f"{ KEY_1 } : { VALUE_1_1 } \n { KEY_2 } : { VALUE_2 } \n { KEY_1 } : { VALUE_1_2 } "
503
505
msgs = [
504
506
f"Subject\n \n { TRAILER } \n " ,
505
507
f"Subject\n \n Some body of a function\n \n { TRAILER } \n " ,
506
508
f"Subject\n \n Some body of a function\n \n non-key: non-value\n \n { TRAILER } \n " ,
507
509
(
508
- # check when trailer has inconsistent whitespace
510
+ # Check when trailer has inconsistent whitespace.
509
511
f"Subject\n \n Some multiline\n body of a function\n \n non-key: non-value\n \n "
510
512
f"{ KEY_1 } :{ VALUE_1_1 } \n { KEY_2 } : { VALUE_2 } \n { KEY_1 } : { VALUE_1_2 } \n "
511
513
),
@@ -523,7 +525,7 @@ def test_trailers(self):
523
525
KEY_2 : [VALUE_2 ],
524
526
}
525
527
526
- # check that trailer stays empty for multiple msg combinations
528
+ # Check that the trailer stays empty for multiple msg combinations.
527
529
msgs = [
528
530
"Subject\n " ,
529
531
"Subject\n \n Body with some\n Text\n " ,
@@ -539,7 +541,7 @@ def test_trailers(self):
539
541
assert commit .trailers_list == []
540
542
assert commit .trailers_dict == {}
541
543
542
- # check that only the last key value paragraph is evaluated
544
+ # Check that only the last key value paragraph is evaluated.
543
545
commit = copy .copy (self .rorepo .commit ("master" ))
544
546
commit .message = f"Subject\n \n Multiline\n Body\n \n { KEY_1 } : { VALUE_1_1 } \n \n { KEY_2 } : { VALUE_2 } \n "
545
547
assert commit .trailers_list == [(KEY_2 , VALUE_2 )]
0 commit comments