Fix "compilation" :)
[gitweb.git] / contrib / fast-import / git-p4
index 5b023b1b7a092cd322a2f40d09c590f1ccf0921e..04433373596e9eddfaced5f4da0aa3309deed4db 100755 (executable)
@@ -107,6 +107,7 @@ def gitBranchExists(branch):
 class Command:
     def __init__(self):
         self.usage = "usage: %prog [options]"
+        self.needsGit = True
 
 class P4Debug(Command):
     def __init__(self):
@@ -114,6 +115,7 @@ class P4Debug(Command):
         self.options = [
         ]
         self.description = "A tool to debug the output of p4 -G."
+        self.needsGit = False
 
     def run(self, args):
         for output in p4CmdList(" ".join(args)):
@@ -165,13 +167,13 @@ class P4Sync(Command):
                 optparse.make_option("--continue", action="store_false", dest="firstTime"),
                 optparse.make_option("--origin", dest="origin"),
                 optparse.make_option("--reset", action="store_true", dest="reset"),
-                optparse.make_option("--master", dest="master"),
                 optparse.make_option("--log-substitutions", dest="substFile"),
                 optparse.make_option("--noninteractive", action="store_false"),
                 optparse.make_option("--dry-run", action="store_true"),
                 optparse.make_option("--apply-as-patch", action="store_true", dest="applyAsPatch")
         ]
         self.description = "Submit changes from git to the perforce depot."
+        self.usage += " [name of git branch to submit into perforce depot]"
         self.firstTime = True
         self.reset = False
         self.interactive = True
@@ -179,7 +181,6 @@ class P4Sync(Command):
         self.substFile = ""
         self.firstTime = True
         self.origin = ""
-        self.master = ""
         self.applyAsPatch = True
 
         self.logSubstitutions = {}
@@ -324,6 +325,16 @@ class P4Sync(Command):
         # make gitdir absolute so we can cd out into the perforce checkout
         gitdir = os.path.abspath(gitdir)
         os.environ["GIT_DIR"] = gitdir
+
+        if len(args) == 0:
+            self.master = currentGitBranch()
+            if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)):
+                die("Detecting current git branch failed!")
+        elif len(args) == 1:
+            self.master = args[0]
+        else:
+            return False
+
         depotPath = ""
         if gitBranchExists("p4"):
             [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4"))
@@ -360,11 +371,6 @@ class P4Sync(Command):
                 tokens = line[:-1].split("=")
                 self.logSubstitutions[tokens[0]] = tokens[1]
 
-        if len(self.master) == 0:
-            self.master = currentGitBranch()
-            if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)):
-                die("Detecting current git branch failed!")
-
         self.check()
         self.configFile = gitdir + "/p4-git-sync.cfg"
         self.config = shelve.open(self.configFile, writeback=True)
@@ -410,7 +416,7 @@ class GitSync(Command):
                 optparse.make_option("--changesfile", dest="changesFile"),
                 optparse.make_option("--silent", dest="silent", action="store_true"),
                 optparse.make_option("--known-branches", dest="knownBranches"),
-                optparse.make_option("--cache", dest="doCache", action="store_true"),
+                optparse.make_option("--data-cache", dest="dataCache", action="store_true"),
                 optparse.make_option("--command-cache", dest="commandCache", action="store_true")
         ]
         self.description = """Imports from Perforce into a git repository.\n
@@ -494,7 +500,7 @@ class GitSync(Command):
             if knownBranch:
                 continue
 
-            for branch in knownBranches:
+            for branch in self.knownBranches:
                 #if relativePath.startswith(branch):
                 if self.isSubPathOf(relativePath, branch):
                     if len(branches) == 0:
@@ -623,7 +629,47 @@ class GitSync(Command):
 
         self.gitStream.write("\n")
 
-        self.lastChange = int(details["change"])
+        change = int(details["change"])
+
+        self.lastChange = change
+
+        if change in self.labels:
+            label = self.labels[change]
+            labelDetails = label[0]
+            labelRevisions = label[1]
+
+            files = p4CmdList("files %s...@%s" % (branchPrefix, change))
+
+            if len(files) == len(labelRevisions):
+
+                cleanedFiles = {}
+                for info in files:
+                    if info["action"] == "delete":
+                        continue
+                    cleanedFiles[info["depotFile"]] = info["rev"]
+
+                if cleanedFiles == labelRevisions:
+                    self.gitStream.write("tag tag_%s\n" % labelDetails["label"])
+                    self.gitStream.write("from %s\n" % branch)
+
+                    owner = labelDetails["Owner"]
+                    tagger = ""
+                    if author in self.users:
+                        tagger = "%s %s %s" % (self.users[owner], epoch, self.tz)
+                    else:
+                        tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz)
+                    self.gitStream.write("tagger %s\n" % tagger)
+                    self.gitStream.write("data <<EOT\n")
+                    self.gitStream.write(labelDetails["Description"])
+                    self.gitStream.write("EOT\n\n")
+
+                else:
+                    if not self.silent:
+                        print "Tag %s does not match with change %s: files do not match." % (labelDetails["label"], change)
+
+            else:
+                if not self.silent:
+                    print "Tag %s does not match with change %s: file count is different." % (labelDetails["label"], change)
 
     def extractFilesInCommitToBranch(self, files, branchPrefix):
         newFiles = []
@@ -755,6 +801,21 @@ class GitSync(Command):
                 continue
             self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
 
+    def getLabels(self):
+        self.labels = {}
+
+        for output in p4CmdList("labels %s..." % self.globalPrefix):
+            label = output["label"]
+            revisions = {}
+            newestChange = 0
+            for file in p4CmdList("files //...@%s" % label):
+                revisions[file["depotFile"]] = file["rev"]
+                change = int(file["change"])
+                if change > newestChange:
+                    newestChange = change
+
+            self.labels[newestChange] = [output, revisions]
+
     def run(self, args):
         self.globalPrefix = ""
         self.changeRange = ""
@@ -827,6 +888,7 @@ class GitSync(Command):
             self.globalPrefix += "/"
 
         self.getUserMap()
+        self.getLabels();
 
         if len(self.changeRange) == 0:
             try:
@@ -1031,21 +1093,22 @@ if len(options) > 0:
 
     (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
 
-gitdir = cmd.gitdir
-if len(gitdir) == 0:
-    gitdir = ".git"
-    if not isValidGitDir(gitdir):
-        cdup = os.popen("git rev-parse --show-cdup").read()[:-1]
-        if isValidGitDir(cdup + "/" + gitdir):
-            os.chdir(cdup)
+if cmd.needsGit:
+    gitdir = cmd.gitdir
+    if len(gitdir) == 0:
+        gitdir = ".git"
+        if not isValidGitDir(gitdir):
+            cdup = os.popen("git rev-parse --show-cdup").read()[:-1]
+            if isValidGitDir(cdup + "/" + gitdir):
+                os.chdir(cdup)
 
-if not isValidGitDir(gitdir):
-    if isValidGitDir(gitdir + "/.git"):
-        gitdir += "/.git"
-    else:
-        die("fatal: cannot locate git repository at %s" % gitdir)
+    if not isValidGitDir(gitdir):
+        if isValidGitDir(gitdir + "/.git"):
+            gitdir += "/.git"
+        else:
+            die("fatal: cannot locate git repository at %s" % gitdir)
 
-os.environ["GIT_DIR"] = gitdir
+    os.environ["GIT_DIR"] = gitdir
 
 if not cmd.run(args):
     parser.print_help()