Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support version retrieval from custom branches #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ ENV/

# CPython checkout for cherry_picker.
cherry_picker/cpython

# pytest
.pytest_cache/
9 changes: 8 additions & 1 deletion cherry_picker/cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import subprocess
import webbrowser
import re
import sys
import requests
import toml
Expand Down Expand Up @@ -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<version>\d+(\.\d+)+).*$', branch).groupdict()['version'].split('.')))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe reading regex template from config is even better?

Copy link
Contributor Author

@webknjaz webknjaz May 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see how it's more flexible. I was thinking about this, but IRL regex is easy to screw up: they'll be exposed to the internals and helpless, and it would require them to actually scan through the code to figure out how it's being used, why and how to fix that.
It also looks like mixing up higher-level public API with implementation details, which IMHO should be separated.
So I'd be cautious when providing the end-users with such tooling.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe invite a syntax like stable-* where the star is version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be more complex than needed

except AttributeError as attr_err:
raise ValueError(f'Branch {branch} seems to not have a version in its name.') from attr_err

return sorted(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mariatta optionally we could try/except this as well and fall back to returning branches list in their original order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mariatta what do you think about falling back to unordered branch list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no need to support unordered branch list yet, I'm thinking to keep things as is and not change the behavior.

self.branches,
reverse=True,
key=lambda v: tuple(map(int, v.split('.'))))
key=version_from_branch)

@property
def username(self):
Expand Down
24 changes: 20 additions & 4 deletions cherry_picker/cherry_picker/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,29 @@ def test_get_author_info_from_short_sha(subprocess_check_output):
assert get_author_info_from_short_sha('22a594a') == 'Armin Rigo <[email protected]>'


@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


@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')
Expand Down
17 changes: 14 additions & 3 deletions cherry_picker/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ 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``).
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.
Expand Down Expand Up @@ -142,8 +149,12 @@ To customize the tool for used by other project:
by ``pip install cherry_picker``

5. Now everything is ready, use ``cherry_picker <commit_sha> <branch1>
<branch2>`` for cherry-picking changes from ``<commit_sha`` into
<branch2>`` for cherry-picking changes from ``<commit_sha>`` 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
----
Expand Down