Skip to content

Commit 4eba21f

Browse files
committed
Extract the validation of a sha within the repo to a common function
1 parent 113eb21 commit 4eba21f

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

cherry_picker/cherry_picker/cherry_picker.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def upstream(self):
7474

7575
@property
7676
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."""
7878
return sorted(
7979
self.branches,
8080
reverse=True,
@@ -349,12 +349,15 @@ def continue_cherry_pick(self):
349349
click.echo(f"Current branch ({cherry_pick_branch}) is not a backport branch. Will not continue. \U0001F61B")
350350

351351
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+
"""
355358
try:
356-
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
357-
except subprocess.SubprocessError:
359+
validate_sha(self.config['check_sha'])
360+
except ValueError:
358361
raise InvalidRepoException()
359362

360363

@@ -416,6 +419,9 @@ def cherry_pick_cli(dry_run, pr_remote, abort, status, push, config_path,
416419
def get_base_branch(cherry_pick_branch):
417420
"""
418421
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
419425
"""
420426
prefix, sha, base_branch = cherry_pick_branch.split('-', 2)
421427

@@ -425,19 +431,30 @@ def get_base_branch(cherry_pick_branch):
425431
if not re.match('[0-9a-f]{7,40}', sha):
426432
raise ValueError(f'branch name has an invalid sha: {sha}')
427433

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)
433437

434438
# 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
436440
version_from_branch(base_branch)
437441

438442
return base_branch
439443

440444

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+
441458
def version_from_branch(branch):
442459
"""
443460
return version information from a git branch name

0 commit comments

Comments
 (0)