Skip to content

Commit f1b6e99

Browse files
authored
Standardize git output and make it a bit more chatty on errors (#31)
* Standardize git output and make it a bit more chatty on errors * Fix Travis
1 parent 0a8ed8e commit f1b6e99

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ python:
1414
- "3.8"
1515
- "3.7"
1616
- "3.6"
17-
- "nightly" # currently, it's 3.10
1817

1918
matrix:
2019
allow_failures:
2120
- python: "nightly"
2221
dist: focal
2322

2423
install:
25-
- python -m pip install --upgrade flit pip
24+
- python -m pip install --upgrade flit
25+
- python -m pip install --upgrade pip
2626
- flit install
2727

2828
script:

cherry_picker/cherry_picker.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def upstream(self):
140140
"""
141141
cmd = ["git", "remote", "get-url", "upstream"]
142142
try:
143-
subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
143+
self.run_cmd(cmd)
144144
except subprocess.CalledProcessError:
145145
return "origin"
146146
return "upstream"
@@ -153,8 +153,7 @@ def sorted_branches(self):
153153
@property
154154
def username(self):
155155
cmd = ["git", "config", "--get", f"remote.{self.pr_remote}.url"]
156-
raw_result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
157-
result = raw_result.decode("utf-8")
156+
result = self.run_cmd(cmd)
158157
# implicit ssh URIs use : to separate host from user, others just use /
159158
username = result.replace(":", "/").split("/")[-2]
160159
return username
@@ -178,7 +177,7 @@ def run_cmd(self, cmd):
178177
click.echo(f" dry-run: {' '.join(cmd)}")
179178
return
180179
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
181-
click.echo(output.decode("utf-8"))
180+
return output.decode("utf-8")
182181

183182
def checkout_branch(self, branch_name):
184183
""" git checkout -b <branch_name> """
@@ -206,8 +205,12 @@ def get_commit_message(self, commit_sha):
206205
replace #<PRID> with GH-<PRID>
207206
"""
208207
cmd = ["git", "show", "-s", "--format=%B", commit_sha]
209-
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
210-
message = output.strip().decode("utf-8")
208+
try:
209+
message = self.run_cmd(cmd).strip()
210+
except subprocess.CalledProcessError as err:
211+
click.echo(f"Error getting commit message for {commit_sha}")
212+
click.echo(err.output)
213+
raise CherryPickException(f"Error getting commit message for {commit_sha}")
211214
if self.config["fix_commit_msg"]:
212215
return message.replace("#", "GH-")
213216
else:
@@ -228,13 +231,13 @@ def status(self):
228231
:return:
229232
"""
230233
cmd = ["git", "status"]
231-
self.run_cmd(cmd)
234+
return self.run_cmd(cmd)
232235

233236
def cherry_pick(self):
234237
""" git cherry-pick -x <commit_sha1> """
235238
cmd = ["git", "cherry-pick", "-x", self.commit_sha1]
236239
try:
237-
self.run_cmd(cmd)
240+
click.echo(self.run_cmd(cmd))
238241
except subprocess.CalledProcessError as err:
239242
click.echo(f"Error cherry-pick {self.commit_sha1}.")
240243
click.echo(err.output)
@@ -271,7 +274,7 @@ def amend_commit_message(self, cherry_pick_branch):
271274
else:
272275
cmd = ["git", "commit", "--amend", "-m", updated_commit_message]
273276
try:
274-
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
277+
self.run_cmd(cmd)
275278
except subprocess.CalledProcessError as cpe:
276279
click.echo("Failed to amend the commit message \u2639")
277280
click.echo(cpe.output)
@@ -285,8 +288,9 @@ def push_to_remote(self, base_branch, head_branch, commit_message=""):
285288
try:
286289
self.run_cmd(cmd)
287290
set_state(WORKFLOW_STATES.PUSHED_TO_REMOTE)
288-
except subprocess.CalledProcessError:
291+
except subprocess.CalledProcessError as cpe:
289292
click.echo(f"Failed to push to {self.pr_remote} \u2639")
293+
click.echo(cpe.output)
290294
set_state(WORKFLOW_STATES.PUSHING_TO_REMOTE_FAILED)
291295
else:
292296
gh_auth = os.getenv("GH_AUTH")
@@ -338,7 +342,7 @@ def open_pr(self, url):
338342

339343
def delete_branch(self, branch):
340344
cmd = ["git", "branch", "-D", branch]
341-
self.run_cmd(cmd)
345+
return self.run_cmd(cmd)
342346

343347
def cleanup_branch(self, branch):
344348
"""Remove the temporary backport branch.
@@ -414,7 +418,7 @@ def abort_cherry_pick(self):
414418
cmd = ["git", "cherry-pick", "--abort"]
415419
try:
416420
set_state(WORKFLOW_STATES.ABORTING)
417-
self.run_cmd(cmd)
421+
click.echo(self.run_cmd(cmd))
418422
set_state(WORKFLOW_STATES.ABORTED)
419423
except subprocess.CalledProcessError as cpe:
420424
click.echo(cpe.output)
@@ -466,7 +470,7 @@ def continue_cherry_pick(self):
466470
updated_commit_message,
467471
"--allow-empty",
468472
]
469-
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
473+
self.run_cmd(cmd)
470474

471475
self.push_to_remote(base, cherry_pick_branch)
472476

0 commit comments

Comments
 (0)