Code

More cleanups and speedups for labels and branches
[git.git] / contrib / fast-import / git-p4
index e993d3f6933f701f54ff610b4ba19fdfe09fbc2e..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,17 +612,18 @@ 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):
-        # map from branch depot path to parent branch
-        self.knownBranches = {}
+        self.projectName = self.depotPath[self.depotPath[:-1].rfind("/") + 1:]
 
         for info in p4CmdList("branches"):
             details = p4Cmd("branch -o %s" % info["branch"])
@@ -629,16 +639,34 @@ class P4Sync(Command):
                 if source.startswith(self.depotPath) and destination.startswith(self.depotPath):
                     source = source[len(self.depotPath):-4]
                     destination = destination[len(self.depotPath):-4]
-                    self.knownBranches[destination] = source
-                    self.knownBranches[source] = source
+                    if destination not in self.knownBranches:
+                        self.knownBranches[destination] = source
+                    if source not in self.knownBranches:
+                        self.knownBranches[source] = source
+
+    def listExistingP4GitBranches(self):
+        self.p4BranchesInGit = []
+
+        for line in mypopen("git rev-parse --symbolic --remotes").readlines():
+            if line.startswith("p4/") and line != "p4/HEAD\n":
+                branch = line[3:-1]
+                self.p4BranchesInGit.append(branch)
+                self.initialParents["refs/remotes/p4/" + branch] = parseRevision(line[:-1])
 
     def run(self, args):
         self.depotPath = ""
         self.changeRange = ""
         self.initialParent = ""
         self.previousDepotPath = ""
+        # map from branch depot path to parent branch
+        self.knownBranches = {}
+        self.initialParents = {}
+
+        self.listExistingP4GitBranches()
+
+        if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master") and not self.detectBranches:
+            ### needs to be ported to multi branch import
 
-        if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master"):
             print "Syncing with origin first as requested by calling git fetch origin"
             system("git fetch origin")
             [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin"))
@@ -662,7 +690,8 @@ class P4Sync(Command):
                 system("git symbolic-ref refs/remotes/p4/HEAD %s" % self.branch)
 
         if len(args) == 0:
-            if not gitBranchExists(self.branch) and gitBranchExists("origin"):
+            if not gitBranchExists(self.branch) and gitBranchExists("origin") and not self.detectBranches:
+                ### needs to be ported to multi branch import
                 if not self.silent:
                     print "Creating %s branch in git repository based on origin" % self.branch
                 branch = self.branch
@@ -670,9 +699,30 @@ class P4Sync(Command):
                     branch = "refs/heads/" + branch
                 system("git update-ref %s origin" % branch)
 
-            [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
-            if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
-                p4Change = int(p4Change) + 1
+            if self.verbose:
+                print "branches: %s" % self.p4BranchesInGit
+
+            p4Change = 0
+            for branch in self.p4BranchesInGit:
+                depotPath, change = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("refs/remotes/p4/" + branch))
+
+                if self.verbose:
+                    print "path %s change %s" % (depotPath, change)
+
+                if len(depotPath) > 0 and len(change) > 0:
+                    change = int(change) + 1
+                    p4Change = max(p4Change, change)
+
+                    if len(self.previousDepotPath) == 0:
+                        self.previousDepotPath = depotPath
+                    else:
+                        i = 0
+                        l = min(len(self.previousDepotPath), len(depotPath))
+                        while i < l and self.previousDepotPath[i] == depotPath[i]:
+                            i = i + 1
+                        self.previousDepotPath = self.previousDepotPath[:i]
+
+            if p4Change > 0:
                 self.depotPath = self.previousDepotPath
                 self.changeRange = "@%s,#head" % p4Change
                 self.initialParent = parseRevision(self.branch)
@@ -729,6 +779,13 @@ class P4Sync(Command):
 
         if self.detectBranches:
             self.getBranchMapping();
+            if self.verbose:
+                print "p4-git branches: %s" % self.p4BranchesInGit
+                print "initial parents: %s" % self.initialParents
+            for b in self.p4BranchesInGit:
+                if b != "master":
+                    b = b[len(self.projectName):]
+                self.createdBranches.add(b)
 
         self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
 
@@ -783,6 +840,8 @@ class P4Sync(Command):
 
                 changes.sort()
             else:
+                if self.verbose:
+                    print "Getting p4 changes for %s...%s" % (self.depotPath, self.changeRange)
                 output = mypopen("p4 changes %s...%s" % (self.depotPath, self.changeRange)).readlines()
 
                 for line in output:
@@ -806,26 +865,51 @@ 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
 
-                            if branch not in self.createdBranches :
+                            if branch not in self.createdBranches:
                                 self.createdBranches.add(branch)
                                 parent = self.knownBranches[branch]
                                 if parent == branch:
                                     parent = ""
+                                elif self.verbose:
+                                    print "parent determined through known branches: %s" % parent
+
+                            # main branch? use master
+                            if branch == "main":
+                                branch = "master"
+                            else:
+                                branch = self.projectName + branch
+
+                            if parent == "main":
+                                parent = "master"
+                            elif len(parent) > 0:
+                                parent = self.projectName + parent
 
                             branch = "refs/remotes/p4/" + branch
                             if len(parent) > 0:
                                 parent = "refs/remotes/p4/" + parent
-                            self.commit(description, files, branch, branchPrefix, parent)
+
+                            if self.verbose:
+                                print "looking for initial parent for %s; current parent is %s" % (branch, parent)
+
+                            if len(parent) == 0 and branch in self.initialParents:
+                                parent = self.initialParents[branch]
+                                del self.initialParents[branch]
+
+                            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:
@@ -837,9 +921,10 @@ class P4Sync(Command):
 
 
         self.gitStream.close()
+        if importProcess.wait() != 0:
+            die("fast-import failed: %s" % self.gitError.read())
         self.gitOutput.close()
         self.gitError.close()
-        importProcess.wait()
 
         return True
 
@@ -906,8 +991,11 @@ class P4Clone(P4Sync):
         if not P4Sync.run(self, [depotPath]):
             return False
         if self.branch != "master":
-            system("git branch master p4")
-            system("git checkout -f")
+            if gitBranchExists("refs/remotes/p4/master"):
+                system("git branch master refs/remotes/p4/master")
+                system("git checkout -f")
+            else:
+                print "Could not detect main branch. No checkout/master branch created."
         return True
 
 class HelpFormatter(optparse.IndentedHelpFormatter):