Skip to content

Commit 7c766e5

Browse files
Pete Wyckoffgitster
authored andcommitted
git-p4: introduce skipSubmitEdit
Add a configuration variable to skip invoking the editor in the submit path. The existing variable skipSubmitEditCheck continues to make sure that the submit template was indeed modified by the editor; but, it is not considered if skipSubmitEdit is true. Reported-by: Loren A. Linden Levy <lindenle@gmail.com> Acked-by: Luke Diamand <luke@diamand.org> Signed-off-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent eb3b8d7 commit 7c766e5

File tree

3 files changed

+136
-24
lines changed

3 files changed

+136
-24
lines changed

contrib/fast-import/git-p4

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,38 @@ class P4Submit(Command, P4UserMap):
847847

848848
return template
849849

850+
def edit_template(self, template_file):
851+
"""Invoke the editor to let the user change the submission
852+
message. Return true if okay to continue with the submit."""
853+
854+
# if configured to skip the editing part, just submit
855+
if gitConfig("git-p4.skipSubmitEdit") == "true":
856+
return True
857+
858+
# look at the modification time, to check later if the user saved
859+
# the file
860+
mtime = os.stat(template_file).st_mtime
861+
862+
# invoke the editor
863+
if os.environ.has_key("P4EDITOR"):
864+
editor = os.environ.get("P4EDITOR")
865+
else:
866+
editor = read_pipe("git var GIT_EDITOR").strip()
867+
system(editor + " " + template_file)
868+
869+
# If the file was not saved, prompt to see if this patch should
870+
# be skipped. But skip this verification step if configured so.
871+
if gitConfig("git-p4.skipSubmitEditCheck") == "true":
872+
return True
873+
874+
if os.stat(template_file).st_mtime <= mtime:
875+
while True:
876+
response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
877+
if response == 'y':
878+
return True
879+
if response == 'n':
880+
return False
881+
850882
def applyCommit(self, id):
851883
print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
852884

@@ -1001,33 +1033,17 @@ class P4Submit(Command, P4UserMap):
10011033

10021034
separatorLine = "######## everything below this line is just the diff #######\n"
10031035

1004-
[handle, fileName] = tempfile.mkstemp()
1036+
(handle, fileName) = tempfile.mkstemp()
10051037
tmpFile = os.fdopen(handle, "w+")
10061038
if self.isWindows:
10071039
submitTemplate = submitTemplate.replace("\n", "\r\n")
10081040
separatorLine = separatorLine.replace("\n", "\r\n")
10091041
newdiff = newdiff.replace("\n", "\r\n")
10101042
tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
10111043
tmpFile.close()
1012-
mtime = os.stat(fileName).st_mtime
1013-
if os.environ.has_key("P4EDITOR"):
1014-
editor = os.environ.get("P4EDITOR")
1015-
else:
1016-
editor = read_pipe("git var GIT_EDITOR").strip()
1017-
system(editor + " " + fileName)
10181044

1019-
if gitConfig("git-p4.skipSubmitEditCheck") == "true":
1020-
checkModTime = False
1021-
else:
1022-
checkModTime = True
1023-
1024-
response = "y"
1025-
if checkModTime and (os.stat(fileName).st_mtime <= mtime):
1026-
response = "x"
1027-
while response != "y" and response != "n":
1028-
response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1029-
1030-
if response == "y":
1045+
if self.edit_template(fileName):
1046+
# read the edited message and submit
10311047
tmpFile = open(fileName, "rb")
10321048
message = tmpFile.read()
10331049
tmpFile.close()
@@ -1039,11 +1055,12 @@ class P4Submit(Command, P4UserMap):
10391055
if self.preserveUser:
10401056
if p4User:
10411057
# Get last changelist number. Cannot easily get it from
1042-
# the submit command output as the output is unmarshalled.
1058+
# the submit command output as the output is
1059+
# unmarshalled.
10431060
changelist = self.lastP4Changelist()
10441061
self.modifyChangelistUser(changelist, p4User)
1045-
10461062
else:
1063+
# skip this patch
10471064
for f in editedFiles:
10481065
p4_revert(f)
10491066
for f in filesToAdd:

contrib/fast-import/git-p4.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,24 @@ able to find the relevant client. This client spec will be used to
202202
both filter the files cloned by git and set the directory layout as
203203
specified in the client (this implies --keep-path style semantics).
204204

205-
git-p4.skipSubmitModTimeCheck
205+
git-p4.skipSubmitEdit
206206

207-
git config [--global] git-p4.skipSubmitModTimeCheck false
207+
git config [--global] git-p4.skipSubmitEdit false
208208

209-
If true, submit will not check if the p4 change template has been modified.
209+
Normally, git-p4 invokes an editor after each commit is applied so
210+
that you can make changes to the submit message. Setting this
211+
variable to true will skip the editing step, submitting the change as is.
212+
213+
git-p4.skipSubmitEditCheck
214+
215+
git config [--global] git-p4.skipSubmitEditCheck false
216+
217+
After the editor is invoked, git-p4 normally makes sure you saved the
218+
change description, as an indication that you did indeed read it over
219+
and edit it. You can quit without saving to abort the submit (or skip
220+
this change and continue). Setting this variable to true will cause
221+
git-p4 not to check if you saved the change description. This variable
222+
only matters if git-p4.skipSubmitEdit has not been set to true.
210223

211224
git-p4.preserveUser
212225

t/t9805-skip-submit-edit.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
3+
test_description='git-p4 skipSubmitEdit config variables'
4+
5+
. ./lib-git-p4.sh
6+
7+
test_expect_success 'start p4d' '
8+
start_p4d
9+
'
10+
11+
test_expect_success 'init depot' '
12+
(
13+
cd "$cli" &&
14+
echo file1 >file1 &&
15+
p4 add file1 &&
16+
p4 submit -d "change 1"
17+
)
18+
'
19+
20+
# this works because EDITOR is set to :
21+
test_expect_success 'no config, unedited, say yes' '
22+
"$GITP4" clone --dest="$git" //depot &&
23+
test_when_finished cleanup_git &&
24+
(
25+
cd "$git" &&
26+
echo line >>file1 &&
27+
git commit -a -m "change 2" &&
28+
echo y | "$GITP4" submit &&
29+
p4 changes //depot/... >wc &&
30+
test_line_count = 2 wc
31+
)
32+
'
33+
34+
test_expect_success 'no config, unedited, say no' '
35+
"$GITP4" clone --dest="$git" //depot &&
36+
test_when_finished cleanup_git &&
37+
(
38+
cd "$git" &&
39+
echo line >>file1 &&
40+
git commit -a -m "change 3 (not really)" &&
41+
printf "bad response\nn\n" | "$GITP4" submit
42+
p4 changes //depot/... >wc &&
43+
test_line_count = 2 wc
44+
)
45+
'
46+
47+
test_expect_success 'skipSubmitEdit' '
48+
"$GITP4" clone --dest="$git" //depot &&
49+
test_when_finished cleanup_git &&
50+
(
51+
cd "$git" &&
52+
git config git-p4.skipSubmitEdit true &&
53+
# will fail if editor is even invoked
54+
git config core.editor /bin/false &&
55+
echo line >>file1 &&
56+
git commit -a -m "change 3" &&
57+
"$GITP4" submit &&
58+
p4 changes //depot/... >wc &&
59+
test_line_count = 3 wc
60+
)
61+
'
62+
63+
test_expect_success 'skipSubmitEditCheck' '
64+
"$GITP4" clone --dest="$git" //depot &&
65+
test_when_finished cleanup_git &&
66+
(
67+
cd "$git" &&
68+
git config git-p4.skipSubmitEditCheck true &&
69+
echo line >>file1 &&
70+
git commit -a -m "change 4" &&
71+
"$GITP4" submit &&
72+
p4 changes //depot/... >wc &&
73+
test_line_count = 4 wc
74+
)
75+
'
76+
77+
78+
test_expect_success 'kill p4d' '
79+
kill_p4d
80+
'
81+
82+
test_done

0 commit comments

Comments
 (0)