[PATCH] convert: handle copies when converting from Perforce (issue4744)

Eugene Baranov eug.baranov at gmail.com
Wed Jul 8 13:53:15 CDT 2015


# HG changeset patch
# User Eugene Baranov <eug.baranov at gmail.com>
# Date 1436375127 -3600
#      Wed Jul 08 18:05:27 2015 +0100
# Branch stable
# Node ID 3dbbd0a980aa0f62805a86f8e305271d687bd313
# Parent  84e7b25bb04440a471ad7e22f0d607ed6c948181
convert: handle copies when converting from Perforce (issue4744)

diff -r 84e7b25bb044 -r 3dbbd0a980aa hgext/convert/p4.py
--- a/hgext/convert/p4.py	Fri Jul 03 18:10:58 2015 +0100
+++ b/hgext/convert/p4.py	Wed Jul 08 18:05:27 2015 +0100
@@ -36,11 +36,13 @@
         self.heads = {}
         self.changeset = {}
         self.files = {}
+        self.copies = {}
         self.tags = {}
         self.lastbranch = {}
         self.parent = {}
         self.encoding = "latin_1"
         self.depotname = {}           # mapping from local name to depot name
+        self.localname = {} # mapping from depot name to local name
         self.re_type = re.compile(
             "([a-z]+)?(text|binary|symlink|apple|resource|unicode|utf\d+)"
             "(\+\w+)?$")
@@ -125,6 +127,8 @@
                        extra={"p4": change})
 
             files = []
+            copies = {}
+            copiedfiles = []
             i = 0
             while ("depotFile%d" % i) in d and ("rev%d" % i) in d:
                 oldname = d["depotFile%d" % i]
@@ -136,9 +140,47 @@
                 if filename:
                     files.append((filename, d["rev%d" % i]))
                     self.depotname[filename] = oldname
+                    if (d.get("action%d" % i) == "move/add"):
+                        copiedfiles.append(filename)
+                    self.localname[oldname] = filename
                 i += 1
+
+            # Collect information about copied files
+            for filename in copiedfiles:
+                oldname = self.depotname[filename]
+
+                flcmd = 'p4 -G filelog %s' \
+                      % util.shellquote(oldname)
+                flstdout = util.popen(flcmd, mode='rb')
+
+                copiedfilename = None
+                for d in loaditer(flstdout):
+                    copiedoldname = None
+
+                    i = 0
+                    while ("change%d" % i) in d:
+                        if (d["change%d" % i] == change and
+                            d["action%d" % i] == "move/add"):
+                            j = 0
+                            while ("file%d,%d" % (i, j)) in d:
+                                if d["how%d,%d" % (i, j)] == "moved from":
+                                    copiedoldname = d["file%d,%d" % (i, j)]
+                                    break
+                                j += 1
+                        i += 1
+
+                    if copiedoldname and copiedoldname in self.localname:
+                        copiedfilename = self.localname[copiedoldname]
+                        break
+
+                if copiedfilename:
+                    copies[filename] = copiedfilename
+                else:
+                    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
             lastid = change
 
         if lastid:
@@ -195,7 +237,7 @@
     def getchanges(self, rev, full):
         if full:
             raise util.Abort(_("convert from p4 do not support --full"))
-        return self.files[rev], {}, set()
+        return self.files[rev], self.copies[rev], set()
 
     def getcommit(self, rev):
         return self.changeset[rev]


More information about the Mercurial-devel mailing list