@@ -74,7 +74,7 @@ def upstream(self):
74
74
75
75
@property
76
76
def sorted_branches (self ):
77
- """Return the branches to cherry-pick to, sorted by version"""
77
+ """Return the branches to cherry-pick to, sorted by version. """
78
78
return sorted (
79
79
self .branches ,
80
80
reverse = True ,
@@ -349,12 +349,15 @@ def continue_cherry_pick(self):
349
349
click .echo (f"Current branch ({ cherry_pick_branch } ) is not a backport branch. Will not continue. \U0001F61B " )
350
350
351
351
def check_repo (self ):
352
- # CPython repo has a commit with
353
- # SHA=7f777ed95a19224294949e1b4ce56bbffcb1fe9f
354
- cmd = ['git' , 'log' , '-r' , self .config ['check_sha' ]]
352
+ """
353
+ Check that the repository is for the project we're configured to operate on.
354
+
355
+ This function performs the check by making sure that the sha specified in the config
356
+ is present in the repository that we're operating on.
357
+ """
355
358
try :
356
- subprocess . check_output ( cmd , stderr = subprocess . STDOUT )
357
- except subprocess . SubprocessError :
359
+ validate_sha ( self . config [ 'check_sha' ] )
360
+ except ValueError :
358
361
raise InvalidRepoException ()
359
362
360
363
@@ -416,6 +419,9 @@ def cherry_pick_cli(dry_run, pr_remote, abort, status, push, config_path,
416
419
def get_base_branch (cherry_pick_branch ):
417
420
"""
418
421
return '2.7' from 'backport-sha-2.7'
422
+
423
+ raises ValueError if the specified branch name is not of a form that
424
+ cherry_picker would have created
419
425
"""
420
426
prefix , sha , base_branch = cherry_pick_branch .split ('-' , 2 )
421
427
@@ -425,19 +431,30 @@ def get_base_branch(cherry_pick_branch):
425
431
if not re .match ('[0-9a-f]{7,40}' , sha ):
426
432
raise ValueError (f'branch name has an invalid sha: { sha } ' )
427
433
428
- cmd = ['git' , 'log' , '-r' , sha ]
429
- try :
430
- subprocess .check_output (cmd , stderr = subprocess .STDOUT )
431
- except subprocess .SubprocessError :
432
- raise ValueError (f'The sha listed in the branch name, { sha } , is not present in the repository' )
434
+ # Validate that the sha refers to a valid commit within the repo
435
+ # Throws a ValueError if the sha is not present in the repo
436
+ validate_sha (sha )
433
437
434
438
# Subject the parsed base_branch to the same tests as when we generated it
435
- # This throws a ValueError if the base_branch doesn't need our requirements
439
+ # This throws a ValueError if the base_branch doesn't meet our requirements
436
440
version_from_branch (base_branch )
437
441
438
442
return base_branch
439
443
440
444
445
+ def validate_sha (sha ):
446
+ """
447
+ Validate that a hexdigest sha is a valid commit in the repo
448
+
449
+ raises ValueError if the sha does not reference a commit within the repo
450
+ """
451
+ cmd = ['git' , 'log' , '-r' , sha ]
452
+ try :
453
+ subprocess .check_output (cmd , stderr = subprocess .STDOUT )
454
+ except subprocess .SubprocessError :
455
+ raise ValueError (f'The sha listed in the branch name, { sha } , is not present in the repository' )
456
+
457
+
441
458
def version_from_branch (branch ):
442
459
"""
443
460
return version information from a git branch name
0 commit comments