diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index eb2fdf2..1a689c4 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -74,7 +74,7 @@ def upstream(self): @property def sorted_branches(self): - """Return the branches to cherry-pick to, sorted by version""" + """Return the branches to cherry-pick to, sorted by version.""" return sorted( self.branches, reverse=True, @@ -349,12 +349,15 @@ def continue_cherry_pick(self): click.echo(f"Current branch ({cherry_pick_branch}) is not a backport branch. Will not continue. \U0001F61B") def check_repo(self): - # CPython repo has a commit with - # SHA=7f777ed95a19224294949e1b4ce56bbffcb1fe9f - cmd = ['git', 'log', '-r', self.config['check_sha']] + """ + Check that the repository is for the project we're configured to operate on. + + This function performs the check by making sure that the sha specified in the config + is present in the repository that we're operating on. + """ try: - subprocess.check_output(cmd, stderr=subprocess.STDOUT) - except subprocess.SubprocessError: + validate_sha(self.config['check_sha']) + except ValueError: raise InvalidRepoException() @@ -416,6 +419,9 @@ def cherry_pick_cli(dry_run, pr_remote, abort, status, push, config_path, def get_base_branch(cherry_pick_branch): """ return '2.7' from 'backport-sha-2.7' + + raises ValueError if the specified branch name is not of a form that + cherry_picker would have created """ prefix, sha, base_branch = cherry_pick_branch.split('-', 2) @@ -425,19 +431,30 @@ def get_base_branch(cherry_pick_branch): if not re.match('[0-9a-f]{7,40}', sha): raise ValueError(f'branch name has an invalid sha: {sha}') - cmd = ['git', 'log', '-r', sha] - try: - subprocess.check_output(cmd, stderr=subprocess.STDOUT) - except subprocess.SubprocessError: - raise ValueError(f'The sha listed in the branch name, {sha}, is not present in the repository') + # Validate that the sha refers to a valid commit within the repo + # Throws a ValueError if the sha is not present in the repo + validate_sha(sha) # Subject the parsed base_branch to the same tests as when we generated it - # This throws a ValueError if the base_branch doesn't need our requirements + # This throws a ValueError if the base_branch doesn't meet our requirements version_from_branch(base_branch) return base_branch +def validate_sha(sha): + """ + Validate that a hexdigest sha is a valid commit in the repo + + raises ValueError if the sha does not reference a commit within the repo + """ + cmd = ['git', 'log', '-r', sha] + try: + subprocess.check_output(cmd, stderr=subprocess.STDOUT) + except subprocess.SubprocessError: + raise ValueError(f'The sha listed in the branch name, {sha}, is not present in the repository') + + def version_from_branch(branch): """ return version information from a git branch name