From 1a3612fe6d347e458a53d7a9e920a91ea502e6ba Mon Sep 17 00:00:00 2001 From: Jason Chang Date: Tue, 8 Aug 2023 14:12:53 -0700 Subject: Raise RepoExitError in place of sys.exit Bug: b/293344017 Change-Id: Icae4932b00e4068cba502a5ab4a0274fd7854d9d Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/382214 Reviewed-by: Gavin Mak Tested-by: Jason Chang Reviewed-by: Aravind Vasudevan Commit-Queue: Jason Chang --- subcmds/cherry_pick.py | 76 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 26 deletions(-) (limited to 'subcmds/cherry_pick.py') diff --git a/subcmds/cherry_pick.py b/subcmds/cherry_pick.py index 4cfb8c88..7a4dd09e 100644 --- a/subcmds/cherry_pick.py +++ b/subcmds/cherry_pick.py @@ -16,6 +16,7 @@ import re import sys from command import Command from git_command import GitCommand +from error import GitError CHANGE_ID_RE = re.compile(r"^\s*Change-Id: I([0-9a-f]{40})\s*$") @@ -44,18 +45,31 @@ change id will be added. ["rev-parse", "--verify", reference], capture_stdout=True, capture_stderr=True, + verify_command=True, ) - if p.Wait() != 0: + try: + p.Wait() + except GitError: print(p.stderr, file=sys.stderr) - sys.exit(1) + raise + sha1 = p.stdout.strip() - p = GitCommand(None, ["cat-file", "commit", sha1], capture_stdout=True) - if p.Wait() != 0: + p = GitCommand( + None, + ["cat-file", "commit", sha1], + capture_stdout=True, + verify_command=True, + ) + + try: + p.Wait() + except GitError: print( "error: Failed to retrieve old commit message", file=sys.stderr ) - sys.exit(1) + raise + old_msg = self._StripHeader(p.stdout) p = GitCommand( @@ -63,37 +77,47 @@ change id will be added. ["cherry-pick", sha1], capture_stdout=True, capture_stderr=True, + verify_command=True, ) - status = p.Wait() + + try: + p.Wait() + except GitError as e: + print(str(e)) + print( + "NOTE: When committing (please see above) and editing the " + "commit message, please remove the old Change-Id-line and " + "add:" + ) + print(self._GetReference(sha1), file=sys.stderr) + print(file=sys.stderr) + raise if p.stdout: print(p.stdout.strip(), file=sys.stdout) if p.stderr: print(p.stderr.strip(), file=sys.stderr) - if status == 0: - # The cherry-pick was applied correctly. We just need to edit the - # commit message. - new_msg = self._Reformat(old_msg, sha1) - - p = GitCommand( - None, - ["commit", "--amend", "-F", "-"], - input=new_msg, - capture_stdout=True, - capture_stderr=True, - ) - if p.Wait() != 0: - print("error: Failed to update commit message", file=sys.stderr) - sys.exit(1) + # The cherry-pick was applied correctly. We just need to edit + # the commit message. + new_msg = self._Reformat(old_msg, sha1) - else: + p = GitCommand( + None, + ["commit", "--amend", "-F", "-"], + input=new_msg, + capture_stdout=True, + capture_stderr=True, + verify_command=True, + ) + try: + p.Wait() + except GitError: print( - "NOTE: When committing (please see above) and editing the " - "commit message, please remove the old Change-Id-line and add:" + "error: Failed to update commit message", + file=sys.stderr, ) - print(self._GetReference(sha1), file=sys.stderr) - print(file=sys.stderr) + raise def _IsChangeId(self, line): return CHANGE_ID_RE.match(line) -- cgit v1.2.3-54-g00ecf