Subject: [PATCH 2/3] git-p4: improve submit dialogs

From: Pal-Kristian Engstad <[email-masked]>
Date: Thu, 21 Jan 2010 18:05:04 -0800

Improve the git-p4 submit process by adding additional submit options.

* The regular commit sequence now gives the options of:
  + [y] to submit the patch as is,
  + [c] to display the Perforce commit through "less",
  + [d] to display the output of "p4 diff -du" through "less",
  + [e] to edit the message before submitting,
  + [n] to skip the patch.

* Add a --no-prompt option to submit multiple check-ins without any
  prompts. This feature is useful for when many changes needs to be
  checked in. The git commit message is assumed as the Perforce
  submit message.

* Add an --edit option to preserve the previous submission process.

Signed-off-by: Pal-Kristian Engstad <[email-masked]>
---
 contrib/fast-import/git-p4 |   75 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 61 insertions(+), 14 deletions(-)

diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
index c65ef8a..ab538b3 100755
--- a/contrib/fast-import/git-p4
+++ b/contrib/fast-import/git-p4
@@ -545,6 +545,10 @@ class P4Submit(Command):
                 optparse.make_option("--origin", dest="origin"),
                 optparse.make_option("--no-detect", dest="detectRename", action="store_false",
                                      help="Disable git's rename detection, forcing \"p4 delete\" and \"p4 add\" instead of \"p4 integrate\" and \"p4 edit\""),
+                optparse.make_option("--no-prompt", dest="prompt", action="store_false",
+                                     help="Check in every git change into the perforce database without any prompts"),
+                optparse.make_option("--edit", dest="editCommit", action="store_true",
+                                     help="Edit every commit before submitting (old procedure)")
         ]
         self.description = "Submit changes from git to the perforce depot."
         self.usage += " [name of git branch to submit into perforce depot]"
@@ -552,6 +556,8 @@ class P4Submit(Command):
         self.origin = ""
         self.detectRename = True
         self.verbose = False
+        self.editCommit = False
+        self.prompt = True
         self.isWindows = (platform.system() == "Windows")
 
     def check(self):
@@ -657,19 +663,21 @@ class P4Submit(Command):
 
         if os.system(tryPatchCmd) != 0:
             print "Unfortunately applying the change failed!"
+            print "This may be because you need to rebase your commits to the latest P4 changes."
             print "What do you want to do?"
             response = "x"
-            while response != "s" and response != "a" and response != "w":
-                response = raw_input("[s]kip this patch / [a]pply the patch forcibly "
+            while response != "s" and response != "f" and response != "a" and response != "w":
+                response = raw_input("[a]bort / [s]kip this patch / apply the patch [f]orcibly "
                                      "and with .rej files / [w]rite the patch to a file (patch.txt) ")
-            if response == "s":
-                print "Skipping! Good luck with the next patches..."
+            if response == "s" or response == "a":
+                if response == "s":
+                    print "Skipping! Next set of patches might fail..."
                 for f in editedFiles:
                     p4_system("revert \"%s\"" % f);
                 for f in filesToAdd:
                     system("rm %s" %f)
-                return
-            elif response == "a":
+                return response == "a"
+            elif response == "f":
                 os.system(applyPatchCmd)
                 if len(filesToAdd) > 0:
                     print "You may also want to call p4 add on the following files:"
@@ -729,18 +737,28 @@ class P4Submit(Command):
                 newdiff = newdiff.replace("\n", "\r\n")
             tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
             tmpFile.close()
-            mtime = os.stat(fileName).st_mtime
+
             if os.environ.has_key("P4EDITOR"):
                 editor = os.environ.get("P4EDITOR")
             else:
                 editor = read_pipe("git var GIT_EDITOR")
-            system(editor + " " + fileName)
 
-            response = "y"
-            if os.stat(fileName).st_mtime <= mtime:
+            if self.editCommit:
+                response = "e"
+            elif not self.prompt:
+                response = "y"
+            else:
                 response = "x"
-                while response != "y" and response != "n":
-                    response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
+                print "--- Description --------------------------------------------------------------------"
+                print logMessage
+                print "------------------------------------------------------------------------------------"
+
+                while response != "y" and response != "n" and response != "e":
+                    response = raw_input("Submit: [y]es, see P4 [c]ommit, see [d]iff, [e]dit message first, or [n]o (skip this patch) ")
+                    if response == "d":
+                        system("less " + fileName)
+                    elif response == "c":
+                        print submitTemplate
 
             if response == "y":
                 tmpFile = open(fileName, "rb")
@@ -750,14 +768,39 @@ class P4Submit(Command):
                 if self.isWindows:
                     submitTemplate = submitTemplate.replace("\r\n", "\n")
                 p4_write_pipe("submit -i", submitTemplate)
-            else:
+            elif response == "n":
                 for f in editedFiles:
                     p4_system("revert \"%s\"" % f);
                 for f in filesToAdd:
                     p4_system("revert \"%s\"" % f);
                     system("rm %s" %f)
+            elif self.editCommit or response == "e":
+                mtime = os.stat(fileName).st_mtime
+                system(editor + " " + fileName)
+
+                response = "y"
+                if os.stat(fileName).st_mtime <= mtime:
+                    response = "x"
+                    while response != "y" and response != "n":
+                        response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
+
+                if response == "y":
+                    tmpFile = open(fileName, "rb")
+                    message = tmpFile.read()
+                    tmpFile.close()
+                    submitTemplate = message[:message.index(separatorLine)]
+                    if self.isWindows:
+                        submitTemplate = submitTemplate.replace("\r\n", "\n")
+                    p4_write_pipe("submit -i", submitTemplate)
+                else:
+                    for f in editedFiles:
+                        p4_system("revert \"%s\"" % f);
+                    for f in filesToAdd:
+                        p4_system("revert \"%s\"" % f);
+                        system("rm %s" %f)
 
             os.remove(fileName)
+            return True
         else:
             fileName = "submit.txt"
             file = open(fileName, "w+")
@@ -766,6 +809,7 @@ class P4Submit(Command):
             print ("Perforce submit template written as %s. "
                    + "Please review/edit and then use p4 submit -i < %s to submit directly!"
                    % (fileName, fileName))
+            return False
 
     def run(self, args):
         if len(args) == 0:
@@ -813,10 +857,13 @@ class P4Submit(Command):
             commits.append(line.strip())
         commits.reverse()
 
+        startCommits = len(commits)
+
         while len(commits) > 0:
             commit = commits[0]
             commits = commits[1:]
-            self.applyCommit(commit)
+            if not self.applyCommit(commit):
+                break
             if not self.interactive:
                 break
 
-- 
1.6.5.2.6.gc3c1e.dirty