Code

Bite the bullet and automatically convert old style refs/heads/p4 repositories
[git.git] / contrib / fast-import / git-p4
index 239304857d894e3612d71d90185606955bfa5d22..cb9961a571eb8dacb43f085a803d942c83f1e245 100755 (executable)
@@ -69,6 +69,9 @@ def isValidGitDir(path):
         return True;
     return False
 
+def parseRevision(ref):
+    return mypopen("git rev-parse %s" % ref).read()[:-1]
+
 def system(cmd):
     if os.system(cmd) != 0:
         die("command failed: %s" % cmd)
@@ -126,44 +129,6 @@ class P4Debug(Command):
             print output
         return True
 
-class P4CleanTags(Command):
-    def __init__(self):
-        Command.__init__(self)
-        self.options = [
-#                optparse.make_option("--branch", dest="branch", default="refs/heads/master")
-        ]
-        self.description = "A tool to remove stale unused tags from incremental perforce imports."
-    def run(self, args):
-        branch = currentGitBranch()
-        print "Cleaning out stale p4 import tags..."
-        sout, sin, serr = popen2.popen3("git name-rev --tags `git rev-parse %s`" % branch)
-        output = sout.read()
-        try:
-            tagIdx = output.index(" tags/p4/")
-        except:
-            print "Cannot find any p4/* tag. Nothing to do."
-            sys.exit(0)
-
-        try:
-            caretIdx = output.index("^")
-        except:
-            caretIdx = len(output) - 1
-        rev = int(output[tagIdx + 9 : caretIdx])
-
-        allTags = mypopen("git tag -l p4/").readlines()
-        for i in range(len(allTags)):
-            allTags[i] = int(allTags[i][3:-1])
-
-        allTags.sort()
-
-        allTags.remove(rev)
-
-        for rev in allTags:
-            print mypopen("git tag -d p4/%s" % rev).read()
-
-        print "%s tags removed." % len(allTags)
-        return True
-
 class P4Submit(Command):
     def __init__(self):
         Command.__init__(self)
@@ -863,26 +828,40 @@ class P4Sync(Command):
         self.changeRange = ""
         self.initialParent = ""
         self.previousDepotPath = ""
+        # importing into default remotes/p4/* layout?
+        defaultImport = False
 
         if len(self.branch) == 0:
-            self.branch = "p4"
+            self.branch = "refs/remotes/p4/master"
+            if gitBranchExists("refs/heads/p4"):
+                system("git update-ref %s refs/heads/p4" % self.branch)
+                system("git symbolic-ref refs/remotes/p4/HEAD refs/remotes/p4/master")
+                system("git branch -D p4");
+            else:
+                defaultImport = True
 
         if len(args) == 0:
             if not gitBranchExists(self.branch) and gitBranchExists("origin"):
                 if not self.silent:
                     print "Creating %s branch in git repository based on origin" % self.branch
-                system("git branch %s origin" % self.branch)
+                branch = self.branch
+                if not branch.startswith("refs"):
+                    branch = "refs/heads/" + branch
+                system("git update-ref %s origin" % branch)
+                if defaultImport:
+                    system("git symbolic-ref refs/remotes/p4/HEAD %s" % branch)
 
             [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
             if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
                 p4Change = int(p4Change) + 1
                 self.depotPath = self.previousDepotPath
                 self.changeRange = "@%s,#head" % p4Change
-                self.initialParent = self.branch
+                self.initialParent = parseRevision(self.branch)
                 if not self.silent:
                     print "Performing incremental import into %s git branch" % self.branch
 
-        self.branch = "refs/heads/" + self.branch
+        if not self.branch.startswith("refs/"):
+            self.branch = "refs/heads/" + self.branch
 
         if len(self.depotPath) != 0:
             self.depotPath = self.depotPath[:-1]
@@ -929,23 +908,6 @@ class P4Sync(Command):
         if self.detectLabels:
             self.getLabels();
 
-        if len(self.changeRange) == 0:
-            try:
-                sout, sin, serr = popen2.popen3("git name-rev --tags `git rev-parse %s`" % self.branch)
-                output = sout.read()
-                if output.endswith("\n"):
-                    output = output[:-1]
-                tagIdx = output.index(" tags/p4/")
-                caretIdx = output.find("^")
-                endPos = len(output)
-                if caretIdx != -1:
-                    endPos = caretIdx
-                self.rev = int(output[tagIdx + 9 : endPos]) + 1
-                self.changeRange = "@%s,#head" % self.rev
-                self.initialParent = mypopen("git rev-parse %s" % self.branch).read()[:-1]
-            except:
-                pass
-
         self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
 
         importProcess = subprocess.Popen(["git", "fast-import"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE);
@@ -1157,7 +1119,6 @@ def printUsage(commands):
 
 commands = {
     "debug" : P4Debug(),
-    "clean-tags" : P4CleanTags(),
     "submit" : P4Submit(),
     "sync" : P4Sync(),
     "rebase" : P4Rebase(),