Merge branch 'master' of git://git.kernel.org/pub/scm/gitk/gitk
[gitweb.git] / contrib / fast-import / git-p4
index 9d52eada425424f1dedcd8704128067fcc5412dc..54a05eb99c3eb747fcf26cfa3fc51a12f9055bd3 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(head = "HEAD"):
+    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]"
@@ -391,10 +413,10 @@ class P4Submit(Command):
         system(applyPatchCmd)
 
         for f in filesToAdd:
-            system("p4 add %s" % f)
+            system("p4 add \"%s\"" % f)
         for f in filesToDelete:
-            system("p4 revert %s" % f)
-            system("p4 delete %s" % f)
+            system("p4 revert \"%s\"" % f)
+            system("p4 delete \"%s\"" % f)
 
         logMessage = ""
         if not self.directSubmit:
@@ -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 = {}
@@ -765,12 +788,11 @@ class P4Sync(Command):
 
         self.gitStream.write("data <<EOT\n")
         self.gitStream.write(details["desc"])
-        self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s: "
-                             "options = %s]\n"
-                             % (','.join (branchPrefixes), details["change"],
-                                details['options']
-                                ))
-        self.gitStream.write("EOT\n\n")
+        self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s"
+                             % (','.join (branchPrefixes), details["change"]))
+        if len(details['options']) > 0:
+            self.gitStream.write(": options = %s" % details['options'])
+        self.gitStream.write("]\nEOT\n\n")
 
         if len(parent) > 0:
             if self.verbose:
@@ -909,9 +931,16 @@ class P4Sync(Command):
 
     def guessProjectName(self):
         for p in self.depotPaths:
-            return p [p.strip().rfind("/") + 1:]
+            if p.endswith("/"):
+                p = p[:-1]
+            p = p[p.strip().rfind("/") + 1:]
+            if not p.endswith("/"):
+               p += "/"
+            return p
 
     def getBranchMapping(self):
+        lostAndFoundBranches = set()
+
         for info in p4CmdList("branches"):
             details = p4Cmd("branch -o %s" % info["branch"])
             viewIdx = 0
@@ -927,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 = []
@@ -1023,6 +1065,8 @@ class P4Sync(Command):
         self.knownBranches = {}
         self.initialParents = {}
         self.hasOrigin = gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
+        if not self.syncWithOrigin:
+            self.hasOrigin = False
 
         if self.importIntoRemotes:
             self.refPrefix = "refs/remotes/p4/"
@@ -1169,7 +1213,7 @@ class P4Sync(Command):
         self.gitError = importProcess.stderr
 
         if self.revision:
-            print "Doing initial import of %s from revision %s" % (' '.join(self.depotPaths), self.revision)
+            print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), self.revision, self.branch)
 
             details = { "user" : "git perforce import user", "time" : int(time.time()) }
             details["desc"] = ("Initial import of %s from the state at revision %s"
@@ -1246,6 +1290,9 @@ class P4Sync(Command):
                     print "No changes to import!"
                 return True
 
+            if not self.silent and not self.detectBranches:
+                print "Import destination: %s" % self.branch
+
             self.updatedBranches = set()
 
             cnt = 1
@@ -1307,7 +1354,7 @@ class P4Sync(Command):
                                 parent = self.initialParents[branch]
                                 del self.initialParents[branch]
 
-                            self.commit(description, filesForCommit, branch, branchPrefix, parent)
+                            self.commit(description, filesForCommit, branch, [branchPrefix], parent)
                     else:
                         files = self.extractFilesFromCommit(description)
                         self.commit(description, files, self.branch, self.depotPaths,
@@ -1345,9 +1392,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
 
@@ -1394,7 +1449,8 @@ class P4Clone(P4Sync):
             self.cloneDestination = self.defaultDestination(args)
 
         print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
-        os.makedirs(self.cloneDestination)
+        if not os.path.exists(self.cloneDestination):
+            os.makedirs(self.cloneDestination)
         os.chdir(self.cloneDestination)
         system("git init")
         self.gitdir = os.getcwd() + "/.git"
@@ -1409,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)
@@ -1433,7 +1514,8 @@ commands = {
     "sync" : P4Sync,
     "rebase" : P4Rebase,
     "clone" : P4Clone,
-    "rollback" : P4RollBack
+    "rollback" : P4RollBack,
+    "branches" : P4Branches
 }