Minor cosmetic fixlet for the git-p4 submit sync question.
[gitweb.git] / contrib / fast-import / git-p4
index 8684e4b20f01e80797e472bada4fdc6a2925cdcf..aaa5d5ee5fc277763584f711a5ddda6b8d065324 100755 (executable)
@@ -36,6 +36,22 @@ def p4Cmd(cmd):
         result.update(entry)
     return result;
 
+def p4Where(depotPath):
+    if not depotPath.endswith("/"):
+        depotPath += "/"
+    output = p4Cmd("where %s..." % depotPath)
+    clientPath = ""
+    if "path" in output:
+        clientPath = output.get("path")
+    elif "data" in output:
+        data = output.get("data")
+        lastSpace = data.rfind(" ")
+        clientPath = data[lastSpace + 1:]
+
+    if clientPath.endswith("..."):
+        clientPath = clientPath[:-3]
+    return clientPath
+
 def die(msg):
     sys.stderr.write(msg + "\n")
     sys.exit(1)
@@ -162,7 +178,7 @@ class P4Sync(Command):
         self.dryRun = False
         self.substFile = ""
         self.firstTime = True
-        self.origin = "origin"
+        self.origin = ""
         self.master = ""
         self.applyAsPatch = True
 
@@ -304,6 +320,38 @@ class P4Sync(Command):
             print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName)
 
     def run(self, args):
+        global gitdir
+        # make gitdir absolute so we can cd out into the perforce checkout
+        gitdir = os.path.abspath(gitdir)
+        os.environ["GIT_DIR"] = gitdir
+        depotPath = ""
+        if gitBranchExists("p4"):
+            [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4"))
+        if len(depotPath) == 0 and gitBranchExists("origin"):
+            [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin"))
+
+        if len(depotPath) == 0:
+            print "Internal error: cannot locate perforce depot path from existing branches"
+            sys.exit(128)
+
+        clientPath = p4Where(depotPath)
+
+        if len(clientPath) == 0:
+            print "Error: Cannot locate perforce checkout of %s in client view" % depotPath
+            sys.exit(128)
+
+        print "Perforce checkout for depot path %s located at %s" % (depotPath, clientPath)
+        os.chdir(clientPath)
+        response = raw_input("Do you want to sync %s with p4 sync? (y/n) " % clientPath)
+        if response == "y" or response == "yes":
+            system("p4 sync ...")
+
+        if len(self.origin) == 0:
+            if gitBranchExists("p4"):
+                self.origin = "p4"
+            else:
+                self.origin = "origin"
+
         if self.reset:
             self.firstTime = True
 
@@ -715,21 +763,22 @@ class GitSync(Command):
 
         if len(self.branch) == 0:
             self.branch = "p4"
-            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)
-
-                [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
-                if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
-                    p4Change = int(p4Change) + 1
-                    self.globalPrefix = self.previousDepotPath
-                    self.changeRange = "@%s,#head" % p4Change
-                    self.initialParent = self.branch
-                    self.tagLastChange = False
-                    if not self.silent:
-                        print "Performing incremental import into %s git branch" % self.branch
+
+        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)
+
+            [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
+            if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
+                p4Change = int(p4Change) + 1
+                self.globalPrefix = self.previousDepotPath
+                self.changeRange = "@%s,#head" % p4Change
+                self.initialParent = self.branch
+                self.tagLastChange = False
+                if not self.silent:
+                    print "Performing incremental import into %s git branch" % self.branch
 
         self.branch = "refs/heads/" + self.branch