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 <gavinmak@google.com>
Tested-by: Jason Chang <jasonnc@google.com>
Reviewed-by: Aravind Vasudevan <aravindvasudev@google.com>
Commit-Queue: Jason Chang <jasonnc@google.com>
This commit is contained in:
Jason Chang
2023-08-08 14:12:53 -07:00
committed by LUCI
parent f0aeb220de
commit 1a3612fe6d
10 changed files with 251 additions and 122 deletions
+50 -26
View File
@@ -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)
# 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)
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)