@@ -74,12 +74,7 @@ def upstream(self):
74
74
75
75
@property
76
76
def sorted_branches (self ):
77
- def version_from_branch (branch ):
78
- try :
79
- return tuple (map (int , re .match (r'^.*(?P<version>\d+(\.\d+)+).*$' , branch ).groupdict ()['version' ].split ('.' )))
80
- except AttributeError as attr_err :
81
- raise ValueError (f'Branch { branch } seems to not have a version in its name.' ) from attr_err
82
-
77
+ """Return the branches to cherry-pick to, sorted by version."""
83
78
return sorted (
84
79
self .branches ,
85
80
reverse = True ,
@@ -354,12 +349,15 @@ def continue_cherry_pick(self):
354
349
click .echo (f"Current branch ({ cherry_pick_branch } ) is not a backport branch. Will not continue. \U0001F61B " )
355
350
356
351
def check_repo (self ):
357
- # CPython repo has a commit with
358
- # SHA=7f777ed95a19224294949e1b4ce56bbffcb1fe9f
359
- 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
+ """
360
358
try :
361
- subprocess . check_output ( cmd , stderr = subprocess . STDOUT )
362
- except subprocess . SubprocessError :
359
+ validate_sha ( self . config [ 'check_sha' ] )
360
+ except ValueError :
363
361
raise InvalidRepoException ()
364
362
365
363
@@ -421,11 +419,52 @@ def cherry_pick_cli(dry_run, pr_remote, abort, status, push, config_path,
421
419
def get_base_branch (cherry_pick_branch ):
422
420
"""
423
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
424
425
"""
425
426
prefix , sha , base_branch = cherry_pick_branch .split ('-' , 2 )
427
+
428
+ if prefix != 'backport' :
429
+ raise ValueError ('branch name is not prefixed with "backport-". Is this a cherry_picker branch?' )
430
+
431
+ if not re .match ('[0-9a-f]{7,40}' , sha ):
432
+ raise ValueError (f'branch name has an invalid sha: { sha } ' )
433
+
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 )
437
+
438
+ # Subject the parsed base_branch to the same tests as when we generated it
439
+ # This throws a ValueError if the base_branch doesn't meet our requirements
440
+ version_from_branch (base_branch )
441
+
426
442
return base_branch
427
443
428
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
+
458
+ def version_from_branch (branch ):
459
+ """
460
+ return version information from a git branch name
461
+ """
462
+ try :
463
+ return tuple (map (int , re .match (r'^.*(?P<version>\d+(\.\d+)+).*$' , branch ).groupdict ()['version' ].split ('.' )))
464
+ except AttributeError as attr_err :
465
+ raise ValueError (f'Branch { branch } seems to not have a version in its name.' ) from attr_err
466
+
467
+
429
468
def get_current_branch ():
430
469
"""
431
470
Return the current branch
0 commit comments