Patchwork [7,of,8,v5] convert: encapsulate commit data fetching and commit object creation

login
register
mail settings
Submitter David Soria Parra
Date Dec. 14, 2016, 8:09 p.m.
Message ID <26ec2474ab06fdaf1aca.1481746168@devbig415.prn1.facebook.com>
Download mbox | patch
Permalink /patch/17919/
State Accepted
Headers show

Comments

David Soria Parra - Dec. 14, 2016, 8:09 p.m.
# HG changeset patch
# User David Soria Parra <davidsp@fb.com>
# Date 1481694598 28800
#      Tue Dec 13 21:49:58 2016 -0800
# Node ID 26ec2474ab06fdaf1aca034c54309245ded267b0
# Parent  c6c26e238617600d0523b44a9043d17f963f9eda
convert: encapsulate commit data fetching and commit object creation

Split fetching the `describe` form from Perforce and the commit object creation
into two functions. This allows us to reuse the commit construction for
revisions passed from a revmap.

Patch

diff --git a/hgext/convert/p4.py b/hgext/convert/p4.py
--- a/hgext/convert/p4.py
+++ b/hgext/convert/p4.py
@@ -151,24 +151,17 @@ 
                 lastid = change
                 continue
 
-            cmd = "p4 -G describe -s %s" % change
-            stdout = util.popen(cmd, mode='rb')
-            d = marshal.load(stdout)
-            desc = self.recode(d.get("desc", ""))
-            shortdesc = desc.split("\n", 1)[0]
-            t = '%s %s' % (d["change"], repr(shortdesc)[1:-1])
-            ui.status(util.ellipsis(t, 80) + '\n')
-
             if lastid:
                 parents = [lastid]
             else:
                 parents = []
 
-            date = (int(d["time"]), 0)     # timezone not set
-            c = common.commit(author=self.recode(d["user"]),
-                              date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'),
-                              parents=parents, desc=desc, branch=None,
-                              extra={"p4": change, "convert_revision": change})
+            d = self._fetch_revision(change)
+            c = self._construct_commit(d, parents)
+
+            shortdesc = c.desc.splitlines(True)[0].rstrip('\r\n')
+            t = '%s %s' % (c.rev, repr(shortdesc)[1:-1])
+            ui.status(util.ellipsis(t, 80) + '\n')
 
             files = []
             copies = {}
@@ -303,6 +296,30 @@ 
             raise error.Abort(_("convert from p4 does not support --full"))
         return self.files[rev], self.copies[rev], set()
 
+    def _construct_commit(self, obj, parents=None):
+        """
+        Constructs a common.commit object from an unmarshalled
+        `p4 describe` output
+        """
+        desc = self.recode(obj.get("desc", ""))
+        shortdesc = desc.split("\n", 1)[0]
+
+        date = (int(obj["time"]), 0)     # timezone not set
+        if parents is None:
+            parents = []
+
+        return common.commit(author=self.recode(obj["user"]),
+            date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'),
+            parents=parents, desc=desc, branch=None, rev=obj['change'],
+            extra={"p4": obj['change'], "convert_revision": obj['change']})
+
+    def _fetch_revision(self, rev):
+        """Return an output of `p4 describe` including author, commit date as
+        a dictionary."""
+        cmd = "p4 -G describe -s %s" % rev
+        stdout = util.popen(cmd, mode='rb')
+        return marshal.load(stdout)
+
     def getcommit(self, rev):
         return self.changeset[rev]