Make it possible to specify the HEAD for the internal findUpstreamBranchPoint function.
[gitweb.git] / contrib / fast-import / git-p4
index e527734be5af0fef713a8d8241de57cb5f2cbb5e..54a05eb99c3eb747fcf26cfa3fc51a12f9055bd3 100755 (executable)
@@ -168,12 +168,12 @@ def gitBranchExists(branch):
 def gitConfig(key):
     return read_pipe("git config %s" % key, ignore_error=True).strip()
 
-def findUpstreamBranchPoint():
+def findUpstreamBranchPoint(head = "HEAD"):
     settings = None
     branchPoint = ""
     parent = 0
     while parent < 65535:
-        commit = "HEAD~%s" % parent
+        commit = head + "~%s" % parent
         log = extractLogMessageFromGitCommit(commit)
         settings = extractSettingsGitLog(log)
         if not settings.has_key("depot-paths"):
@@ -700,6 +700,7 @@ class P4Sync(Command):
                     if branch not in branches:
                         branches[branch] = []
                     branches[branch].append(file)
+                    break
 
         return branches
 
@@ -711,9 +712,23 @@ class P4Sync(Command):
         if not files:
             return
 
-        filedata = p4CmdList('print %s' % ' '.join(['"%s#%s"' % (f['path'],
-                                                                 f['rev'])
-                                                    for f in files]))
+        # We cannot put all the files on the command line
+        # OS have limitations on the max lenght of arguments
+        # POSIX says it's 4096 bytes, default for Linux seems to be 130 K.
+        # and all OS from the table below seems to be higher than POSIX.
+        # See http://www.in-ulm.de/~mascheck/various/argmax/
+        argmax = min(4000, os.sysconf('SC_ARG_MAX'))
+        chunk = ''
+        filedata = []
+        for i in xrange(len(files)):
+            f = files[i]
+            chunk += '"%s#%s" ' % (f['path'], f['rev'])
+            if len(chunk) > argmax or i == len(files)-1:
+                data = p4CmdList('print %s' % chunk)
+                if "p4ExitCode" in data[0]:
+                    die("Problems executing p4. Error: [%d]." % (data[0]['p4ExitCode']));
+                filedata.extend(data)
+                chunk = ''
 
         j = 0;
         contents = {}
@@ -924,6 +939,8 @@ class P4Sync(Command):
             return p
 
     def getBranchMapping(self):
+        lostAndFoundBranches = set()
+
         for info in p4CmdList("branches"):
             details = p4Cmd("branch -o %s" % info["branch"])
             viewIdx = 0
@@ -939,10 +956,23 @@ class P4Sync(Command):
                 if source.startswith(self.depotPaths[0]) and destination.startswith(self.depotPaths[0]):
                     source = source[len(self.depotPaths[0]):-4]
                     destination = destination[len(self.depotPaths[0]):-4]
-                    if destination not in self.knownBranches:
-                        self.knownBranches[destination] = source
+
+                    if destination in self.knownBranches:
+                        if not self.silent:
+                            print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
+                            print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
+                        continue
+
+                    self.knownBranches[destination] = source
+
+                    lostAndFoundBranches.discard(destination)
+
                     if source not in self.knownBranches:
-                        self.knownBranches[source] = source
+                        lostAndFoundBranches.add(source)
+
+
+        for branch in lostAndFoundBranches:
+            self.knownBranches[branch] = branch
 
     def listExistingP4GitBranches(self):
         self.p4BranchesInGit = []
@@ -1435,6 +1465,31 @@ class P4Clone(P4Sync):
 
         return True
 
+class P4Branches(Command):
+    def __init__(self):
+        Command.__init__(self)
+        self.options = [ ]
+        self.description = ("Shows the git branches that hold imports and their "
+                            + "corresponding perforce depot paths")
+        self.verbose = False
+
+    def run(self, args):
+        cmdline = "git rev-parse --symbolic "
+        cmdline += " --remotes"
+
+        for line in read_pipe_lines(cmdline):
+            line = line.strip()
+
+            if not line.startswith('p4/') or line == "p4/HEAD":
+                continue
+            branch = line
+
+            log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
+            settings = extractSettingsGitLog(log)
+
+            print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
+        return True
+
 class HelpFormatter(optparse.IndentedHelpFormatter):
     def __init__(self):
         optparse.IndentedHelpFormatter.__init__(self)
@@ -1459,7 +1514,8 @@ commands = {
     "sync" : P4Sync,
     "rebase" : P4Rebase,
     "clone" : P4Clone,
-    "rollback" : P4RollBack
+    "rollback" : P4RollBack,
+    "branches" : P4Branches
 }