[PATCH 3 of 4] convert: return calculated values from parse() instead of manpulating state

David Soria Parra davidsp at fb.com
Tue Dec 20 13:07:21 EST 2016


# HG changeset patch
# User David Soria Parra <davidsp at fb.com>
# Date 1482254630 28800
#      Tue Dec 20 09:23:50 2016 -0800
# Node ID 77e1912e2e0817a96633568c400450a9566e7e33
# Parent  6f7154489117ac5cc9f0f876e922fef5c44803d8
convert: return calculated values from parse() instead of manpulating state

diff --git a/hgext/convert/p4.py b/hgext/convert/p4.py
--- a/hgext/convert/p4.py
+++ b/hgext/convert/p4.py
@@ -56,7 +56,6 @@
         common.checktool('p4', abort=False)
 
         self.revmap = {}
-        self.p4changes = {}
         self.heads = []
         self.changeset = {}
         self.files = {}
@@ -75,7 +74,7 @@
         if revs and len(revs) > 1:
             raise error.Abort(_("p4 source does not support specifying "
                                "multiple revisions"))
-        self._parse(ui, path)
+        self._parse_once(ui, path)
 
     def setrevmap(self, revmap):
         """Sets the parsed revmap dictionary.
@@ -103,11 +102,19 @@
 
     def _parse(self, ui, path):
         "Prepare list of P4 filenames and revisions to import"
+        p4changes = {}
+        changeset = {}
+        files_map = {}
+        copies_map = {}
+        localname = {}
+        depotname = {}
+        heads = []
+
         ui.status(_('reading p4 views\n'))
 
         # read client spec or view
         if "/" in path:
-            self.p4changes.update(self._parse_view(path))
+            p4changes.update(self._parse_view(path))
             if path.startswith("//") and path.endswith("/..."):
                 views = {path[:-3]:""}
             else:
@@ -120,7 +127,7 @@
             for client in clientspec:
                 if client.startswith("View"):
                     sview, cview = clientspec[client].split()
-                    self.p4changes.update(self._parse_view(sview))
+                    p4changes.update(self._parse_view(sview))
                     if sview.endswith("...") and cview.endswith("..."):
                         sview = sview[:-3]
                         cview = cview[:-3]
@@ -129,8 +136,8 @@
                     views[sview] = cview
 
         # list of changes that affect our source files
-        self.p4changes = self.p4changes.keys()
-        self.p4changes.sort(key=int)
+        p4changes = p4changes.keys()
+        p4changes.sort(key=int)
 
         # list with depot pathnames, longest first
         vieworder = views.keys()
@@ -142,7 +149,7 @@
         # now read the full changelists to get the list of file revisions
         ui.status(_('collecting p4 changelists\n'))
         lastid = None
-        for change in self.p4changes:
+        for change in p4changes:
             if startrev and int(change) < int(startrev):
                 continue
             if self.revs and int(change) > int(self.revs[0]):
@@ -167,7 +174,6 @@
             files = []
             copies = {}
             copiedfiles = []
-            localname = {}
             i = 0
             while ("depotFile%d" % i) in d and ("rev%d" % i) in d:
                 oldname = d["depotFile%d" % i]
@@ -178,7 +184,7 @@
                         break
                 if filename:
                     files.append((filename, d["rev%d" % i]))
-                    self.depotname[filename] = oldname
+                    depotname[filename] = oldname
                     if (d.get("action%d" % i) == "move/add"):
                         copiedfiles.append(filename)
                     localname[oldname] = filename
@@ -186,7 +192,7 @@
 
             # Collect information about copied files
             for filename in copiedfiles:
-                oldname = self.depotname[filename]
+                oldname = depotname[filename]
 
                 flcmd = 'p4 -G filelog %s' \
                       % util.shellquote(oldname)
@@ -218,13 +224,29 @@
                     ui.warn(_("cannot find source for copied file: %s@%s\n")
                             % (filename, change))
 
-            self.changeset[change] = c
-            self.files[change] = files
-            self.copies[change] = copies
+            changeset[change] = c
+            files_map[change] = files
+            copies_map[change] = copies
             lastid = change
 
-        if lastid and len(self.changeset) > 0:
-            self.heads = [lastid]
+        if lastid and len(changeset) > 0:
+            heads = [lastid]
+
+        return {
+            'changeset': changeset,
+            'files': files_map,
+            'copies': copies_map,
+            'heads': heads,
+            'depotname': depotname,
+        }
+
+    def _parse_once(self, ui, path):
+        d = self._parse(ui, path)
+        self.changeset = d['changeset']
+        self.heads = d['heads']
+        self.files = d['files']
+        self.copies = d['copies']
+        self.depotname = d['depotname']
 
     def getheads(self):
         return self.heads


More information about the Mercurial-devel mailing list