Skip to content

Commit c1e26f9

Browse files
webknjazMariatta
authored andcommitted
Support version retrieval from custom branches (GH-253)
Support other versioning schemes such as something-X.Y, X.Y-somethingelse.
1 parent 6f434b1 commit c1e26f9

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,6 @@ ENV/
9090

9191
# CPython checkout for cherry_picker.
9292
cherry_picker/cpython
93+
94+
# pytest
95+
.pytest_cache/

cherry_picker/cherry_picker/cherry_picker.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pathlib
88
import subprocess
99
import webbrowser
10+
import re
1011
import sys
1112
import requests
1213
import toml
@@ -72,10 +73,16 @@ def upstream(self):
7273

7374
@property
7475
def sorted_branches(self):
76+
def version_from_branch(branch):
77+
try:
78+
return tuple(map(int, re.match(r'^.*(?P<version>\d+(\.\d+)+).*$', branch).groupdict()['version'].split('.')))
79+
except AttributeError as attr_err:
80+
raise ValueError(f'Branch {branch} seems to not have a version in its name.') from attr_err
81+
7582
return sorted(
7683
self.branches,
7784
reverse=True,
78-
key=lambda v: tuple(map(int, v.split('.'))))
85+
key=version_from_branch)
7986

8087
@property
8188
def username(self):

cherry_picker/cherry_picker/test.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,29 @@ def test_get_author_info_from_short_sha(subprocess_check_output):
6464
assert get_author_info_from_short_sha('22a594a') == 'Armin Rigo <[email protected]>'
6565

6666

67+
@pytest.mark.parametrize('input_branches,sorted_branches', [
68+
(['3.1', '2.7', '3.10', '3.6'], ['3.10', '3.6', '3.1', '2.7']),
69+
(['stable-3.1', 'lts-2.7', '3.10-other', 'smth3.6else'], ['3.10-other', 'smth3.6else', 'stable-3.1', 'lts-2.7']),
70+
])
6771
@mock.patch('os.path.exists')
68-
def test_sorted_branch(os_path_exists, config):
72+
def test_sorted_branch(os_path_exists, config, input_branches, sorted_branches):
6973
os_path_exists.return_value = True
70-
branches = ["3.1", "2.7", "3.10", "3.6"]
7174
cp = CherryPicker('origin', '22a594a0047d7706537ff2ac676cdc0f1dcb329c',
72-
branches, config=config)
73-
assert cp.sorted_branches == ["3.10", "3.6", "3.1", "2.7"]
75+
input_branches, config=config)
76+
assert cp.sorted_branches == sorted_branches
77+
78+
79+
@pytest.mark.parametrize('input_branches', [
80+
(['3.1', '2.7', '3.x10', '3.6', '']),
81+
(['stable-3.1', 'lts-2.7', '3.10-other', 'smth3.6else', 'invalid']),
82+
])
83+
@mock.patch('os.path.exists')
84+
def test_invalid_branches(os_path_exists, config, input_branches):
85+
os_path_exists.return_value = True
86+
cp = CherryPicker('origin', '22a594a0047d7706537ff2ac676cdc0f1dcb329c',
87+
input_branches, config=config)
88+
with pytest.raises(ValueError):
89+
cp.sorted_branches
7490

7591

7692
@mock.patch('os.path.exists')

cherry_picker/readme.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ Usage (from a cloned CPython directory) ::
1111
About
1212
=====
1313

14-
Use this to backport CPython changes from ``master`` into one or more of the
15-
maintenance branches (``3.6``, ``3.5``, ``2.7``).
14+
This tool is used to backport CPython changes from ``master`` into one or more
15+
of the maintenance branches (``3.6``, ``3.5``, ``2.7``).
16+
17+
``cherry_picker`` can be configured to backport other projects with similar
18+
workflow as CPython. See the configuration file options below for more details.
19+
20+
The maintenance branch names should contain some sort of version number (X.Y).
21+
For example: ``3.6``, ``3.5``, ``2.7``, ``stable-2.6``, ``2.5-lts``, are all
22+
supported branch names.
1623

1724
It will prefix the commit message with the branch, e.g. ``[3.6]``, and then
1825
opens up the pull request page.
@@ -142,8 +149,12 @@ To customize the tool for used by other project:
142149
by ``pip install cherry_picker``
143150

144151
5. Now everything is ready, use ``cherry_picker <commit_sha> <branch1>
145-
<branch2>`` for cherry-picking changes from ``<commit_sha`` into
152+
<branch2>`` for cherry-picking changes from ``<commit_sha>`` into
146153
maintenance branches.
154+
Branch name should contain at least major and minor version numbers
155+
and may have some prefix or suffix.
156+
Only the first version-like substring is matched when the version
157+
is extracted from branch name.
147158

148159
Demo
149160
----

0 commit comments

Comments
 (0)