Skip to content

Commit b7638fe

Browse files
GIRARD Etiennepeff
authored andcommitted
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 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Luke Diamand <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent b05c2f9 commit b7638fe

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

git-p4.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,44 +1538,47 @@ def applyCommit(self, id):
15381538
#
15391539
# Let the user edit the change description, then submit it.
15401540
#
1541-
if self.edit_template(fileName):
1542-
# read the edited message and submit
1543-
ret = True
1544-
tmpFile = open(fileName, "rb")
1545-
message = tmpFile.read()
1546-
tmpFile.close()
1547-
if self.isWindows:
1548-
message = message.replace("\r\n", "\n")
1549-
submitTemplate = message[:message.index(separatorLine)]
1550-
p4_write_pipe(['submit', '-i'], submitTemplate)
1551-
1552-
if self.preserveUser:
1553-
if p4User:
1554-
# Get last changelist number. Cannot easily get it from
1555-
# the submit command output as the output is
1556-
# unmarshalled.
1557-
changelist = self.lastP4Changelist()
1558-
self.modifyChangelistUser(changelist, p4User)
1559-
1560-
# The rename/copy happened by applying a patch that created a
1561-
# new file. This leaves it writable, which confuses p4.
1562-
for f in pureRenameCopy:
1563-
p4_sync(f, "-f")
1541+
submitted = False
15641542

1565-
else:
1543+
try:
1544+
if self.edit_template(fileName):
1545+
# read the edited message and submit
1546+
tmpFile = open(fileName, "rb")
1547+
message = tmpFile.read()
1548+
tmpFile.close()
1549+
if self.isWindows:
1550+
message = message.replace("\r\n", "\n")
1551+
submitTemplate = message[:message.index(separatorLine)]
1552+
p4_write_pipe(['submit', '-i'], submitTemplate)
1553+
1554+
if self.preserveUser:
1555+
if p4User:
1556+
# Get last changelist number. Cannot easily get it from
1557+
# the submit command output as the output is
1558+
# unmarshalled.
1559+
changelist = self.lastP4Changelist()
1560+
self.modifyChangelistUser(changelist, p4User)
1561+
1562+
# The rename/copy happened by applying a patch that created a
1563+
# new file. This leaves it writable, which confuses p4.
1564+
for f in pureRenameCopy:
1565+
p4_sync(f, "-f")
1566+
submitted = True
1567+
1568+
finally:
15661569
# skip this patch
1567-
ret = False
1568-
print "Submission cancelled, undoing p4 changes."
1569-
for f in editedFiles:
1570-
p4_revert(f)
1571-
for f in filesToAdd:
1572-
p4_revert(f)
1573-
os.remove(f)
1574-
for f in filesToDelete:
1575-
p4_revert(f)
1570+
if not submitted:
1571+
print "Submission cancelled, undoing p4 changes."
1572+
for f in editedFiles:
1573+
p4_revert(f)
1574+
for f in filesToAdd:
1575+
p4_revert(f)
1576+
os.remove(f)
1577+
for f in filesToDelete:
1578+
p4_revert(f)
15761579

15771580
os.remove(fileName)
1578-
return ret
1581+
return submitted
15791582

15801583
# Export git tags as p4 labels. Create a p4 label and then tag
15811584
# with that.

t/t9807-git-p4-submit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ test_expect_success 'description with Jobs section and bogus following text' '
389389
(
390390
cd "$cli" &&
391391
p4 revert desc6 &&
392-
rm desc6
392+
rm -f desc6
393393
)
394394
'
395395

0 commit comments

Comments
 (0)