Skip to content

Commit

Permalink
Fix --dry-run option incorrect behaviour (#75)
Browse files Browse the repository at this point in the history
```
# No setting upstream, so output command sequence should print origin
root@2ec8214d91c9:/tmp/cpython# git remote get-url upstream
error: No such remote 'upstream'
```

```
root@2ec8214d91c9:/tmp/cpython# cherry_picker --dry-run ab5b56ba6d 3.7
... ...
Dry run requested, listing expected command sequence
  dry-run: git remote get-url upstream
  dry-run: git fetch upstream --no-tags
Now backporting 'ab5b56ba6d' into '3.7'
  dry-run: git remote get-url upstream
  dry-run: git checkout -b backport-ab5b56b-3.7 upstream/3.7
  dry-run: git cherry-pick -x ab5b56ba6d

  dry-run: git show -s --format=%B ab5b56ba6d
Traceback (most recent call last):
... ...

  File "/usr/local/lib/python3.10/dist-packages/cherry_picker/cherry_picker.py", line 645, in cherry_pick_cli
    cherry_picker.backport()
  File "/usr/local/lib/python3.10/dist-packages/cherry_picker/cherry_picker.py", line 387, in backport
    commit_message = self.amend_commit_message(cherry_pick_branch)
  File "/usr/local/lib/python3.10/dist-packages/cherry_picker/cherry_picker.py", line 269, in amend_commit_message
    updated_commit_message = f"""{commit_prefix}{self.get_commit_message(self.commit_sha1)}
  File "/usr/local/lib/python3.10/dist-packages/cherry_picker/cherry_picker.py", line 211, in get_commit_message
    message = self.run_cmd(cmd).strip()
AttributeError: 'NoneType' object has no attribute 'strip'
```

Signed-off-by: Chenyang Yan <memory.yancy@gmail.com>

Signed-off-by: Chenyang Yan <memory.yancy@gmail.com>
  • Loading branch information
uddmorningsun committed Oct 4, 2022
1 parent 9ab4f63 commit 9c64d2c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def upstream(self):
cmd[-1] = self.upstream_remote

try:
self.run_cmd(cmd)
self.run_cmd(cmd, required_real_result=True)
except subprocess.CalledProcessError:
if self.upstream_remote is not None:
raise ValueError(f"There is no remote with name {cmd[-1]!r}.")
Expand All @@ -196,7 +196,7 @@ def sorted_branches(self):
@property
def username(self):
cmd = ["git", "config", "--get", f"remote.{self.pr_remote}.url"]
result = self.run_cmd(cmd)
result = self.run_cmd(cmd, required_real_result=True)
# implicit ssh URIs use : to separate host from user, others just use /
username = result.replace(":", "/").split("/")[-2]
return username
Expand All @@ -214,9 +214,9 @@ def fetch_upstream(self):
self.run_cmd(cmd)
set_state(WORKFLOW_STATES.FETCHED_UPSTREAM)

def run_cmd(self, cmd):
def run_cmd(self, cmd, required_real_result=False):
assert not isinstance(cmd, str)
if self.dry_run:
if not required_real_result and self.dry_run:
click.echo(f" dry-run: {' '.join(cmd)}")
return
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
Expand Down Expand Up @@ -252,7 +252,7 @@ def get_commit_message(self, commit_sha):
"""
cmd = ["git", "show", "-s", "--format=%B", commit_sha]
try:
message = self.run_cmd(cmd).strip()
message = self.run_cmd(cmd, required_real_result=True).strip()
except subprocess.CalledProcessError as err:
click.echo(f"Error getting commit message for {commit_sha}")
click.echo(err.output)
Expand Down Expand Up @@ -665,7 +665,7 @@ def is_mirror(self) -> bool:

cmd = ["git", "config", "--local", "--get", "remote.origin.mirror"]
try:
out = self.run_cmd(cmd)
out = self.run_cmd(cmd, required_real_result=True)
except subprocess.CalledProcessError:
return False
return out.startswith("true")
Expand Down
2 changes: 1 addition & 1 deletion cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def test_start_end_states(method_name, start_state, end_state, tmp_git_repo_dir)
cherry_picker.remember_previous_branch()
assert get_state() == WORKFLOW_STATES.UNSET

def _fetch(cmd):
def _fetch(cmd, *args, **kwargs):
assert get_state() == start_state

with mock.patch.object(cherry_picker, "run_cmd", _fetch):
Expand Down

0 comments on commit 9c64d2c

Please sign in to comment.