git-p4: Do not include diff in spec file when just preparing p4

The diff information render the spec file unusable as is by p4, do not include it when run with --prepare-p4-only so that the given file can be directly passed to p4. With --prepare-p4-only, git-p4 already tells the user it can use p4 submit with the generated spec file. This fails because of the diff being present in the file. Not including the diff fixes that. Without --prepare-p4-only, keeping the diff makes sense for a quick review of the patch before submitting it. And does not cause problems with p4 as we remove it programmatically. Signed-off-by: Maxime Coste <frrrwww@gmail.com> Acked-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Maxime Coste committed May 24, 2014 at 18:40 UTC b4073bb387ef303c9ac3c044f46d6a8ae6e190f0
2 files changed +28 -24
git-p4.py
+26 -23
@@ -1238,6 +1238,28 @@ class P4Submit(Command, P4UserMap):
1238 if response == 'n':
1239 return False
1240
1241 + def get_diff_description(self, editedFiles):
1242 + # diff
1243 + if os.environ.has_key("P4DIFF"):
1244 + del(os.environ["P4DIFF"])
1245 + diff = ""
1246 + for editedFile in editedFiles:
1247 + diff += p4_read_pipe(['diff', '-du',
1248 + wildcard_encode(editedFile)])
1249 +
1250 + # new file diff
1251 + newdiff = ""
1252 + for newFile in filesToAdd:
1253 + newdiff += "==== new file ====\n"
1254 + newdiff += "--- /dev/null\n"
1255 + newdiff += "+++ %s\n" % newFile
1256 + f = open(newFile, "r")
1257 + for line in f.readlines():
1258 + newdiff += "+" + line
1259 + f.close()
1260 +
1261 + return diff + newdiff
1262 +
1263 def applyCommit(self, id):
1264 """Apply one commit, return True if it succeeded."""
1265
@@ -1398,34 +1420,15 @@ class P4Submit(Command, P4UserMap):
1420 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
1421
1422 separatorLine = "######## everything below this line is just the diff #######\n"
1423 + if not self.prepare_p4_only:
1424 + submitTemplate += separatorLine
1425 + submitTemplate += self.get_diff_description(editedFiles)
1426
1402 - # diff
1403 - if os.environ.has_key("P4DIFF"):
1404 - del(os.environ["P4DIFF"])
1405 - diff = ""
1406 - for editedFile in editedFiles:
1407 - diff += p4_read_pipe(['diff', '-du',
1408 - wildcard_encode(editedFile)])
1409 -
1410 - # new file diff
1411 - newdiff = ""
1412 - for newFile in filesToAdd:
1413 - newdiff += "==== new file ====\n"
1414 - newdiff += "--- /dev/null\n"
1415 - newdiff += "+++ %s\n" % newFile
1416 - f = open(newFile, "r")
1417 - for line in f.readlines():
1418 - newdiff += "+" + line
1419 - f.close()
1420 -
1421 - # change description file: submitTemplate, separatorLine, diff, newdiff
1427 (handle, fileName) = tempfile.mkstemp()
1428 tmpFile = os.fdopen(handle, "w+")
1429 if self.isWindows:
1430 submitTemplate = submitTemplate.replace("\n", "\r\n")
1426 - separatorLine = separatorLine.replace("\n", "\r\n")
1427 - newdiff = newdiff.replace("\n", "\r\n")
1428 - tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
1431 + tmpFile.write(submitTemplate)
1432 tmpFile.close()
1433
1434 if self.prepare_p4_only:
t/t9807-git-p4-submit.sh
+2 -1
@@ -403,7 +403,8 @@ test_expect_success 'submit --prepare-p4-only' '
403 git commit -m "prep only add" &&
404 git p4 submit --prepare-p4-only >out &&
405 test_i18ngrep "prepared for submission" out &&
406 - test_i18ngrep "must be deleted" out
406 + test_i18ngrep "must be deleted" out &&
407 + ! test_i18ngrep "everything below this line is just the diff" out
408 ) &&
409 (
410 cd "$cli" &&