Skip to content

Commit b42b8a9

Browse files
committed
Validate a branch that we parse when running cherry_picker --continue
When running cherry_picker --continue we count on being able to get certain information from the branch's name which cherry_picker constructed earlier. Verify as best we can that the branch name is one which cherry_picker could have constructed. Relies on the changes here: python#265 (which does the work of one of the validations)
1 parent f90d993 commit b42b8a9

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

cherry_picker/cherry_picker/cherry_picker.py

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

7575
@property
7676
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"""
8378
return sorted(
8479
self.branches,
8580
reverse=True,
@@ -423,9 +418,28 @@ def get_base_branch(cherry_pick_branch):
423418
return '2.7' from 'backport-sha-2.7'
424419
"""
425420
prefix, sha, base_branch = cherry_pick_branch.split('-', 2)
421+
422+
if prefix != 'backport':
423+
raise ValueError('branch name is not prefixed with "backport-". Is this a cherry_picker branch?')
424+
if not re.match('[0-9a-f]{7,40}', sha):
425+
raise ValueError('branch name has an invalid sha')
426+
# Subject the parsed base_branch to the same tests as when we generated it
427+
# This throws a ValueError if the base_branch doesn't need our requirements
428+
version_from_branch(base_branch)
429+
426430
return base_branch
427431

428432

433+
def version_from_branch(branch):
434+
"""
435+
return version information from a git branch name
436+
"""
437+
try:
438+
return tuple(map(int, re.match(r'^.*(?P<version>\d+(\.\d+)+).*$', branch).groupdict()['version'].split('.')))
439+
except AttributeError as attr_err:
440+
raise ValueError(f'Branch {branch} seems to not have a version in its name.') from attr_err
441+
442+
429443
def get_current_branch():
430444
"""
431445
Return the current branch

cherry_picker/cherry_picker/test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,29 @@ def changedir(d):
3333

3434

3535
def test_get_base_branch():
36-
# The format of cherry-pick branches we create are "backport-{SHA}-{base_branch}"
36+
# The format of cherry-pick branches we create are::
37+
# backport-{SHA}-{base_branch}
3738
cherry_pick_branch = 'backport-afc23f4-2.7'
3839
result = get_base_branch(cherry_pick_branch)
3940
assert result == '2.7'
4041

4142

4243
def test_get_base_branch_which_has_dashes():
43-
cherry_pick_branch ='backport-afc23f4-baseprefix-2.7-basesuffix'
44+
cherry_pick_branch = 'backport-afc23f4-baseprefix-2.7-basesuffix'
4445
result = get_base_branch(cherry_pick_branch)
4546
assert result == 'baseprefix-2.7-basesuffix'
4647

4748

49+
@pytest.mark.parametrize('cherry_pick_branch', ['backport-afc23f4', # Not enough fields
50+
'prefix-afc23f4-2.7', # Not the prefix we were expecting
51+
'backport-afc23f4-base', # No version info in the base branch
52+
]
53+
)
54+
def test_get_base_branch_invalid(cherry_pick_branch):
55+
with pytest.raises(ValueError):
56+
get_base_branch(cherry_pick_branch)
57+
58+
4859
@mock.patch('subprocess.check_output')
4960
def test_get_current_branch(subprocess_check_output):
5061
subprocess_check_output.return_value = b'master'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy