From fb8b84c37f7c868c2fb95f768b6e3a5defc0c105 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 23 May 2018 17:04:13 +0200 Subject: [PATCH 1/6] Support version retrieval from custom branches Fixes #251 --- cherry_picker/cherry_picker/cherry_picker.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index ff046a4..a107ee1 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -7,6 +7,7 @@ import pathlib import subprocess import webbrowser +import re import sys import requests import toml @@ -72,10 +73,16 @@ def upstream(self): @property def sorted_branches(self): + def version_from_branch(branch): + try: + return tuple(map(int, re.match(r'^.*(?P\d+(\.\d+)+).*$', branch).groupdict()['version'].split('.'))) + except AttributeError as attr_err: + raise ValueError(f'Branch {branch} seems to not have a version in its name.') from attr_err + return sorted( self.branches, reverse=True, - key=lambda v: tuple(map(int, v.split('.')))) + key=version_from_branch) @property def username(self): From 115d6165bbc2f43878f764afb47d85d217736402 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 23 May 2018 17:15:07 +0200 Subject: [PATCH 2/6] Parametrize test for custom named branches --- cherry_picker/cherry_picker/test.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cherry_picker/cherry_picker/test.py b/cherry_picker/cherry_picker/test.py index 7ce83be..af9e357 100644 --- a/cherry_picker/cherry_picker/test.py +++ b/cherry_picker/cherry_picker/test.py @@ -64,13 +64,16 @@ def test_get_author_info_from_short_sha(subprocess_check_output): assert get_author_info_from_short_sha('22a594a') == 'Armin Rigo ' +@pytest.mark.parametrize('input_branches,sorted_branches', [ + (['3.1', '2.7', '3.10', '3.6'], ['3.10', '3.6', '3.1', '2.7']), + (['stable-3.1', 'lts-2.7', '3.10-other', 'smth3.6else'], ['3.10-other', 'smth3.6else', 'stable-3.1', 'lts-2.7']), +]) @mock.patch('os.path.exists') -def test_sorted_branch(os_path_exists, config): +def test_sorted_branch(os_path_exists, config, input_branches, sorted_branches): os_path_exists.return_value = True - branches = ["3.1", "2.7", "3.10", "3.6"] cp = CherryPicker('origin', '22a594a0047d7706537ff2ac676cdc0f1dcb329c', - branches, config=config) - assert cp.sorted_branches == ["3.10", "3.6", "3.1", "2.7"] + input_branches, config=config) + assert cp.sorted_branches == sorted_branches @mock.patch('os.path.exists') From f53858d6d7bdf536f03964beea39778e68a27927 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 23 May 2018 17:21:25 +0200 Subject: [PATCH 3/6] Add negative tests for branch sorter --- cherry_picker/cherry_picker/test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cherry_picker/cherry_picker/test.py b/cherry_picker/cherry_picker/test.py index af9e357..9be3498 100644 --- a/cherry_picker/cherry_picker/test.py +++ b/cherry_picker/cherry_picker/test.py @@ -76,6 +76,19 @@ def test_sorted_branch(os_path_exists, config, input_branches, sorted_branches): assert cp.sorted_branches == sorted_branches +@pytest.mark.parametrize('input_branches', [ + (['3.1', '2.7', '3.x10', '3.6', '']), + (['stable-3.1', 'lts-2.7', '3.10-other', 'smth3.6else', 'invalid']), +]) +@mock.patch('os.path.exists') +def test_invalid_branches(os_path_exists, config, input_branches): + os_path_exists.return_value = True + cp = CherryPicker('origin', '22a594a0047d7706537ff2ac676cdc0f1dcb329c', + input_branches, config=config) + with pytest.raises(ValueError): + cp.sorted_branches + + @mock.patch('os.path.exists') def test_get_cherry_pick_branch(os_path_exists, config): os_path_exists.return_value = True From ff86b9f99070a1a75a2f39727e4e51e9980f1241 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 23 May 2018 17:22:53 +0200 Subject: [PATCH 4/6] Ignore pytest's cache dir --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 2c22d75..eff40d7 100644 --- a/.gitignore +++ b/.gitignore @@ -90,3 +90,6 @@ ENV/ # CPython checkout for cherry_picker. cherry_picker/cpython + +# pytest +.pytest_cache/ From 32090619a052e6244b60bb0531a32f1f96207fd9 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 9 Jun 2018 11:08:42 +0200 Subject: [PATCH 5/6] Document new matching branch names --- cherry_picker/readme.rst | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cherry_picker/readme.rst b/cherry_picker/readme.rst index e0d4a38..fbffdbf 100644 --- a/cherry_picker/readme.rst +++ b/cherry_picker/readme.rst @@ -11,8 +11,14 @@ Usage (from a cloned CPython directory) :: About ===== -Use this to backport CPython changes from ``master`` into one or more of the -maintenance branches (``3.6``, ``3.5``, ``2.7``). +Use this to backport changes from the main branch of your project into one or +more of the maintenance branches (``3.6``, ``3.5``, ``2.7``, ``stable-2.6``, +``2.5-lts``). +The main requirement is that the target branches should contain some sort of +version number, which is at least two numbers separated by a dot. + +This tool is used to backport CPython changes from ``master`` into one or more +of the maintenance branches (``3.6``, ``3.5``, ``2.7``). It will prefix the commit message with the branch, e.g. ``[3.6]``, and then opens up the pull request page. @@ -142,8 +148,12 @@ To customize the tool for used by other project: by ``pip install cherry_picker`` 5. Now everything is ready, use ``cherry_picker - `` for cherry-picking changes from ```` for cherry-picking changes from ```` into maintenance branches. + Branch name should contain at least major and minor version numbers + and may have some prefix or suffix. + Only the first version-like substring is matched when the version + is extracted from branch name. Demo ---- From 1bac4b76a96ef91d7488a411c6d7f5463f2cdccf Mon Sep 17 00:00:00 2001 From: Mariatta Date: Sat, 9 Jun 2018 16:42:11 -0700 Subject: [PATCH 6/6] Update readme.rst --- cherry_picker/readme.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cherry_picker/readme.rst b/cherry_picker/readme.rst index fbffdbf..1e4f16f 100644 --- a/cherry_picker/readme.rst +++ b/cherry_picker/readme.rst @@ -11,15 +11,16 @@ Usage (from a cloned CPython directory) :: About ===== -Use this to backport changes from the main branch of your project into one or -more of the maintenance branches (``3.6``, ``3.5``, ``2.7``, ``stable-2.6``, -``2.5-lts``). -The main requirement is that the target branches should contain some sort of -version number, which is at least two numbers separated by a dot. - This tool is used to backport CPython changes from ``master`` into one or more of the maintenance branches (``3.6``, ``3.5``, ``2.7``). +``cherry_picker`` can be configured to backport other projects with similar +workflow as CPython. See the configuration file options below for more details. + +The maintenance branch names should contain some sort of version number (X.Y). +For example: ``3.6``, ``3.5``, ``2.7``, ``stable-2.6``, ``2.5-lts``, are all +supported branch names. + It will prefix the commit message with the branch, e.g. ``[3.6]``, and then opens up the pull request page.