Code

More cleanups and speedups for labels and branches
[git.git] / contrib / fast-import / git-p4
index 2c1dc9e2b3431a8f0bfa3e20d0f5672c049f763e..49114d2c6f3e377b2805f0e5e3442d71fb306f41 100755 (executable)
@@ -461,15 +461,32 @@ class P4Sync(Command):
             fnum = fnum + 1
         return files
 
-    def branchesForCommit(self, files):
-        branches = Set()
+    def splitFilesIntoBranches(self, commit):
+        branches = {}
 
-        for file in files:
-            path = file["path"][len(self.depotPath):]
+        fnum = 0
+        while commit.has_key("depotFile%s" % fnum):
+            path =  commit["depotFile%s" % fnum]
+            if not path.startswith(self.depotPath):
+    #            if not self.silent:
+    #                print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.depotPath, change)
+                fnum = fnum + 1
+                continue
+
+            file = {}
+            file["path"] = path
+            file["rev"] = commit["rev%s" % fnum]
+            file["action"] = commit["action%s" % fnum]
+            file["type"] = commit["type%s" % fnum]
+            fnum = fnum + 1
+
+            relPath = path[len(self.depotPath):]
 
             for branch in self.knownBranches.keys():
-                if path.startswith(branch):
-                    branches.add(branch)
+                if relPath.startswith(branch):
+                    if branch not in branches:
+                        branches[branch] = []
+                    branches[branch].append(file)
 
         return branches
 
@@ -540,6 +557,8 @@ class P4Sync(Command):
             label = self.labels[change]
             labelDetails = label[0]
             labelRevisions = label[1]
+            if self.verbose:
+                print "Change %s is labelled %s" % (change, labelDetails)
 
             files = p4CmdList("files %s...@%s" % (branchPrefix, change))
 
@@ -574,16 +593,6 @@ class P4Sync(Command):
                 if not self.silent:
                     print "Tag %s does not match with change %s: file count is different." % (labelDetails["label"], change)
 
-    def extractFilesInCommitToBranch(self, files, branchPrefix):
-        newFiles = []
-
-        for file in files:
-            path = file["path"]
-            if path.startswith(branchPrefix):
-                newFiles.append(file)
-
-        return newFiles
-
     def getUserMap(self):
         self.users = {}
 
@@ -603,13 +612,15 @@ class P4Sync(Command):
             label = output["label"]
             revisions = {}
             newestChange = 0
-            for file in p4CmdList("files //...@%s" % label):
+            if self.verbose:
+                print "Querying files for label %s" % label
+            for file in p4CmdList("files %s...@%s" % (self.depotPath, label)):
                 revisions[file["depotFile"]] = file["rev"]
                 change = int(file["change"])
                 if change > newestChange:
                     newestChange = change
 
-            self.labels[newestChange] = [output, revisions]
+            self.labels[int(newestChange)] = [output, revisions]
 
     def getBranchMapping(self):
         self.projectName = self.depotPath[self.depotPath[:-1].rfind("/") + 1:]
@@ -713,8 +724,7 @@ class P4Sync(Command):
 
             if p4Change > 0:
                 self.depotPath = self.previousDepotPath
-                #self.changeRange = "@%s,#head" % p4Change
-                self.changeRange = "@%s,%s" % (p4Change, p4Change + 10)
+                self.changeRange = "@%s,#head" % p4Change
                 self.initialParent = parseRevision(self.branch)
                 if not self.silent:
                     print "Performing incremental import into %s git branch" % self.branch
@@ -855,14 +865,14 @@ class P4Sync(Command):
                 cnt = cnt + 1
 
                 try:
-                    files = self.extractFilesFromCommit(description)
                     if self.detectBranches:
-                        for branch in self.branchesForCommit(files):
+                        branches = self.splitFilesIntoBranches(description)
+                        for branch in branches.keys():
                             branchPrefix = self.depotPath + branch + "/"
 
                             parent = ""
 
-                            filesForCommit = self.extractFilesInCommitToBranch(files, branch)
+                            filesForCommit = branches[branch]
 
                             if self.verbose:
                                 print "branch is %s" % branch
@@ -897,8 +907,9 @@ class P4Sync(Command):
                                 parent = self.initialParents[branch]
                                 del self.initialParents[branch]
 
-                            self.commit(description, files, branch, branchPrefix, parent)
+                            self.commit(description, filesForCommit, branch, branchPrefix, parent)
                     else:
+                        files = self.extractFilesFromCommit(description)
                         self.commit(description, files, self.branch, self.depotPath, self.initialParent)
                         self.initialParent = ""
                 except IOError: