Fix creating the remotes/p4 branches based on origin/* for the multi-branch import
[gitweb.git] / contrib / fast-import / git-p4
index b2341b7ec4592f0457adfddc6e4f711191b9e36a..b587e79b482975cff1e366a1d3c33ea95e944a3a 100755 (executable)
@@ -540,6 +540,7 @@ class P4Sync(Command):
         self.verbose = False
         self.importIntoRemotes = True
         self.maxChanges = ""
+        self.isWindows = (platform.system() == "Windows")
 
     def p4File(self, depotPath):
         return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
@@ -647,6 +648,9 @@ class P4Sync(Command):
 
                 data = self.p4File(depotPath)
 
+                if self.isWindows and file["type"].endswith("text"):
+                    data = data.replace("\r\n", "\n")
+
                 self.gitStream.write("M %s inline %s\n" % (mode, relPath))
                 self.gitStream.write("data %s\n" % len(data))
                 self.gitStream.write(data)
@@ -697,6 +701,8 @@ class P4Sync(Command):
                     print "Tag %s does not match with change %s: file count is different." % (labelDetails["label"], change)
 
     def getUserMapFromPerforceServer(self):
+        if self.userMapFromPerforceServer:
+            return
         self.users = {}
 
         for output in p4CmdList("users"):
@@ -708,9 +714,11 @@ class P4Sync(Command):
         for user in self.users.keys():
             cache.write("%s\t%s\n" % (user, self.users[user]))
         cache.close();
+        self.userMapFromPerforceServer = True
 
     def loadUserMapFromCache(self):
         self.users = {}
+        self.userMapFromPerforceServer = False
         try:
             cache = open(gitdir + "/p4-usercache.txt", "rb")
             lines = cache.readlines()
@@ -787,6 +795,20 @@ class P4Sync(Command):
             self.p4BranchesInGit.append(branch)
             self.initialParents[self.refPrefix + branch] = parseRevision(line[:-1])
 
+    def createBranchesFromOrigin(self):
+        if not self.silent:
+            print "Creating branch(es) in %s based on origin branch(es)" % self.refPrefix
+
+        for line in mypopen("git rev-parse --symbolic --remotes"):
+            if (not line.startswith("origin/")) or line.endswith("HEAD\n"):
+                continue
+            headName = line[len("origin/"):-1]
+            remoteHead = self.refPrefix + headName
+            if not os.path.exists(gitdir + "/" + remoteHead):
+                if self.verbose:
+                    print "creating %s" % remoteHead
+                system("git update-ref %s origin/%s" % (remoteHead, headName))
+
     def run(self, args):
         self.depotPath = ""
         self.changeRange = ""
@@ -831,19 +853,16 @@ class P4Sync(Command):
 
         # this needs to be called after the conversion from heads/p4 to remotes/p4/master
         self.listExistingP4GitBranches()
-        if len(self.p4BranchesInGit) > 1 and not self.silent:
-            print "Importing from/into multiple branches"
+        if len(self.p4BranchesInGit) > 1:
+            if not self.silent:
+                print "Importing from/into multiple branches"
             self.detectBranches = True
 
         if len(args) == 0:
-            if not gitBranchExists(self.branch) and gitBranchExists("origin") and not self.detectBranches:
-                ### needs to be ported to multi branch import
-                if not self.silent:
-                    print "Creating %s branch in git repository based on origin" % self.branch
-                branch = self.branch
-                if not branch.startswith("refs"):
-                    branch = "refs/heads/" + branch
-                system("git update-ref %s origin" % branch)
+            if len(self.p4BranchesInGit) == 0:
+                self.createBranchesFromOrigin()
+                self.listExistingP4GitBranches()
+                return True
 
             if self.verbose:
                 print "branches: %s" % self.p4BranchesInGit