8000 Standardize git output and make it a bit more chatty on errors by ambv · Pull Request #31 · python/cherry-picker · GitHub
[go: up one dir, main page]

Skip to content

Standardize git output and make it a bit more chatty on errors #31

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 2 commits into from
Aug 2, 2021
Merged
Changes from 1 commit
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
Next Next commit
Standardize git output and make it a bit more chatty on errors
  • Loading branch information
ambv committed Aug 2, 2021
commit 07ca046dbf94fa038e585b357ce61b2925881aa6
30 changes: 17 additions & 13 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def upstream(self):
"""
cmd = ["git", "remote", "get-url", "upstream"]
try:
subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
self.run_cmd(cmd)
except subprocess.CalledProcessError:
return "origin"
return "upstream"
Expand All @@ -153,8 +153,7 @@ def sorted_branches(self):
@property
def username(self):
cmd = ["git", "config", "--get", f"remote.{self.pr_remote}.url"]
raw_result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
result = raw_result.decode("utf-8")
result = self.run_cmd(cmd)
# implicit ssh URIs use : to separate host from user, others just use /
username = result.replace(":", "/").split("/")[-2]
return username
Expand All @@ -178,7 +177,7 @@ def run_cmd(self, cmd):
click.echo(f" dry-run: {' '.join(cmd)}")
return
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
click.echo(output.decode("utf-8"))
return output.decode("utf-8")

def checkout_branch(self, branch_name):
""" git checkout -b <branch_name> """
Expand Down Expand Up @@ -206,8 +205,12 @@ def get_commit_message(self, commit_sha):
replace #<PRID> with GH-<PRID>
"""
cmd = ["git", "show", "-s", "--format=%B", commit_sha]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
message = output.strip().decode("utf-8")
try:
message = self.run_cmd(cmd).strip()
except subprocess.CalledProcessError as err:
click.echo(f"Error getting commit message for {commit_sha}")
click.echo(err.output)
raise CherryPickException(f"Error getting commit message for {commit_sha}")
if self.config["fix_commit_msg"]:
return message.replace("#", "GH-")
else:
Expand All @@ -228,13 +231,13 @@ def status(self):
:return:
"""
cmd = ["git", "status"]
self.run_cmd(cmd)
return self.run_cmd(cmd)

def cherry_pick(self):
""" git cherry-pick -x <commit_sha1> """
cmd = ["git", "cherry-pick", "-x", self.commit_sha1]
try:
self.run_cmd(cmd)
click.echo(self.run_cmd(cmd))
except subprocess.CalledProcessError as err:
click.echo(f"Error cherry-pick {self.commit_sha1}.")
click.echo(err.output)
Expand Down Expand Up @@ -271,7 +274,7 @@ def amend_commit_message(self, cherry_pick_branch):
else:
cmd = ["git", "commit", "--amend", "-m", updated_commit_message]
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
self.run_cmd(cmd)
except subprocess.CalledProcessError as cpe:
click.echo("Failed to amend the commit message \u2639")
click.echo(cpe.output)
Expand All @@ -285,8 +288,9 @@ def push_to_remote(self, base_branch, head_branch, commit_message=""):
try:
self.run_cmd(cmd)
set_state(WORKFLOW_STATES.PUSHED_TO_REMOTE)
except subprocess.CalledProcessError:
except subprocess.CalledProcessError as cpe:
click.echo(f"Failed to push to {self.pr_remote} \u2639")
click.echo(cpe.output)
set_state(WORKFLOW_STATES.PUSHING_TO_REMOTE_FAILED)
else:
gh_auth = os.getenv("GH_AUTH")
Expand Down Expand Up @@ -338,7 +342,7 @@ def open_pr(self, url):

def delete_branch(self, branch):
cmd = ["git", "branch", "-D", branch]
self.run_cmd(cmd)
return self.run_cmd(cmd)

def cleanup_branch(self, branch):
"""Remove the temporary backport branch.
Expand Down Expand Up @@ -414,7 +418,7 @@ def abort_cherry_pick(self):
cmd = ["git", "cherry-pick", "--abort"]
try:
set_state(WORKFLOW_STATES.ABORTING)
self.run_cmd(cmd)
click.echo(self.run_cmd(cmd))
set_state(WORKFLOW_STATES.ABORTED)
except subprocess.CalledProcessError as cpe:
click.echo(cpe.output)
Expand Down Expand Up @@ -466,7 +470,7 @@ def continue_cherry_pick(self):
updated_commit_message,
"--allow-empty",
]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
self.run_cmd(cmd)

self.push_to_remote(base, cherry_pick_branch)

Expand Down
0