Fix the branch mapping detection to be independent from the order of the "p4 branches" output.
[gitweb.git] / contrib / fast-import / git-p4
index 21f9ba7e07e8eb577c9ef722fd30f7f30ce9f652..3b6d8a09d1fabcf877b5f9967eaadf50c0b8df2e 100755 (executable)
@@ -168,6 +168,28 @@ def gitBranchExists(branch):
 def gitConfig(key):
     return read_pipe("git config %s" % key, ignore_error=True).strip()
 
+def findUpstreamBranchPoint():
+    settings = None
+    branchPoint = ""
+    parent = 0
+    while parent < 65535:
+        commit = "HEAD~%s" % parent
+        log = extractLogMessageFromGitCommit(commit)
+        settings = extractSettingsGitLog(log)
+        if not settings.has_key("depot-paths"):
+            parent = parent + 1
+            continue
+
+        names = read_pipe_lines("git name-rev \"--refs=refs/remotes/p4/*\" \"%s\"" % commit)
+        if len(names) <= 0:
+            continue
+
+        # strip away the beginning of 'HEAD~42 refs/remotes/p4/foo'
+        branchPoint = names[0].strip()[len(commit) + 1:]
+        break
+
+    return [branchPoint, settings]
+
 class Command:
     def __init__(self):
         self.usage = "usage: %prog [options]"
@@ -494,25 +516,10 @@ class P4Submit(Command):
         else:
             return False
 
-        depotPath = ""
-        parent = 0
-        while parent < 65535:
-            commit = "HEAD~%s" % parent
-            log = extractLogMessageFromGitCommit(commit)
-            settings = extractSettingsGitLog(log)
-            if not settings.has_key("depot-paths"):
-                parent = parent + 1
-                continue
-
-            depotPath = settings['depot-paths'][0]
-
-            if len(self.origin) == 0:
-                names = read_pipe_lines("git name-rev '--refs=refs/remotes/p4/*' '%s'" % commit)
-                if len(names) > 0:
-                    # strip away the beginning of 'HEAD~42 refs/remotes/p4/foo'
-                    self.origin = names[0].strip()[len(commit) + 1:]
-
-            break
+        [upstream, settings] = findUpstreamBranchPoint()
+        depotPath = settings['depot-paths'][0]
+        if len(self.origin) == 0:
+            self.origin = upstream
 
         if self.verbose:
             print "Origin branch is " + self.origin
@@ -630,6 +637,7 @@ class P4Sync(Command):
         self.isWindows = (platform.system() == "Windows")
         self.keepRepoPath = False
         self.depotPaths = None
+        self.p4BranchesInGit = []
 
         if gitConfig("git-p4.syncFromOrigin") == "false":
             self.syncWithOrigin = False
@@ -692,6 +700,7 @@ class P4Sync(Command):
                     if branch not in branches:
                         branches[branch] = []
                     branches[branch].append(file)
+                    break
 
         return branches
 
@@ -703,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 = {}
@@ -916,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
@@ -931,10 +956,17 @@ 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
+
+                    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 = []
@@ -1354,9 +1386,17 @@ class P4Rebase(Command):
     def run(self, args):
         sync = P4Sync()
         sync.run([])
-        print "Rebasing the current branch"
+
+        [upstream, settings] = findUpstreamBranchPoint()
+        if len(upstream) == 0:
+            die("Cannot find upstream branchpoint for rebase")
+
+        # the branchpoint may be p4/foo~3, so strip off the parent
+        upstream = re.sub("~[0-9]+$", "", upstream)
+
+        print "Rebasing the current branch onto %s" % upstream
         oldHead = read_pipe("git rev-parse HEAD").strip()
-        system("git rebase p4")
+        system("git rebase %s" % upstream)
         system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
         return True