Refactoring: Extract git checks in push and merge scripts.

This extracts the pattern "if call git fails: raise exception", which is spread all over the place. Now all calls to git are required to return gracefully and give a uniform exception message if they don't.

BUG=
R=ulan@chromium.org

Review URL: https://codereview.chromium.org/166903012

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19494 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 7a948d32
...@@ -220,6 +220,10 @@ class NoRetryException(Exception): ...@@ -220,6 +220,10 @@ class NoRetryException(Exception):
pass pass
class GitFailedException(Exception):
pass
class CommonOptions(object): class CommonOptions(object):
def __init__(self, options, manual=True): def __init__(self, options, manual=True):
self.requires_editor = True self.requires_editor = True
...@@ -320,7 +324,10 @@ class Step(object): ...@@ -320,7 +324,10 @@ class Step(object):
def Git(self, args="", prefix="", pipe=True, retry_on=None): def Git(self, args="", prefix="", pipe=True, retry_on=None):
cmd = lambda: self._side_effect_handler.Command("git", args, prefix, pipe) cmd = lambda: self._side_effect_handler.Command("git", args, prefix, pipe)
return self.Retry(cmd, retry_on, [5, 30]) result = self.Retry(cmd, retry_on, [5, 30])
if result is None:
raise GitFailedException("'git %s' failed." % args)
return result
def SVN(self, args="", prefix="", pipe=True, retry_on=None): def SVN(self, args="", prefix="", pipe=True, retry_on=None):
cmd = lambda: self._side_effect_handler.Command("svn", args, prefix, pipe) cmd = lambda: self._side_effect_handler.Command("svn", args, prefix, pipe)
...@@ -361,8 +368,7 @@ class Step(object): ...@@ -361,8 +368,7 @@ class Step(object):
if re.match(r".*\s+%s$" % name, line): if re.match(r".*\s+%s$" % name, line):
msg = "Branch %s exists, do you want to delete it?" % name msg = "Branch %s exists, do you want to delete it?" % name
if self.Confirm(msg): if self.Confirm(msg):
if self.Git("branch -D %s" % name) is None: self.Git("branch -D %s" % name)
self.Die("Deleting branch '%s' failed." % name)
print "Branch %s deleted." % name print "Branch %s deleted." % name
else: else:
msg = "Can't continue. Please delete branch %s and try again." % name msg = "Can't continue. Please delete branch %s and try again." % name
...@@ -393,8 +399,7 @@ class Step(object): ...@@ -393,8 +399,7 @@ class Step(object):
break break
# Fetch unfetched revisions. # Fetch unfetched revisions.
if self.Git("svn fetch") is None: self.Git("svn fetch")
self.Die("'git svn fetch' failed.")
def PrepareBranch(self): def PrepareBranch(self):
# Get ahold of a safe temporary branch and check it out. # Get ahold of a safe temporary branch and check it out.
...@@ -457,7 +462,9 @@ class Step(object): ...@@ -457,7 +462,9 @@ class Step(object):
# Takes a file containing the patch to apply as first argument. # Takes a file containing the patch to apply as first argument.
def ApplyPatch(self, patch_file, reverse_patch=""): def ApplyPatch(self, patch_file, reverse_patch=""):
args = "apply --index --reject %s \"%s\"" % (reverse_patch, patch_file) args = "apply --index --reject %s \"%s\"" % (reverse_patch, patch_file)
if self.Git(args) is None: try:
self.Git(args)
except GitFailedException:
self.WaitForResolvingConflicts(patch_file) self.WaitForResolvingConflicts(patch_file)
def FindLastTrunkPush(self): def FindLastTrunkPush(self):
...@@ -484,8 +491,7 @@ class UploadStep(Step): ...@@ -484,8 +491,7 @@ class UploadStep(Step):
% (author, reviewer, force_flag)) % (author, reviewer, force_flag))
# TODO(machenbach): Check output in forced mode. Verify that all required # TODO(machenbach): Check output in forced mode. Verify that all required
# base files were uploaded, if not retry. # base files were uploaded, if not retry.
if self.Git(args, pipe=False) is None: self.Git(args, pipe=False)
self.Die("'git cl upload' failed, please try again.")
def MakeStep(step_class=Step, number=0, state=None, config=None, def MakeStep(step_class=Step, number=0, state=None, config=None,
......
...@@ -91,10 +91,8 @@ class CreateBranch(Step): ...@@ -91,10 +91,8 @@ class CreateBranch(Step):
MESSAGE = "Create a fresh branch for the patch." MESSAGE = "Create a fresh branch for the patch."
def RunStep(self): def RunStep(self):
args = "checkout -b %s svn/%s" % (self.Config(BRANCHNAME), self.Git("checkout -b %s svn/%s" % (self.Config(BRANCHNAME),
self["merge_to_branch"]) self["merge_to_branch"]))
if self.Git(args) is None:
self.die("Creating branch %s failed." % self.Config(BRANCHNAME))
class SearchArchitecturePorts(Step): class SearchArchitecturePorts(Step):
...@@ -226,24 +224,17 @@ class CommitLocal(Step): ...@@ -226,24 +224,17 @@ class CommitLocal(Step):
MESSAGE = "Commit to local branch." MESSAGE = "Commit to local branch."
def RunStep(self): def RunStep(self):
if self.Git("commit -a -F \"%s\"" % self.Config(COMMITMSG_FILE)) is None: self.Git("commit -a -F \"%s\"" % self.Config(COMMITMSG_FILE))
self.Die("'git commit -a' failed.")
class CommitRepository(Step): class CommitRepository(Step):
MESSAGE = "Commit to the repository." MESSAGE = "Commit to the repository."
def RunStep(self): def RunStep(self):
if self.Git("checkout %s" % self.Config(BRANCHNAME)) is None: self.Git("checkout %s" % self.Config(BRANCHNAME))
self.Die("Cannot ensure that the current branch is %s"
% self.Config(BRANCHNAME))
self.WaitForLGTM() self.WaitForLGTM()
if self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"") is None: self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"")
self.Die("Presubmit failed.") self.Git("cl dcommit -f --bypass-hooks", retry_on=lambda x: x is None)
if self.Git("cl dcommit -f --bypass-hooks",
retry_on=lambda x: x is None) is None:
self.Die("Failed to commit to %s" % self._status["merge_to_branch"])
class PrepareSVN(Step): class PrepareSVN(Step):
...@@ -252,8 +243,7 @@ class PrepareSVN(Step): ...@@ -252,8 +243,7 @@ class PrepareSVN(Step):
def RunStep(self): def RunStep(self):
if self._options.revert_bleeding_edge: if self._options.revert_bleeding_edge:
return return
if self.Git("svn fetch") is None: self.Git("svn fetch")
self.Die("'git svn fetch' failed.")
args = ("log -1 --format=%%H --grep=\"%s\" svn/%s" args = ("log -1 --format=%%H --grep=\"%s\" svn/%s"
% (self["new_commit_msg"], self["merge_to_branch"])) % (self["new_commit_msg"], self["merge_to_branch"]))
commit_hash = self.Git(args).strip() commit_hash = self.Git(args).strip()
......
...@@ -97,8 +97,7 @@ class FreshBranch(Step): ...@@ -97,8 +97,7 @@ class FreshBranch(Step):
def RunStep(self): def RunStep(self):
args = "checkout -b %s svn/bleeding_edge" % self.Config(BRANCHNAME) args = "checkout -b %s svn/bleeding_edge" % self.Config(BRANCHNAME)
if self.Git(args) is None: self.Git(args)
self.Die("Creating branch %s failed." % self.Config(BRANCHNAME))
class DetectLastPush(Step): class DetectLastPush(Step):
...@@ -268,9 +267,7 @@ class CommitLocal(Step): ...@@ -268,9 +267,7 @@ class CommitLocal(Step):
review = "\n\nTBR=%s" % self._options.reviewer review = "\n\nTBR=%s" % self._options.reviewer
else: else:
review = "" review = ""
if self.Git("commit -a -m \"%s%s\"" self.Git("commit -a -m \"%s%s\"" % (self["prep_commit_msg"], review))
% (self["prep_commit_msg"], review)) is None:
self.Die("'git commit -a' failed.")
class CommitRepository(Step): class CommitRepository(Step):
...@@ -283,12 +280,8 @@ class CommitRepository(Step): ...@@ -283,12 +280,8 @@ class CommitRepository(Step):
TextToFile(GetLastChangeLogEntries(self.Config(CHANGELOG_FILE)), TextToFile(GetLastChangeLogEntries(self.Config(CHANGELOG_FILE)),
self.Config(CHANGELOG_ENTRY_FILE)) self.Config(CHANGELOG_ENTRY_FILE))
if self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"") is None: self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"")
self.Die("'git cl presubmit' failed, please try again.") self.Git("cl dcommit -f --bypass-hooks", retry_on=lambda x: x is None)
if self.Git("cl dcommit -f --bypass-hooks",
retry_on=lambda x: x is None) is None:
self.Die("'git cl dcommit' failed, please try again.")
class StragglerCommits(Step): class StragglerCommits(Step):
...@@ -296,8 +289,7 @@ class StragglerCommits(Step): ...@@ -296,8 +289,7 @@ class StragglerCommits(Step):
"started.") "started.")
def RunStep(self): def RunStep(self):
if self.Git("svn fetch") is None: self.Git("svn fetch")
self.Die("'git svn fetch' failed.")
self.Git("checkout svn/bleeding_edge") self.Git("checkout svn/bleeding_edge")
args = "log -1 --format=%%H --grep=\"%s\"" % self["prep_commit_msg"] args = "log -1 --format=%%H --grep=\"%s\"" % self["prep_commit_msg"]
self["prepare_commit_hash"] = self.Git(args).strip() self["prepare_commit_hash"] = self.Git(args).strip()
...@@ -342,9 +334,7 @@ class NewBranch(Step): ...@@ -342,9 +334,7 @@ class NewBranch(Step):
MESSAGE = "Create a new branch from trunk." MESSAGE = "Create a new branch from trunk."
def RunStep(self): def RunStep(self):
if self.Git("checkout -b %s svn/trunk" % self.Config(TRUNKBRANCH)) is None: self.Git("checkout -b %s svn/trunk" % self.Config(TRUNKBRANCH))
self.Die("Checking out a new branch '%s' failed." %
self.Config(TRUNKBRANCH))
class ApplyChanges(Step): class ApplyChanges(Step):
...@@ -380,8 +370,7 @@ class CommitTrunk(Step): ...@@ -380,8 +370,7 @@ class CommitTrunk(Step):
def RunStep(self): def RunStep(self):
self.Git("add \"%s\"" % self.Config(VERSION_FILE)) self.Git("add \"%s\"" % self.Config(VERSION_FILE))
if self.Git("commit -F \"%s\"" % self.Config(COMMITMSG_FILE)) is None: self.Git("commit -F \"%s\"" % self.Config(COMMITMSG_FILE))
self.Die("'git commit' failed.")
Command("rm", "-f %s*" % self.Config(COMMITMSG_FILE)) Command("rm", "-f %s*" % self.Config(COMMITMSG_FILE))
...@@ -423,10 +412,9 @@ class TagRevision(Step): ...@@ -423,10 +412,9 @@ class TagRevision(Step):
MESSAGE = "Tag the new revision." MESSAGE = "Tag the new revision."
def RunStep(self): def RunStep(self):
if self.Git(("svn tag %s -m \"Tagging version %s\"" self.Git(("svn tag %s -m \"Tagging version %s\""
% (self["version"], self["version"])), % (self["version"], self["version"])),
retry_on=lambda x: x is None) is None: retry_on=lambda x: x is None)
self.Die("'git svn tag' failed.")
class CheckChromium(Step): class CheckChromium(Step):
...@@ -466,14 +454,9 @@ class UpdateChromiumCheckout(Step): ...@@ -466,14 +454,9 @@ class UpdateChromiumCheckout(Step):
def RunStep(self): def RunStep(self):
os.chdir(self["chrome_path"]) os.chdir(self["chrome_path"])
if self.Git("checkout master") is None: self.Git("checkout master")
self.Die("'git checkout master' failed.") self.Git("pull")
if self.Git("pull") is None: self.Git("checkout -b v8-roll-%s" % self["trunk_revision"])
self.Die("'git pull' failed, please try again.")
args = "checkout -b v8-roll-%s" % self["trunk_revision"]
if self.Git(args) is None:
self.Die("Failed to checkout a new branch.")
class UploadCL(Step): class UploadCL(Step):
...@@ -497,17 +480,13 @@ class UploadCL(Step): ...@@ -497,17 +480,13 @@ class UploadCL(Step):
print "Please enter the email address of a reviewer for the roll CL: ", print "Please enter the email address of a reviewer for the roll CL: ",
self.DieNoManualMode("A reviewer must be specified in forced mode.") self.DieNoManualMode("A reviewer must be specified in forced mode.")
rev = self.ReadLine() rev = self.ReadLine()
args = ("commit -am \"Update V8 to version %s " self.Git("commit -am \"Update V8 to version %s "
"(based on bleeding_edge revision r%s).\n\nTBR=%s\"" "(based on bleeding_edge revision r%s).\n\nTBR=%s\""
% (self["version"], self["svn_revision"], rev)) % (self["version"], self["svn_revision"], rev))
if self.Git(args) is None:
self.Die("'git commit' failed.")
author_option = self._options.author author_option = self._options.author
author = " --email \"%s\"" % author_option if author_option else "" author = " --email \"%s\"" % author_option if author_option else ""
force_flag = " -f" if self._options.force_upload else "" force_flag = " -f" if self._options.force_upload else ""
if self.Git("cl upload%s --send-mail%s" % (author, force_flag), self.Git("cl upload%s --send-mail%s" % (author, force_flag), pipe=False)
pipe=False) is None:
self.Die("'git cl upload' failed, please try again.")
print "CL uploaded." print "CL uploaded."
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment