From 16193ebeac19e08d30e7f254f4f64fb7496993c8 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 23 May 2018 17:48:17 +0200 Subject: [PATCH 01/10] Add support for default branch detection --- cherry_picker/cherry_picker/cherry_picker.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index ff046a4..11b7da7 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -104,6 +104,7 @@ def run_cmd(self, cmd): return output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) click.echo(output.decode('utf-8')) + return output def checkout_branch(self, branch_name): """ git checkout -b """ @@ -128,9 +129,12 @@ def get_commit_message(self, commit_sha): else: return message - def checkout_master(self): - """ git checkout master """ - cmd = ['git', 'checkout', 'master'] + def checkout_default_branch(self): + """ git checkout default branch """ + cmd = ["sh", "-c", "git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'"] + default_branch = self.run_cmd(cmd).strip().decode() or 'master' + + cmd = ['git', 'checkout', default_branch] self.run_cmd(cmd) def status(self): @@ -249,7 +253,7 @@ def delete_branch(self, branch): self.run_cmd(cmd) def cleanup_branch(self, branch): - self.checkout_master() + self.checkout_default_branch() try: self.delete_branch(branch) except subprocess.CalledProcessError: From ddfcb55b3633f7ac62516e916d899d95418516da Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Mon, 28 May 2018 14:37:26 +0200 Subject: [PATCH 02/10] Memorize initial branch and restore it upon finish Based on suggestions of @serhiy-storchaka @terryjreedy in #250: Co-authored-by: Terry Jan Reedy Co-authored-by: Serhiy Storchaka --- cherry_picker/cherry_picker/cherry_picker.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index 11b7da7..bae9248 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -51,6 +51,8 @@ def __init__(self, pr_remote, commit_sha1, branches, if dry_run: click.echo("Dry run requested, listing expected command sequence") + self.remember_current_branch() + self.pr_remote = pr_remote self.commit_sha1 = commit_sha1 self.branches = branches @@ -131,10 +133,8 @@ def get_commit_message(self, commit_sha): def checkout_default_branch(self): """ git checkout default branch """ - cmd = ["sh", "-c", "git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'"] - default_branch = self.run_cmd(cmd).strip().decode() or 'master' - cmd = ['git', 'checkout', default_branch] + cmd = 'git', 'checkout', self._start_branch self.run_cmd(cmd) def status(self): @@ -357,6 +357,9 @@ def check_repo(self): except subprocess.SubprocessError: raise InvalidRepoException() + def remember_current_branch(self): + self._start_branch = get_current_branch() or 'master' + CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) From f456d38cb9b7b507a5d616fb4fec5b5c4f4242bf Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 29 May 2018 13:23:35 +0200 Subject: [PATCH 03/10] Inline branch detection into initializer --- cherry_picker/cherry_picker/cherry_picker.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index bae9248..dbaa652 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -51,7 +51,7 @@ def __init__(self, pr_remote, commit_sha1, branches, if dry_run: click.echo("Dry run requested, listing expected command sequence") - self.remember_current_branch() + self._start_branch = get_current_branch() or 'master' self.pr_remote = pr_remote self.commit_sha1 = commit_sha1 @@ -357,9 +357,6 @@ def check_repo(self): except subprocess.SubprocessError: raise InvalidRepoException() - def remember_current_branch(self): - self._start_branch = get_current_branch() or 'master' - CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) From a1156b5b855e4b5ef70b53edab453d588fede83f Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 29 May 2018 13:24:34 +0200 Subject: [PATCH 04/10] Unprotect memorized branch var --- cherry_picker/cherry_picker/cherry_picker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index dbaa652..d30d190 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -51,7 +51,7 @@ def __init__(self, pr_remote, commit_sha1, branches, if dry_run: click.echo("Dry run requested, listing expected command sequence") - self._start_branch = get_current_branch() or 'master' + self.start_branch = get_current_branch() or 'master' self.pr_remote = pr_remote self.commit_sha1 = commit_sha1 @@ -134,7 +134,7 @@ def get_commit_message(self, commit_sha): def checkout_default_branch(self): """ git checkout default branch """ - cmd = 'git', 'checkout', self._start_branch + cmd = 'git', 'checkout', self.start_branch self.run_cmd(cmd) def status(self): From ea9b1c28234696f7fa8fad9034e4a2ac3a107996 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 29 May 2018 13:25:10 +0200 Subject: [PATCH 05/10] Rename memorized branch attribute --- cherry_picker/cherry_picker/cherry_picker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index d30d190..f62d106 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -51,7 +51,7 @@ def __init__(self, pr_remote, commit_sha1, branches, if dry_run: click.echo("Dry run requested, listing expected command sequence") - self.start_branch = get_current_branch() or 'master' + self.memorized_branch = get_current_branch() or 'master' self.pr_remote = pr_remote self.commit_sha1 = commit_sha1 @@ -134,7 +134,7 @@ def get_commit_message(self, commit_sha): def checkout_default_branch(self): """ git checkout default branch """ - cmd = 'git', 'checkout', self.start_branch + cmd = 'git', 'checkout', self.memorized_branch self.run_cmd(cmd) def status(self): From dcc9f20ab1d13cabb7cf126a63e260bb87096be4 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 29 May 2018 13:26:58 +0200 Subject: [PATCH 06/10] Don't return anything from run_cmd method --- cherry_picker/cherry_picker/cherry_picker.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index f62d106..5dd961e 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -106,7 +106,6 @@ def run_cmd(self, cmd): return output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) click.echo(output.decode('utf-8')) - return output def checkout_branch(self, branch_name): """ git checkout -b """ From 5a7efe8dcc738ed4b30cb183252cf62e1fd222d6 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 29 May 2018 13:28:33 +0200 Subject: [PATCH 07/10] Rename checking out to initial branch --- cherry_picker/cherry_picker/cherry_picker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index 5dd961e..c86ebd1 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -130,7 +130,7 @@ def get_commit_message(self, commit_sha): else: return message - def checkout_default_branch(self): + def checkout_memorized_branch(self): """ git checkout default branch """ cmd = 'git', 'checkout', self.memorized_branch @@ -252,7 +252,7 @@ def delete_branch(self, branch): self.run_cmd(cmd) def cleanup_branch(self, branch): - self.checkout_default_branch() + self.checkout_memorized_branch() try: self.delete_branch(branch) except subprocess.CalledProcessError: From dbeef678fff3109a4f230eaef87e6c226b5ab47f Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 9 Jun 2018 11:21:32 +0200 Subject: [PATCH 08/10] =?UTF-8?q?=F0=9F=8E=A8=20Make=20config=20store=20de?= =?UTF-8?q?fault=20branch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per @Mariatta's advice --- cherry_picker/cherry_picker/cherry_picker.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index c86ebd1..f41aca5 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -21,7 +21,8 @@ 'team': 'python', 'repo': 'cpython', 'check_sha': '7f777ed95a19224294949e1b4ce56bbffcb1fe9f', - 'fix_commit_msg': True + 'fix_commit_msg': True, + 'default_branch': 'master', }) @@ -51,8 +52,6 @@ def __init__(self, pr_remote, commit_sha1, branches, if dry_run: click.echo("Dry run requested, listing expected command sequence") - self.memorized_branch = get_current_branch() or 'master' - self.pr_remote = pr_remote self.commit_sha1 = commit_sha1 self.branches = branches @@ -130,10 +129,10 @@ def get_commit_message(self, commit_sha): else: return message - def checkout_memorized_branch(self): + def checkout_default_branch(self): """ git checkout default branch """ - cmd = 'git', 'checkout', self.memorized_branch + cmd = 'git', 'checkout', self.config['default_branch'] self.run_cmd(cmd) def status(self): @@ -252,7 +251,7 @@ def delete_branch(self, branch): self.run_cmd(cmd) def cleanup_branch(self, branch): - self.checkout_memorized_branch() + self.checkout_default_branch() try: self.delete_branch(branch) except subprocess.CalledProcessError: From b2d9fdb992b4b4b1f6cc9855d1686de37fabb608 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 9 Jun 2018 11:26:10 +0200 Subject: [PATCH 09/10] =?UTF-8?q?=F0=9F=93=9D=20Document=20default=5Fbranc?= =?UTF-8?q?h=20in=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cherry_picker/readme.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cherry_picker/readme.rst b/cherry_picker/readme.rst index e0d4a38..2e834af 100644 --- a/cherry_picker/readme.rst +++ b/cherry_picker/readme.rst @@ -103,6 +103,7 @@ Configuration file example:: repo = "aiohttp" check_sha = "f382b5ffc445e45a110734f5396728da7914aeb6" fix_commit_msg = false + default_branch = "devel" Available config options:: @@ -126,14 +127,18 @@ Available config options:: on pull-request xxxx. For projects using GitHub Issues, this option can be disabled. + repo Project's default branch name, + e.g "devel" for https://github.com/ansible/ansible + ("master" by default) + To customize the tool for used by other project: 1. Create a file called ``.cherry_picker.toml`` in the project's root folder (alongside with ``.git`` folder). -2. Add ``team``, ``repo``, ``fix_commit_msg`` and ``check_sha`` - config values as described above. +2. Add ``team``, ``repo``, ``fix_commit_msg``, ``check_sha`` and + ``default_branch`` config values as described above. 3. Use ``git add .cherry_picker.toml`` / ``git commit`` to add the config into ``git``. From 443dbdb5a458f5f66deb9141e8ffee64b22d0223 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 9 Jun 2018 11:38:22 +0200 Subject: [PATCH 10/10] =?UTF-8?q?=E2=9C=85=F0=9F=90=9B=20Add=20test=20for?= =?UTF-8?q?=20default=5Fbranch=20config=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And fix test name shaddowing --- cherry_picker/cherry_picker/test.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cherry_picker/cherry_picker/test.py b/cherry_picker/cherry_picker/test.py index 7ce83be..64293e8 100644 --- a/cherry_picker/cherry_picker/test.py +++ b/cherry_picker/cherry_picker/test.py @@ -178,7 +178,7 @@ def test_find_config_not_found(tmpdir, cd): assert find_config() is None -def test_load_config(tmpdir, cd): +def test_load_full_config(tmpdir, cd): cd(tmpdir) subprocess.run('git init .'.split(), check=True) cfg = tmpdir.join('.cherry_picker.toml') @@ -186,14 +186,18 @@ def test_load_config(tmpdir, cd): team = "python" repo = "core-workfolow" check_sha = "5f007046b5d4766f971272a0cc99f8461215c1ec" + default_branch = "devel" ''') cfg = load_config(None) assert cfg == {'check_sha': '5f007046b5d4766f971272a0cc99f8461215c1ec', 'repo': 'core-workfolow', - 'team': 'python'} + 'team': 'python', + 'fix_commit_msg': True, + 'default_branch': 'devel', + } -def test_load_config(tmpdir, cd): +def test_load_partial_config(tmpdir, cd): cfg = tmpdir.join('.cherry_picker.toml') cfg.write('''\ repo = "core-workfolow" @@ -202,7 +206,9 @@ def test_load_config(tmpdir, cd): assert cfg == {'check_sha': '7f777ed95a19224294949e1b4ce56bbffcb1fe9f', 'repo': 'core-workfolow', 'team': 'python', - 'fix_commit_msg': True} + 'fix_commit_msg': True, + 'default_branch': 'master', + } def test_normalize_long_commit_message():