autoland: refactor: split transplant actions into methods (bug 1349997); r?gps draft
authorbyron jones <glob@mozilla.com>
Thu, 23 Mar 2017 14:08:19 +0800
changeset 10579 0d0c11414c2f6f26ab70cc2f1821d175fddd116c
parent 10578 59f5a72f798be190b4a10eb128c08ea6b82ae831
child 10580 ce521a3cd1b288b2b9bd00361abb519637f9466e
push id1595
push userbjones@mozilla.com
push dateFri, 31 Mar 2017 04:09:17 +0000
reviewersgps
bugs1349997
autoland: refactor: split transplant actions into methods (bug 1349997); r?gps Move logic for determining what action to perform into caller. MozReview-Commit-ID: L0IEbD72GdX
autoland/autoland/transplant.py
--- a/autoland/autoland/transplant.py
+++ b/autoland/autoland/transplant.py
@@ -16,77 +16,77 @@ class HgCommandError(Exception):
         # we want to strip out any sensitive --config options
         hg_args = map(lambda x: x if not x.startswith('bugzilla') else 'xxx',
                       hg_args)
         message = 'hg error in cmd: hg %s: %s' % (' '.join(hg_args),
                                                   out.getvalue())
         super(self.__class__, self).__init__(message)
 
 
-def get_repo_path(tree):
-    return config.get('repos').get(tree,
-                                   os.path.join(os.path.sep, 'repos', tree))
-
-
 def transplant(tree, destination, rev, trysyntax=None,
                push_bookmark=False, commit_descriptions=None):
     """Transplant a specified revision and ancestors to the specified tree.
 
     If ``trysyntax`` is specified, a Try commit will be created using the
     syntax specified.
     """
     # These values can appear in command arguments. Don't let unicode leak
     # into these.
     assert isinstance(tree, str)
     assert isinstance(destination, str)
     assert isinstance(rev, str)
     if push_bookmark:
         assert isinstance(push_bookmark, str)
 
-    path = get_repo_path(tree)
-    configs = ['ui.interactive=False']
-    with hglib.open(path, encoding='utf-8', configs=configs) as hg_repo:
-        tp = Transplant(hg_repo, tree, destination, rev)
-        return tp.transplant(trysyntax=trysyntax, push_bookmark=push_bookmark,
-                             commit_descriptions=commit_descriptions)
+    try:
+        path = config.get('repos').get(tree,
+                                       os.path.join(os.path.sep, 'repos', tree))
+        configs = ['ui.interactive=False']
+        with hglib.open(path, encoding='utf-8', configs=configs) as hg_repo:
+            tp = Transplant(hg_repo, tree, destination, rev)
+
+            if trysyntax:
+                return True, tp.push_try(trysyntax)
+
+            elif push_bookmark:
+                return True, tp.push_bookmark(commit_descriptions,
+                                              push_bookmark)
+
+            else:
+                return True, tp.push(commit_descriptions)
+    except Exception as e:
+        return False, str(e)
 
 
 class Transplant:
     def __init__(self, hg_repo, tree, destination, rev):
         self.hg_repo = hg_repo
         self.tree = tree
         self.destination = destination
         self.source_rev = rev
 
-    def transplant(self, trysyntax=None, push_bookmark=False,
-                   commit_descriptions=None):
-        result = ''
-        try:
-            # Update from "upstream"
-            remote_tip = self.update_repo()
-
-            # Update commit descriptions and rebase.
-            if not trysyntax:
-                result = self.apply_changes(remote_tip, commit_descriptions)
+    def push_try(self, trysyntax):
+        self.update_repo()
+        rev = self.push_to_try(trysyntax)
+        self.strip_drafts()
+        return rev
 
-            # Now we push to the destination
-            if trysyntax:
-                result = self.push_to_try(trysyntax)
-            elif push_bookmark:
-                self.push_bookmark_to_repo(push_bookmark)
-            else:
-                self.push_to_repo()
+    def push_bookmark(self, commit_descriptions, bookmark):
+        remote_tip = self.update_repo()
+        rev = self.apply_changes(remote_tip, commit_descriptions)
+        self.push_bookmark_to_repo(bookmark)
+        self.strip_drafts()
+        return rev
 
-            # Strip any lingering draft changesets.
-            self.strip_drafts()
-
-            return True, result
-
-        except Exception as e:
-            return False, str(e)
+    def push(self, commit_descriptions):
+        remote_tip = self.update_repo()
+        rev = self.apply_changes(remote_tip, commit_descriptions)
+        self.push_to_repo()
+        self.strip_drafts()
+        return rev
 
     def update_repo(self):
         # Obtain remote tip. We assume there is only a single head.
         remote_tip = self.get_remote_tip()
 
         # Strip any lingering draft changesets.
         self.strip_drafts()