Fix initial multi-branch import.
[gitweb.git] / contrib / fast-import / git-p4
index cababc7fc898bd34ae8ceb7102adda4dfa7ceacd..e527734be5af0fef713a8d8241de57cb5f2cbb5e 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]"
@@ -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
@@ -1175,7 +1183,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"
@@ -1252,6 +1260,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
@@ -1351,9 +1362,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