From b935bf1f05bb1f0a8d86eb771e7410985912352f Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Mon, 29 Jan 2018 17:03:21 -0800 Subject: [PATCH 1/2] Append Co-authored-by: in the commit message. --- cherry_picker/cherry_picker/cherry_picker.py | 18 ++++++++++++-- cherry_picker/cherry_picker/test.py | 26 ++++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/cherry_picker/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker/cherry_picker.py index 432b41b..91166fe 100755 --- a/cherry_picker/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker/cherry_picker.py @@ -149,7 +149,10 @@ def amend_commit_message(self, cherry_pick_branch): commit_prefix = "" if self.prefix_commit: commit_prefix = f"[{get_base_branch(cherry_pick_branch)}] " - updated_commit_message = f"{commit_prefix}{self.get_commit_message(self.commit_sha1)}{os.linesep}(cherry picked from commit {self.commit_sha1})" + updated_commit_message = f"""{commit_prefix}{self.get_commit_message(self.commit_sha1)} +(cherry picked from commit {self.commit_sha1}) + +Co-authored-by: {get_author_info_from_short_sha(self.commit_sha1)}""" updated_commit_message = updated_commit_message.replace('#', 'GH-') if self.dry_run: click.echo(f" dry-run: git commit --amend -m '{updated_commit_message}'") @@ -295,7 +298,11 @@ def continue_cherry_pick(self): short_sha = cherry_pick_branch[cherry_pick_branch.index('-')+1:cherry_pick_branch.index(base)-1] full_sha = get_full_sha_from_short(short_sha) commit_message = self.get_commit_message(short_sha) - updated_commit_message = f'[{base}] {commit_message}. \n(cherry picked from commit {full_sha})' + co_author_info = f"Co-authored-by: {get_author_info_from_short_sha(short_sha)}" + updated_commit_message = f"""[{base}] {commit_message}. +(cherry picked from commit {full_sha}) + +{co_author_info}""" if self.dry_run: click.echo(f" dry-run: git commit -am '{updated_commit_message}' --allow-empty") else: @@ -389,6 +396,13 @@ def get_full_sha_from_short(short_sha): return full_sha +def get_author_info_from_short_sha(short_sha): + cmd = f"git log -1 --format='%aN <%ae>' {short_sha}" + output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) + author = output.strip().decode('utf-8') + return author + + def is_cpython_repo(): cmd = "git log -r 7f777ed95a19224294949e1b4ce56bbffcb1fe9f" try: diff --git a/cherry_picker/cherry_picker/test.py b/cherry_picker/cherry_picker/test.py index 6c50b4f..f9447a0 100644 --- a/cherry_picker/cherry_picker/test.py +++ b/cherry_picker/cherry_picker/test.py @@ -3,7 +3,8 @@ import pytest from .cherry_picker import get_base_branch, get_current_branch, \ - get_full_sha_from_short, is_cpython_repo, CherryPicker, \ + get_full_sha_from_short, get_author_info_from_short_sha, \ + is_cpython_repo, CherryPicker, \ normalize_commit_message @@ -44,6 +45,13 @@ def test_get_full_sha_from_short(subprocess_check_output): assert get_full_sha_from_short('22a594a') == '22a594a0047d7706537ff2ac676cdc0f1dcb329c' +@mock.patch('subprocess.check_output') +def test_get_author_info_from_short_sha(subprocess_check_output): + mock_output = b"Armin Rigo " + subprocess_check_output.return_value = mock_output + assert get_author_info_from_short_sha('22a594a') == 'Armin Rigo ' + + @mock.patch('os.path.exists') def test_sorted_branch(os_path_exists): os_path_exists.return_value = True @@ -119,18 +127,26 @@ def test_normalize_long_commit_message(): The `Show Source` was broken because of a change made in sphinx 1.5.1 In Sphinx 1.4.9, the sourcename was "index.txt". In Sphinx 1.5.1+, it is now "index.rst.txt". -(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69)""" +(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) + +Co-authored-by: Elmar Ritsch <35851+elritsch@users.noreply.github.com>""" title, body = normalize_commit_message(commit_message) assert title == "[3.6] Fix broken `Show Source` links on documentation pages (GH-3113)" assert body == """The `Show Source` was broken because of a change made in sphinx 1.5.1 In Sphinx 1.4.9, the sourcename was "index.txt". In Sphinx 1.5.1+, it is now "index.rst.txt". -(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69)""" +(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) + +Co-authored-by: Elmar Ritsch <35851+elritsch@users.noreply.github.com>""" def test_normalize_short_commit_message(): commit_message = """[3.6] Fix broken `Show Source` links on documentation pages (GH-3113) -(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69)""" +(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) + +Co-authored-by: Elmar Ritsch <35851+elritsch@users.noreply.github.com>""" title, body = normalize_commit_message(commit_message) assert title == "[3.6] Fix broken `Show Source` links on documentation pages (GH-3113)" - assert body == """(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69)""" \ No newline at end of file + assert body == """(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) + +Co-authored-by: Elmar Ritsch <35851+elritsch@users.noreply.github.com>""" \ No newline at end of file From 54066945f40db646e118819aa16daa54e3af39b4 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Mon, 29 Jan 2018 17:11:39 -0800 Subject: [PATCH 2/2] EOL --- cherry_picker/cherry_picker/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cherry_picker/cherry_picker/test.py b/cherry_picker/cherry_picker/test.py index f9447a0..33f241d 100644 --- a/cherry_picker/cherry_picker/test.py +++ b/cherry_picker/cherry_picker/test.py @@ -149,4 +149,4 @@ def test_normalize_short_commit_message(): assert title == "[3.6] Fix broken `Show Source` links on documentation pages (GH-3113)" assert body == """(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) -Co-authored-by: Elmar Ritsch <35851+elritsch@users.noreply.github.com>""" \ No newline at end of file +Co-authored-by: Elmar Ritsch <35851+elritsch@users.noreply.github.com>"""