@matheusbsbs / matheus-bagliano / commits / cd1e0dc47a
Fork 0

git-p4: restructure code in submit

In preparation for adding new hooks to the submit method of git-p4, restructure the applyCommit function in the P4Submit class. Make the following changes: * Move all the code after the definition of submitted = False into the Try-Finally block. This ensures that any error that occurs will properly recover. This is not necessary with the current code because none of it should throw an exception, however the next set of changes will introduce exceptional code. Existing flow control can remain as defined - the if-block for prepare-p4-only sets the local variable "submitted" to True and exits the function. New early exits, leave submitted set to False so the Finally block will undo changes to the P4 workspace. * Make the small usability change of adding an empty string to the print statements displayed to the user when the prepare-p4-only option is selected. On Windows, the command print() may display a set of parentheses "()" to the user when the print() function is called with no parameters. By supplying an empty string, the intended blank line will print as expected. * Fix a small bug when the submittedTemplate is edited by the user and all content in the file is removed. The existing code will throw an exception if the separateLine is not found in the file. Change this code to test for the separator line using a find() test first and only split on the separator if it is found. * Additionally, add the new behavior that if the changelist file has been completely emptied that the Submit action for this changelist will be aborted. Signed-off-by: Ben Keene <seraphire@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Keene committed Feb 14, 2020 at 14:44 UTC cd1e0dc47a5ab1520b823d051d23f60c89072349
1 file changed +51 -41
git-p4.py
+51 -41
@@ -2102,47 +2102,47 @@ class P4Submit(Command, P4UserMap):
2102 tmpFile.write(submitTemplate)
2103 tmpFile.close()
2104
2105 - if self.prepare_p4_only:
2106 - #
2107 - # Leave the p4 tree prepared, and the submit template around
2108 - # and let the user decide what to do next
2109 - #
2110 - print()
2111 - print("P4 workspace prepared for submission.")
2112 - print("To submit or revert, go to client workspace")
2113 - print(" " + self.clientPath)
2114 - print()
2115 - print("To submit, use \"p4 submit\" to write a new description,")
2116 - print("or \"p4 submit -i <%s\" to use the one prepared by" \
2117 - " \"git p4\"." % fileName)
2118 - print("You can delete the file \"%s\" when finished." % fileName)
2119 -
2120 - if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2121 - print("To preserve change ownership by user %s, you must\n" \
2122 - "do \"p4 change -f <change>\" after submitting and\n" \
2123 - "edit the User field.")
2124 - if pureRenameCopy:
2125 - print("After submitting, renamed files must be re-synced.")
2126 - print("Invoke \"p4 sync -f\" on each of these files:")
2127 - for f in pureRenameCopy:
2128 - print(" " + f)
2129 -
2130 - print()
2131 - print("To revert the changes, use \"p4 revert ...\", and delete")
2132 - print("the submit template file \"%s\"" % fileName)
2133 - if filesToAdd:
2134 - print("Since the commit adds new files, they must be deleted:")
2135 - for f in filesToAdd:
2136 - print(" " + f)
2137 - print()
2138 - return True
2139 -
2140 - #
2141 - # Let the user edit the change description, then submit it.
2142 - #
2105 submitted = False
2106
2107 try:
2108 +
2109 + if self.prepare_p4_only:
2110 + #
2111 + # Leave the p4 tree prepared, and the submit template around
2112 + # and let the user decide what to do next
2113 + #
2114 + submitted = True
2115 + print("")
2116 + print("P4 workspace prepared for submission.")
2117 + print("To submit or revert, go to client workspace")
2118 + print(" " + self.clientPath)
2119 + print("")
2120 + print("To submit, use \"p4 submit\" to write a new description,")
2121 + print("or \"p4 submit -i <%s\" to use the one prepared by" \
2122 + " \"git p4\"." % fileName)
2123 + print("You can delete the file \"%s\" when finished." % fileName)
2124 +
2125 + if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2126 + print("To preserve change ownership by user %s, you must\n" \
2127 + "do \"p4 change -f <change>\" after submitting and\n" \
2128 + "edit the User field.")
2129 + if pureRenameCopy:
2130 + print("After submitting, renamed files must be re-synced.")
2131 + print("Invoke \"p4 sync -f\" on each of these files:")
2132 + for f in pureRenameCopy:
2133 + print(" " + f)
2134 +
2135 + print("")
2136 + print("To revert the changes, use \"p4 revert ...\", and delete")
2137 + print("the submit template file \"%s\"" % fileName)
2138 + if filesToAdd:
2139 + print("Since the commit adds new files, they must be deleted:")
2140 + for f in filesToAdd:
2141 + print(" " + f)
2142 + print("")
2143 + sys.stdout.flush()
2144 + return True
2145 +
2146 if self.edit_template(fileName):
2147 # read the edited message and submit
2148 tmpFile = open(fileName, "rb")
@@ -2150,7 +2150,15 @@ class P4Submit(Command, P4UserMap):
2150 tmpFile.close()
2151 if self.isWindows:
2152 message = message.replace("\r\n", "\n")
2153 - submitTemplate = message[:message.index(separatorLine)]
2153 + if message.find(separatorLine) != -1:
2154 + submitTemplate = message[:message.index(separatorLine)]
2155 + else:
2156 + submitTemplate = message
2157 +
2158 + if len(submitTemplate.strip()) == 0:
2159 + print("Changelist is empty, aborting this changelist.")
2160 + sys.stdout.flush()
2161 + return False
2162
2163 if update_shelve:
2164 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
@@ -2174,19 +2182,21 @@ class P4Submit(Command, P4UserMap):
2182 submitted = True
2183
2184 finally:
2177 - # skip this patch
2185 + # Revert changes if we skip this patch
2186 if not submitted or self.shelve:
2187 if self.shelve:
2188 print ("Reverting shelved files.")
2189 else:
2190 print ("Submission cancelled, undoing p4 changes.")
2191 + sys.stdout.flush()
2192 for f in editedFiles | filesToDelete:
2193 p4_revert(f)
2194 for f in filesToAdd:
2195 p4_revert(f)
2196 os.remove(f)
2197
2189 - os.remove(fileName)
2198 + if not self.prepare_p4_only:
2199 + os.remove(fileName)
2200 return submitted
2201
2202 # Export git tags as p4 labels. Create a p4 label and then tag