Code

git-p4: Cleanup, used common function for listing imported p4 branches
[git.git] / contrib / fast-import / git-p4
index 6c199296d39869aae435e95590b776c6ded705a1..e3404ca853c8b54f1a0c64be7038d282746652a7 100755 (executable)
@@ -63,21 +63,34 @@ def system(cmd):
     if os.system(cmd) != 0:
         die("command failed: %s" % cmd)
 
-def p4CmdList(cmd):
+def p4CmdList(cmd, stdin=None, stdin_mode='w+b'):
     cmd = "p4 -G %s" % cmd
     if verbose:
         sys.stderr.write("Opening pipe: %s\n" % cmd)
-    pipe = os.popen(cmd, "rb")
+
+    # Use a temporary file to avoid deadlocks without
+    # subprocess.communicate(), which would put another copy
+    # of stdout into memory.
+    stdin_file = None
+    if stdin is not None:
+        stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
+        stdin_file.write(stdin)
+        stdin_file.flush()
+        stdin_file.seek(0)
+
+    p4 = subprocess.Popen(cmd, shell=True,
+                          stdin=stdin_file,
+                          stdout=subprocess.PIPE)
 
     result = []
     try:
         while True:
-            entry = marshal.load(pipe)
+            entry = marshal.load(p4.stdout)
             result.append(entry)
     except EOFError:
         pass
-    exitCode = pipe.close()
-    if exitCode != None:
+    exitCode = p4.wait()
+    if exitCode != 0:
         entry = {}
         entry["p4ExitCode"] = exitCode
         result.append(entry)
@@ -168,6 +181,56 @@ def gitBranchExists(branch):
 def gitConfig(key):
     return read_pipe("git config %s" % key, ignore_error=True).strip()
 
+def p4BranchesInGit(branchesAreInRemotes = True):
+    branches = {}
+
+    cmdline = "git rev-parse --symbolic "
+    if branchesAreInRemotes:
+        cmdline += " --remotes"
+    else:
+        cmdline += " --branches"
+
+    for line in read_pipe_lines(cmdline):
+        line = line.strip()
+
+        ## only import to p4/
+        if not line.startswith('p4/') or line == "p4/HEAD":
+            continue
+        branch = line
+
+        # strip off p4
+        branch = re.sub ("^p4/", "", line)
+
+        branches[branch] = parseRevision(line)
+    return branches
+
+def findUpstreamBranchPoint(head = "HEAD"):
+    branches = p4BranchesInGit()
+    # map from depot-path to branch name
+    branchByDepotPath = {}
+    for branch in branches.keys():
+        tip = branches[branch]
+        log = extractLogMessageFromGitCommit(tip)
+        settings = extractSettingsGitLog(log)
+        if settings.has_key("depot-paths"):
+            paths = ",".join(settings["depot-paths"])
+            branchByDepotPath[paths] = "remotes/p4/" + branch
+
+    settings = None
+    parent = 0
+    while parent < 65535:
+        commit = head + "~%s" % parent
+        log = extractLogMessageFromGitCommit(commit)
+        settings = extractSettingsGitLog(log)
+        if settings.has_key("depot-paths"):
+            paths = ",".join(settings["depot-paths"])
+            if branchByDepotPath.has_key(paths):
+                return [branchByDepotPath[paths], settings]
+
+        parent = parent + 1
+
+    return ["", settings]
+
 class Command:
     def __init__(self):
         self.usage = "usage: %prog [options]"
@@ -391,10 +454,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 +557,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 +678,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 +741,7 @@ class P4Sync(Command):
                     if branch not in branches:
                         branches[branch] = []
                     branches[branch].append(file)
+                    break
 
         return branches
 
@@ -703,9 +753,13 @@ class P4Sync(Command):
         if not files:
             return
 
-        filedata = p4CmdList('print %s' % ' '.join(['"%s#%s"' % (f['path'],
-                                                                 f['rev'])
-                                                    for f in files]))
+        filedata = p4CmdList('-x - print',
+                             stdin='\n'.join(['%s#%s' % (f['path'], f['rev'])
+                                              for f in files]),
+                             stdin_mode='w+')
+        if "p4ExitCode" in filedata[0]:
+            die("Problems executing p4. Error: [%d]."
+                % (filedata[0]['p4ExitCode']));
 
         j = 0;
         contents = {}
@@ -916,6 +970,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,33 +987,30 @@ 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 source not in self.knownBranches:
-                        self.knownBranches[source] = source
 
-    def listExistingP4GitBranches(self):
-        self.p4BranchesInGit = []
+                    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
 
-        cmdline = "git rev-parse --symbolic "
-        if self.importIntoRemotes:
-            cmdline += " --remotes"
-        else:
-            cmdline += " --branches"
+                    self.knownBranches[destination] = source
 
-        for line in read_pipe_lines(cmdline):
-            line = line.strip()
+                    lostAndFoundBranches.discard(destination)
 
-            ## only import to p4/
-            if not line.startswith('p4/') or line == "p4/HEAD":
-                continue
-            branch = line
+                    if source not in self.knownBranches:
+                        lostAndFoundBranches.add(source)
 
-            # strip off p4
-            branch = re.sub ("^p4/", "", line)
 
-            self.p4BranchesInGit.append(branch)
-            self.initialParents[self.refPrefix + branch] = parseRevision(line)
+        for branch in lostAndFoundBranches:
+            self.knownBranches[branch] = branch
+
+    def listExistingP4GitBranches(self):
+        # branches holds mapping from name to commit
+        branches = p4BranchesInGit(self.importIntoRemotes)
+        self.p4BranchesInGit = branches.keys()
+        for branch in branches.keys():
+            self.initialParents[self.refPrefix + branch] = branches[branch]
 
     def createOrUpdateBranchesFromOrigin(self):
         if not self.silent:
@@ -1354,9 +1407,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
 
@@ -1419,6 +1480,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)
@@ -1443,7 +1529,8 @@ commands = {
     "sync" : P4Sync,
     "rebase" : P4Rebase,
     "clone" : P4Clone,
-    "rollback" : P4RollBack
+    "rollback" : P4RollBack,
+    "branches" : P4Branches
 }