2 test_description
='Test "stg edit"'
6 test_expect_success
'Setup' '
7 printf "000\n111\n222\n333\n" >> foo &&
9 git commit -m "Initial commit" &&
10 sed "s/000/000xx/" foo > foo.tmp && mv foo.tmp foo &&
11 git commit -a -m "First change" &&
12 git notes add -m note1 &&
13 sed "s/111/111yy/" foo > foo.tmp && mv foo.tmp foo &&
14 git commit -a -m "Second change" &&
15 git notes add -m note2 &&
16 sed "s/222/222zz/" foo > foo.tmp && mv foo.tmp foo &&
17 git commit -a -m "Third change" &&
18 git notes add -m note3 &&
19 sed "s/333/333zz/" foo > foo.tmp && mv foo.tmp foo &&
20 git commit -a -m "Fourth change" &&
21 git notes add -m note4 &&
23 stg uncommit -n 4 p &&
26 test "$(echo $(stg series --all))" = "+ p1 > p2 - p3 ! p4"
29 # Commit parse functions.
30 msg
() { git cat-file
-p $1 |
sed '1,/^$/d' |
tr '\n' / |
sed 's,/*$,,' ; }
31 auth
() { git log
-n 1 --pretty=format
:"%an, %ae" $1 ; }
32 adate
() { git log
-n 1 --pretty=format
:%ai
$1 ; }
34 write_script diffedit
<<EOF
37 test_expect_success
'Empty editor aborts edit' '
38 EDITOR=./diffedit command_error stg edit 2>err &&
39 grep -e "Aborting edit due to empty patch description" err
43 write_script diffedit
<<EOF
44 sed 's/Empty patch/Empty Patch/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
46 test_expect_success
'Edit new patch with no diff' '
47 stg new -m "Empty patch" &&
48 test_when_finished stg delete empty-patch &&
49 EDITOR=./diffedit stg edit -d &&
50 test "$(msg HEAD)" = "Empty Patch"
54 test_expect_success
'Edit message of top patch' '
55 test "$(msg HEAD)" = "Second change" &&
56 stg edit p2 -m "Second change 2" &&
57 test "$(msg HEAD)" = "Second change 2" &&
58 test "$(git notes show $(stg id p2))" = "note2"
61 test_expect_success
'Edit message of non-top patch' '
62 test "$(msg HEAD^)" = "First change" &&
63 stg edit p1 -m "First change 2" &&
64 test "$(msg HEAD^)" = "First change 2" &&
65 test "$(git notes show $(stg id p1))" = "note1"
69 test_expect_success
'Edit message of unapplied patch' '
70 test "$(msg $(stg id p3))" = "Third change" &&
71 stg edit p3 -m "Third change 2" &&
72 test "$(msg $(stg id p3))" = "Third change 2" &&
73 test "$(git notes show $(stg id p3))" = "note3"
76 test_expect_success
'Edit message of hidden patch' '
77 test "$(msg $(stg id p4))" = "Fourth change" &&
78 stg edit p4 -m "Fourth change 2" &&
79 test "$(msg $(stg id p4))" = "Fourth change 2" &&
80 test "$(git notes show $(stg id p4))" = "note4"
83 test_expect_success
'Set patch message with --file <file>' '
84 test "$(msg HEAD)" = "Second change 2" &&
85 echo "Pride or Prejudice" > commitmsg &&
86 stg edit p2 -f commitmsg &&
87 test "$(msg HEAD)" = "Pride or Prejudice"
90 test_expect_success
'Set patch message with --file -' '
91 echo "Pride and Prejudice" | stg edit p2 -f - &&
92 test "$(msg HEAD)" = "Pride and Prejudice"
95 ( printf 'Patch: p2\nFrom: A Ú Thor <author@example.com>\nDate: <omitted>'
96 printf '\n\nPride and Prejudice\n'
97 printf '\n# Everything here is editable! You can modify the patch name, author,'
98 printf '\n# date, commit message, and the diff (if --diff was given).'
99 printf "\n# Lines starting with '#' will be ignored, and an empty message"
100 printf '\n# aborts the edit.\n'
102 omit_date
() { sed "s/^Date:.*$/Date: <omitted>/" ; }
104 test_expect_success
'Save template to file' '
105 stg edit --save-template saved-tmpl p2 &&
106 omit_date < saved-tmpl > saved-tmpl-d &&
107 test_cmp expected-tmpl saved-tmpl-d
110 test_expect_success
'Save template to stdout' '
111 stg edit --save-template - p2 > saved-tmpl2 &&
112 omit_date < saved-tmpl2 > saved-tmpl2-d &&
113 test_cmp expected-tmpl saved-tmpl2-d
116 # Test the various ways of invoking the interactive editor. The
117 # preference order should be
120 # 2. stgit.editor (legacy)
128 write_script
"$1" <<EOF
134 test_expect_success
'Edit commit message interactively (vi)' '
137 PATH=.:$PATH stg edit p2 &&
138 test "$(msg HEAD)" = "$m//vi"
142 test_expect_success
'Edit commit message interactively (EDITOR)' '
144 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
145 echo $m && echo $(msg HEAD) &&
146 test "$(msg HEAD)" = "$m//e1"
150 test_expect_success
'Edit commit message interactively (VISUAL)' '
152 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
153 test "$(msg HEAD)" = "$m//e2"
157 test_expect_success
'Edit commit message interactively (core.editor)' '
159 git config core.editor e3 &&
160 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
161 test "$(msg HEAD)" = "$m//e3"
165 test_expect_success
'Edit commit message interactively (stgit.editor)' '
167 git config stgit.editor e4 &&
168 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
169 test "$(msg HEAD)" = "$m//e4"
173 test_expect_success
'Edit commit message interactively (GIT_EDITOR)' '
175 GIT_EDITOR=./e5 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
176 test "$(msg HEAD)" = "$m//e5"
179 rm -f vi e1 e2 e3 e4 e5
180 git config
--unset core.editor
181 git config
--unset stgit.editor
184 test_expect_success
'Both noninterative and interactive editing' '
185 EDITOR=./twoliner stg edit -e -m "oneliner" p2 &&
186 test "$(msg HEAD)" = "oneliner//twoliner"
190 write_script diffedit
<<EOF
191 sed 's/111yy/111YY/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
193 test_expect_success
'Edit patch diff' '
194 EDITOR=./diffedit stg edit -d p2 &&
195 test "$(grep 111 foo)" = "111YY"
199 write_script diffedit
<<EOF
200 sed 's/+1,4/+1,5/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
202 test_expect_success
'Edit patch diff which fails to apply' '
203 EDITOR=./diffedit command_error stg edit -d p2 2>err &&
204 grep -e "Edited patch did not apply." err &&
205 test "$(grep 111 foo)" = "111YY" &&
206 test_file_not_empty .stgit-failed.patch
209 rm -f .stgit-failed.
patch
211 write_script diffedit
<<EOF
212 sed 's/Patch: p2/Patch: p2-new/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
214 test_expect_success
'Edit patch name of top patch' '
215 EDITOR=./diffedit stg edit -d &&
216 test "$(stg top)" = "p2-new" &&
217 test "$(stg series -c)" = "3" &&
222 write_script diffedit
<<EOF
223 sed 's/Patch: p1/Patch: p1-new/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
225 test_expect_success
'Edit patch name of non-top applied patch' '
226 EDITOR=./diffedit stg edit -d p1 &&
227 test "$(stg series --noprefix | head -n 1)" = "p1-new" &&
228 test "$(stg series -c)" = "3" &&
233 write_script diffedit
<<EOF
234 sed 's/Patch: p3/Patch: p3-new/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
236 test_expect_success
'Edit patch name of non-applied patch' '
237 EDITOR=./diffedit stg edit -d p3 &&
238 test "$(stg series --noprefix --unapplied)" = "p3-new" &&
239 test "$(stg series -c)" = "3" &&
244 write_script diffedit
<<EOF
245 sed 's/Patch: p4/Patch: p4-new/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
247 test_expect_success
'Edit patch name of hidden patch' '
248 EDITOR=./diffedit stg edit -d p4 &&
249 test "$(stg series --noprefix --hidden)" = "p4-new" &&
254 write_script diffedit
<<EOF
255 sed 's/Patch: p2/Patch:/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
257 test_expect_success
'Clearing the patch name results in a new autogenerated name' '
258 EDITOR=./diffedit stg edit -d &&
259 test "$(stg top)" = "oneliner" &&
260 test "$(stg series -c)" = "3" &&
261 stg rename oneliner p2
265 write_script diffedit
<<EOF
266 sed -e 's/Patch: p2/Patch: p2-new/' -e 's/twoliner/twoliner-new/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
268 test_expect_success
'Rename patch and edit its diff' '
269 EDITOR=./diffedit stg edit -d &&
270 test "$(stg top)" = "p2-new" &&
271 stg show | grep twoliner-new &&
276 test_expect_success
'Set author' '
277 stg edit p2 --author "Jane Austin <jaustin@example.com>" &&
278 test "$(auth HEAD)" = "Jane Austin, jaustin@example.com"
281 test_expect_success
'Fail to set broken author' '
282 command_error stg edit p2 --author "No Mail Address" &&
283 test "$(auth HEAD)" = "Jane Austin, jaustin@example.com"
286 test_expect_success
'Set author name' '
287 stg edit p2 --authname "Jane Austen" &&
288 test "$(auth HEAD)" = "Jane Austen, jaustin@example.com"
291 test_expect_success
'Set author email' '
292 stg edit p2 --authemail "jausten@example.com" &&
293 test "$(auth HEAD)" = "Jane Austen, jausten@example.com"
296 test_expect_success
'Set author date (RFC2822 format)' '
297 stg edit p2 --authdate "Wed, 10 Jul 2013 23:39:00 -0300" &&
298 test "$(adate HEAD)" = "2013-07-10 23:39:00 -0300"
301 test_expect_success
'Set author date (ISO 8601 format)' '
302 stg edit p2 --authdate "2013-01-28 22:30:00 -0300" &&
303 test "$(adate HEAD)" = "2013-01-28 22:30:00 -0300"
306 test_expect_success
'Fail to set invalid author date' '
307 command_error stg edit p2 --authdate "28 Jan 1813" &&
308 test "$(adate HEAD)" = "2013-01-28 22:30:00 -0300"
311 test_expect_success
'Set author date to "now"' '
312 before=$(date "+%F %T %z") &&
313 stg edit p2 --authdate now &&
314 after=$(date "+%F %T %z") &&
315 printf "$before\n$(adate HEAD)\n$after\n" | sort -c -
318 test_expect_success
'Set patch tree' '
319 p2tree=$(git log -1 --pretty=format:%T $(stg id p2)) &&
320 p4commit=$(stg id p4) &&
321 stg edit --set-tree $p4commit &&
322 test "$(git write-tree)" = "$(git rev-parse ${p4commit}^{tree})" &&
323 grep "^333zz$" foo &&
325 stg edit --set-tree $p2tree p2 &&
326 stg push --set-tree &&
327 test "$(git write-tree)" = "$p2tree" &&
329 stg edit --set-tree $p2tree p1 &&
330 test "$(echo $(stg series --empty --all))" = "+ p1 0> p2 - p3 ! p4" &&
331 test "$(git notes show $(stg id p1))" = "note1" &&
332 test "$(git notes show $(stg id p2))" = "note2" &&
333 test "$(git notes show $(stg id p3))" = "note3" &&
334 test "$(git notes show $(stg id p4))" = "note4"