autoland: refactor: consolidate transplant's run_hg_cmds (bug 1349997); r?gps draft
authorbyron jones <glob@mozilla.com>
Wed, 22 Mar 2017 21:54:19 +0800
changeset 10574 bc300124464a2953232b99ff78d831d6ee4bd0fa
parent 10573 2a200664d68d563ada6880d57e7e720e652ece91
child 10575 1eb9c1858c813342383904c66a4ff4c0e7b16daf
push id1595
push userbjones@mozilla.com
push dateFri, 31 Mar 2017 04:09:17 +0000
reviewersgps
bugs1349997
autoland: refactor: consolidate transplant's run_hg_cmds (bug 1349997); r?gps To reduce code duplication add run_hg_cmds. MozReview-Commit-ID: E1AuQ6SPjsB
autoland/autoland/transplant.py
--- a/autoland/autoland/transplant.py
+++ b/autoland/autoland/transplant.py
@@ -91,33 +91,41 @@ def run_hg(hg_repo, rev, args):
     out = hglib.util.BytesIO()
     out_channels = {b'o': out.write, b'e': out.write}
     ret = hg_repo.runcommand(args, {}, out_channels)
     if ret:
         raise hglib.error.CommandError(args, ret, out, None)
     return out.getvalue()
 
 
+def run_hg_cmds(hg_repo, rev, cmds):
+    last_result = ''
+    for cmd in cmds:
+        try:
+            last_result = run_hg(hg_repo, rev, cmd)
+        except hglib.error.CommandError as e:
+            raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
+    return last_result
+
+
 def strip_drafts(hg_repo, rev):
     # Strip any lingering draft changesets.
     try:
         run_hg(hg_repo, rev, ['strip', '--no-backup', '-r', 'not public()'])
     except hglib.error.CommandError:
         pass
 
 
 def get_remote_tip(hg_repo, rev):
     # Obtain remote tip. We assume there is only a single head.
     # Output can contain bookmark or branch name after a space. Only take
     # first component.
-    cmd = ['identify', 'upstream', '-r', 'tip']
-    try:
-        remote_tip = run_hg(hg_repo, rev, cmd)
-    except hglib.error.CommandError:
-        raise Exception(formulate_hg_error(['hg'] + cmd, ''))
+    remote_tip = run_hg_cmds(hg_repo, rev, [
+        ['identify', 'upstream', '-r', 'tip']
+    ])
     remote_tip = remote_tip.split()[0]
     assert len(remote_tip) == 12, remote_tip
     return remote_tip
 
 
 def update_repo(hg_repo, rev, tree, remote_rev):
     # Pull "upstream" and update to remote tip. Pull revisions to land and
     # update to them.
@@ -147,51 +155,47 @@ def rewrite_commit_descriptions(hg_repo,
     # Rewrite commit descriptions as per the mapping provided.  Returns the
     # revision of the base commit.
     assert commit_descriptions, 'commit_descriptions requires for transplant'
 
     with tempfile.NamedTemporaryFile() as f:
         json.dump(commit_descriptions, f)
         f.flush()
 
-        cmd = ['rewritecommitdescriptions', '--descriptions=%s' % f.name, rev]
-        try:
-            cmd_output = run_hg(hg_repo, rev, cmd)
+        cmd_output = run_hg_cmds(hg_repo, rev, [
+            ['rewritecommitdescriptions', '--descriptions=%s' % f.name, rev]
+        ])
 
-            base_revision = None
-            for line in cmd_output.splitlines():
-                m = re.search(r'^rev: [0-9a-z]+ -> ([0-9a-z]+)', line)
-                if m and m.groups():
-                    base_revision = m.groups()[0]
-                    break
+        base_revision = None
+        for line in cmd_output.splitlines():
+            m = re.search(r'^rev: [0-9a-z]+ -> ([0-9a-z]+)', line)
+            if m and m.groups():
+                base_revision = m.groups()[0]
+                break
 
-            if not base_revision:
-                raise Exception('Could not determine base revision for '
-                                'rebase: %s' % cmd_output)
+        if not base_revision:
+            raise Exception('Could not determine base revision for '
+                            'rebase: %s' % cmd_output)
 
-            return base_revision
-        except hglib.error.CommandError as e:
-            raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
+        return base_revision
 
 
 def rebase(hg_repo, rev, base_revision, remote_tip):
     # Perform rebase if necessary.  Returns tip revision.
     cmd = ['rebase', '-s', base_revision, '-d', remote_tip]
     try:
         run_hg(hg_repo, rev, cmd)
     except hglib.error.CommandError as e:
         output = e.out.getvalue()
         if 'nothing to rebase' not in output:
             raise Exception(formulate_hg_error(['hg'] + cmd, output))
 
-    cmd = ['log', '-r', 'tip', '-T', '{node|short}']
-    try:
-        return run_hg(hg_repo, rev, cmd)
-    except hglib.error.CommandError as e:
-        raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
+    return run_hg_cmds(hg_repo, rev, [
+        ['log', '-r', 'tip', '-T', '{node|short}']
+    ])
 
 
 def validate_descriptions(hg_repo, destination, commit_descriptions):
     # Match outgoing commit descriptions against incoming commit
     # descriptions. If these don't match exactly, prevent the landing
     # from occurring.
     incoming_descriptions = set([c.encode(hg_repo.encoding)
                                  for c in commit_descriptions.values()])
@@ -207,49 +211,31 @@ def validate_descriptions(hg_repo, desti
                         "rewriting or rebasing your commits. The commits "
                         "being pushed no longer match what was requested. "
                         "Please file a bug.")
 
 
 def push_to_try(hg_repo, rev, trysyntax):
     if not trysyntax.startswith("try: "):
         trysyntax = "try: %s" % trysyntax
-    cmds = [
+    return run_hg_cmds(hg_repo, rev, [
         [
             '--encoding=utf-8',
             '--config', 'ui.allowemptycommit=true',
             'commit',
             '-m', trysyntax
         ],
+        ['push', '-r', '.', '-f', 'try'],
         ['log', '-r', 'tip', '-T', '{node|short}'],
-        ['push', '-r', '.', '-f', 'try']
-    ]
-
-    result = ''
-    for cmd in cmds:
-        try:
-            output = run_hg(hg_repo, rev, cmd)
-            if 'log' in cmd:
-                result = output
-        except hglib.error.CommandError as e:
-            raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
-    return result
+    ])
 
 
 def push_bookmark_to_repo(hg_repo, rev, destination, bookmark):
-    cmds = [['bookmark', bookmark],
-            ['push', '-B', bookmark, destination]]
-
-    for cmd in cmds:
-        try:
-            run_hg(hg_repo, rev, cmd)
-        except hglib.error.CommandError as e:
-            raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
+    run_hg_cmds(hg_repo, rev, [
+        ['bookmark', bookmark],
+        ['push', '-B', bookmark, destination],
+    ])
 
 
 def push_to_repo(hg_repo, rev, destination):
-    cmds = [['push', '-r', 'tip', destination]]
-
-    for cmd in cmds:
-        try:
-            run_hg(hg_repo, rev, cmd)
-        except hglib.error.CommandError as e:
-            raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
+    run_hg_cmds(hg_repo, rev, [
+        ['push', '-r', 'tip', destination]
+    ])