threads / patch / 40665

patchgit-p4: Handle p4 submit failure

Subject: [PATCH] git-p4: Handle p4 submit failure

## tl;dr

6 messages between Oct 28, 2015 and Oct 30, 2015. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Etienne Girard· Oct 28, 2015, 13:12 UTC · lore

Clean the workspace if p4_write_pipe raised SystemExit, so that the user don't have to do it themselves.

Signed-off-by: GIRARD Etienne <egirard@murex.com>
---
 git-p4.py | 71 +++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 37 insertions(+), 34 deletions(-)

The p4 submit command may fail, for example if the changelist contains a job that does not exist in the Jobs section. If this is the case, p4_write_pipe will exit the script. This change makes it so that the workspace is reverted before letting the script die.

Show changes to git-p4.py +37 −34
diff --git a/git-p4.py b/git-p4.py
index 0093fa3..d535904 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -1538,44 +1538,47 @@ class P4Submit(Command, P4UserMap):
         #
         # Let the user edit the change description, then submit it.
         #
-        if self.edit_template(fileName):
-            # read the edited message and submit
-            ret = True
-            tmpFile = open(fileName, "rb")
-            message = tmpFile.read()
-            tmpFile.close()
-            if self.isWindows:
-                message = message.replace("\r\n", "\n")
-            submitTemplate = message[:message.index(separatorLine)]
-            p4_write_pipe(['submit', '-i'], submitTemplate)
-
-            if self.preserveUser:
-                if p4User:
-                    # Get last changelist number. Cannot easily get it from
-                    # the submit command output as the output is
-                    # unmarshalled.
-                    changelist = self.lastP4Changelist()
-                    self.modifyChangelistUser(changelist, p4User)
-
-            # The rename/copy happened by applying a patch that created a
-            # new file.  This leaves it writable, which confuses p4.
-            for f in pureRenameCopy:
-                p4_sync(f, "-f")
+        submitted = False

-        else:
+        try:
+            if self.edit_template(fileName):
+                # read the edited message and submit
+                tmpFile = open(fileName, "rb")
+                message = tmpFile.read()
+                tmpFile.close()
+                if self.isWindows:
+                    message = message.replace("\r\n", "\n")
+                submitTemplate = message[:message.index(separatorLine)]
+                p4_write_pipe(['submit', '-i'], submitTemplate)
+
+                if self.preserveUser:
+                    if p4User:
+                        # Get last changelist number. Cannot easily get it from
+                        # the submit command output as the output is
+                        # unmarshalled.
+                        changelist = self.lastP4Changelist()
+                        self.modifyChangelistUser(changelist, p4User)
+
+                # The rename/copy happened by applying a patch that created a
+                # new file.  This leaves it writable, which confuses p4.
+                for f in pureRenameCopy:
+                    p4_sync(f, "-f")
+                submitted = True
+
+        finally:
             # skip this patch
-            ret = False
-            print "Submission cancelled, undoing p4 changes."
-            for f in editedFiles:
-                p4_revert(f)
-            for f in filesToAdd:
-                p4_revert(f)
-                os.remove(f)
-            for f in filesToDelete:
-                p4_revert(f)
+            if not submitted:
+                print "Submission cancelled, undoing p4 changes."
+                for f in editedFiles:
+                    p4_revert(f)
+                for f in filesToAdd:
+                    p4_revert(f)
+                    os.remove(f)
+                for f in filesToDelete:
+                    p4_revert(f)

         os.remove(fileName)
-        return ret
+        return submitted

     # Export git tags as p4 labels. Create a p4 label and then tag
     # with that.
-- 
1.9.1
Junio C Hamano· Oct 30, 2015, 16:44 UTC · re: Etienne Girard · lore

Re: [PATCH] git-p4: Handle p4 submit failure

Etienne Girard <etienne.g.girard@gmail.com> writes:
Show 5 quoted lines
> Clean the workspace if p4_write_pipe raised SystemExit,
> so that the user don't have to do it themselves.
>
> Signed-off-by: GIRARD Etienne <egirard@murex.com>
> ---

Thanks. As I do not really do "p4", I am CC'ing this response to the area experts.

First, one "procedural" comment.

You sent the patch from your gmail.com address, but signed it off with another address (with name spelled differently). If applied as-is, the result will have your gmail.com address listed as the author and that will be the name you would see in "git shortlog".

If you rather prefer to be known as the name and address you placed on your sign-off, while sending the patch from another address, you can do so by staring the _body_ of the message with what is called an "in-body header", like this:

        From: GIRARD Etienne <egirard@murex.com>
        Clean the workspace if p4_write_pipe raised SystemExit,
        so that the user don't have to do it themselves.
        Signed-off-by: GIRARD Etienne <egirard@murex.com>
        ---
	  ... here you have the usual patch ...

That "From:" line would sit on the first line of the body of your message, separated by a blank line from the body of the log message.

This is not even a problem from the project's point of view, but I thought you would want to be aware of it, and may want a chance to change it.

	Note: this time, you do not need to resend the patch; just
	please let me know if you want me to do the equivalent of
	the above while applying to make your murex address and name
	appear as the author in "git log" and "git shortlog" output.

FYI, you could override the "Subject:" of your e-mail header with an in-body header (this is often useful when replying to an existing discussion thread with a patch that shows a solution), e.g.

        From: GIRARD Etienne <egirard@murex.com>
	Subject: git-p4: clean up after p4 submit failure
        Clean the workspace if p4_write_pipe raised SystemExit,
        so that the user don't have to do it themselves.
        Signed-off-by: GIRARD Etienne <egirard@murex.com>
        ---
	  ... here you have the usual patch ...
Show 7 quoted lines
>  git-p4.py | 71 +++++++++++++++++++++++++++++++++------------------------------
>  1 file changed, 37 insertions(+), 34 deletions(-)
>
> The p4 submit command may fail, for example if the changelist contains
> a job that does not exist in the Jobs section. If this is the case,
> p4_write_pipe will exit the script. This change makes it so that the
> workspace is reverted before letting the script die.

Some of the information contained in this paragraph deserves to be in the log message proper. How about

        From: GIRARD Etienne <egirard@murex.com>
	Subject: git-p4: clean up after p4 submit failure
	When "p4 submit" command fails in P4Submit.applyCommit, the
	workspace is left with the changes.  We already have a code
        to revert the changes to the workspace when the user decides
	to cancel submission by aborting the editor that edits the
	change description, and we should treat the "p4 submit"
	failure the same way.
        Clean the workspace if p4_write_pipe raised SystemExit,
        so that the user don't have to do it themselves.
        Signed-off-by: GIRARD Etienne <egirard@murex.com>
or something like that?

While trying to come up with the above description, I started wondering if the error message wants to differentiate the two cases.

When self.edit_template() returns false, we know that the user actively said "no I do not want to submit", and "Submission cancelled" is perfectly fine, but when "p4 submit" fails because it did not like the change description or if it found some other issues, it is not necessarily that the user said "I want to cancel"; it is more like "Submission failed".

Hmm?
Etienne Girard· Oct 30, 2015, 17:33 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-p4: Handle p4 submit failure

Hello,
>         Note: this time, you do not need to resend the patch; just
>         please let me know if you want me to do the equivalent of
>         the above while applying to make your murex address and name
>         appear as the author in "git log" and "git shortlog" output.

I'd like my murex address to appear on the log please, if it is not too much trouble. Thank you for all these tips on the submitting process.

Show 24 quoted lines
>> The p4 submit command may fail, for example if the changelist contains
>> a job that does not exist in the Jobs section. If this is the case,
>> p4_write_pipe will exit the script. This change makes it so that the
>> workspace is reverted before letting the script die.
>
> Some of the information contained in this paragraph deserves to be
> in the log message proper.  How about
>
>         From: GIRARD Etienne <egirard@murex.com>
>         Subject: git-p4: clean up after p4 submit failure
>
>         When "p4 submit" command fails in P4Submit.applyCommit, the
>         workspace is left with the changes.  We already have a code
>         to revert the changes to the workspace when the user decides
>         to cancel submission by aborting the editor that edits the
>         change description, and we should treat the "p4 submit"
>         failure the same way.
>
>         Clean the workspace if p4_write_pipe raised SystemExit,
>         so that the user don't have to do it themselves.
>
>         Signed-off-by: GIRARD Etienne <egirard@murex.com>
>
> or something like that?

It seems like a good description. Please let me know if I should submit another patch with the proper log message

Show 10 quoted lines
>
> While trying to come up with the above description, I started
> wondering if the error message wants to differentiate the two cases.
>
> When self.edit_template() returns false, we know that the user
> actively said "no I do not want to submit", and "Submission
> cancelled" is perfectly fine, but when "p4 submit" fails because it
> did not like the change description or if it found some other
> issues, it is not necessarily that the user said "I want to cancel";
> it is more like "Submission failed".

Yes, however if `p4 submit` fails the corresponding "Command failed" error message is displayed, and the p4 error message itself is displayed if any. Tthe script will also terminate successfully if self.edit_template returns false but it will exit with error code 1 if p4 submit fails.

So the user will get "Command failed: [...]" followed by "Submission cancelled, undoing p4 changes", to let him know that the script failed because of p4 and that nothing was submitted.

Junio C Hamano· Oct 30, 2015, 17:57 UTC · re: Etienne Girard · lore

Re: [PATCH] git-p4: Handle p4 submit failure

Etienne Girard <etienne.g.girard@gmail.com> writes:
Show 9 quoted lines
> Yes, however if `p4 submit` fails the corresponding "Command failed"
> error message is displayed, and the p4 error message itself is
> displayed if any.
> Tthe script will also terminate successfully if self.edit_template
> returns false but it will exit with error code 1 if p4 submit fails.
>
> So the user will get "Command failed: [...]" followed by "Submission
> cancelled, undoing p4 changes", to let him know that the script failed
> because of p4 and that nothing was submitted.

OK, then it sounds like all I have to do is to update the log message with the "How about this" version and correct the authorship to use your murex address, and then wait for reviews from real "git p4" reviewers.

Thanks.
Luke Diamand· Oct 30, 2015, 19:53 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-p4: Handle p4 submit failure

On 30/10/15 17:57, Junio C Hamano wrote:
Show 17 quoted lines
> Etienne Girard <etienne.g.girard@gmail.com> writes:
>
>> Yes, however if `p4 submit` fails the corresponding "Command failed"
>> error message is displayed, and the p4 error message itself is
>> displayed if any.
>> Tthe script will also terminate successfully if self.edit_template
>> returns false but it will exit with error code 1 if p4 submit fails.
>>
>> So the user will get "Command failed: [...]" followed by "Submission
>> cancelled, undoing p4 changes", to let him know that the script failed
>> because of p4 and that nothing was submitted.
>
> OK, then it sounds like all I have to do is to update the log
> message with the "How about this" version and correct the authorship
> to use your murex address, and then wait for reviews from real "git
> p4" reviewers.
>
Looks good to me. Nice use of try...finally.

One very small thing - test t9807-git-p4-submit.sh is now failing with this change.

That's because it tries to delete one of the files it created, but you are now deleting it already! Could you just update that please?

Thanks! Luke

Junio C Hamano· Oct 30, 2015, 20:23 UTC · re: Luke Diamand · lore

Re: [PATCH] git-p4: Handle p4 submit failure

Luke Diamand <luke@diamand.org> writes:
Show 26 quoted lines
> On 30/10/15 17:57, Junio C Hamano wrote:
>> Etienne Girard <etienne.g.girard@gmail.com> writes:
>>
>>> Yes, however if `p4 submit` fails the corresponding "Command failed"
>>> error message is displayed, and the p4 error message itself is
>>> displayed if any.
>>> Tthe script will also terminate successfully if self.edit_template
>>> returns false but it will exit with error code 1 if p4 submit fails.
>>>
>>> So the user will get "Command failed: [...]" followed by "Submission
>>> cancelled, undoing p4 changes", to let him know that the script failed
>>> because of p4 and that nothing was submitted.
>>
>> OK, then it sounds like all I have to do is to update the log
>> message with the "How about this" version and correct the authorship
>> to use your murex address, and then wait for reviews from real "git
>> p4" reviewers.
>>
>
> Looks good to me. Nice use of try...finally.
>
> One very small thing - test t9807-git-p4-submit.sh is now failing with
> this change.
>
> That's because it tries to delete one of the files it created, but you
> are now deleting it already! Could you just update that please?

I'll ask Etienne include that change in a reroll, as my environment is not equipped to run any p4 tests.

Thanks.

← back to recent threads