]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3415-rebase-autosquash.sh
t3415: stop losing return codes of git commands
[thirdparty/git.git] / t / t3415-rebase-autosquash.sh
CommitLineData
f59baa50
NS
1#!/bin/sh
2
3test_description='auto squash'
4
5. ./test-lib.sh
6
22c5b136
AP
7. "$TEST_DIRECTORY"/lib-rebase.sh
8
f59baa50
NS
9test_expect_success setup '
10 echo 0 >file0 &&
11 git add . &&
12 test_tick &&
13 git commit -m "initial commit" &&
14 echo 0 >file1 &&
15 echo 2 >file2 &&
16 git add . &&
17 test_tick &&
18 git commit -m "first commit" &&
b1a6c0a9 19 git tag first-commit &&
f59baa50
NS
20 echo 3 >file3 &&
21 git add . &&
22 test_tick &&
23 git commit -m "second commit" &&
24 git tag base
25'
26
b1a6c0a9 27test_auto_fixup () {
f59baa50
NS
28 git reset --hard base &&
29 echo 1 >file1 &&
30 git add -u &&
31 test_tick &&
7c6eafa3 32 git commit -m "fixup! first" &&
f59baa50 33
dd1e5b31 34 git tag $1 &&
f59baa50 35 test_tick &&
dd1e5b31 36 git rebase $2 -i HEAD^^^ &&
f59baa50 37 git log --oneline >actual &&
3fb0459b 38 test_line_count = 3 actual &&
dd1e5b31 39 git diff --exit-code $1 &&
a781cd6f
DL
40 echo 1 >expect &&
41 git cat-file blob HEAD^:file1 >actual &&
42 test_cmp expect actual &&
43 git cat-file commit HEAD^ >commit &&
44 grep first commit >actual &&
45 test_line_count = 1 actual
dd1e5b31
HV
46}
47
48test_expect_success 'auto fixup (option)' '
49 test_auto_fixup final-fixup-option --autosquash
50'
51
52test_expect_success 'auto fixup (config)' '
53 git config rebase.autosquash true &&
54 test_auto_fixup final-fixup-config-true &&
55 test_must_fail test_auto_fixup fixup-config-true-no --no-autosquash &&
56 git config rebase.autosquash false &&
57 test_must_fail test_auto_fixup final-fixup-config-false
f59baa50
NS
58'
59
b1a6c0a9 60test_auto_squash () {
f59baa50
NS
61 git reset --hard base &&
62 echo 1 >file1 &&
63 git add -u &&
64 test_tick &&
7c6eafa3 65 git commit -m "squash! first" &&
f59baa50 66
dd1e5b31 67 git tag $1 &&
f59baa50 68 test_tick &&
dd1e5b31 69 git rebase $2 -i HEAD^^^ &&
f59baa50 70 git log --oneline >actual &&
3fb0459b 71 test_line_count = 3 actual &&
dd1e5b31 72 git diff --exit-code $1 &&
a781cd6f
DL
73 echo 1 >expect &&
74 git cat-file blob HEAD^:file1 >actual &&
75 test_cmp expect actual &&
76 git cat-file commit HEAD^ >commit &&
77 grep first commit >actual &&
78 test_line_count = 2 actual
dd1e5b31
HV
79}
80
81test_expect_success 'auto squash (option)' '
82 test_auto_squash final-squash --autosquash
83'
84
85test_expect_success 'auto squash (config)' '
86 git config rebase.autosquash true &&
87 test_auto_squash final-squash-config-true &&
88 test_must_fail test_auto_squash squash-config-true-no --no-autosquash &&
89 git config rebase.autosquash false &&
90 test_must_fail test_auto_squash final-squash-config-false
f59baa50
NS
91'
92
93test_expect_success 'misspelled auto squash' '
94 git reset --hard base &&
95 echo 1 >file1 &&
96 git add -u &&
97 test_tick &&
7c6eafa3 98 git commit -m "squash! forst" &&
f59baa50
NS
99 git tag final-missquash &&
100 test_tick &&
101 git rebase --autosquash -i HEAD^^^ &&
102 git log --oneline >actual &&
3fb0459b 103 test_line_count = 4 actual &&
f59baa50 104 git diff --exit-code final-missquash &&
a781cd6f
DL
105 git rev-list final-missquash...HEAD >list &&
106 test_must_be_empty list
f59baa50
NS
107'
108
d3d7a421
KB
109test_expect_success 'auto squash that matches 2 commits' '
110 git reset --hard base &&
111 echo 4 >file4 &&
112 git add file4 &&
113 test_tick &&
114 git commit -m "first new commit" &&
115 echo 1 >file1 &&
116 git add -u &&
117 test_tick &&
118 git commit -m "squash! first" &&
119 git tag final-multisquash &&
120 test_tick &&
121 git rebase --autosquash -i HEAD~4 &&
122 git log --oneline >actual &&
3fb0459b 123 test_line_count = 4 actual &&
d3d7a421 124 git diff --exit-code final-multisquash &&
a781cd6f
DL
125 echo 1 >expect &&
126 git cat-file blob HEAD^^:file1 >actual &&
127 test_cmp expect actual &&
128 git cat-file commit HEAD^^ >commit &&
129 grep first commit >actual &&
130 test_line_count = 2 actual &&
131 git cat-file commit HEAD >commit &&
132 grep first commit >actual &&
133 test_line_count = 1 actual
d3d7a421
KB
134'
135
136test_expect_success 'auto squash that matches a commit after the squash' '
137 git reset --hard base &&
138 echo 1 >file1 &&
139 git add -u &&
140 test_tick &&
141 git commit -m "squash! third" &&
142 echo 4 >file4 &&
143 git add file4 &&
144 test_tick &&
145 git commit -m "third commit" &&
146 git tag final-presquash &&
147 test_tick &&
148 git rebase --autosquash -i HEAD~4 &&
149 git log --oneline >actual &&
3fb0459b 150 test_line_count = 5 actual &&
d3d7a421 151 git diff --exit-code final-presquash &&
a781cd6f
DL
152 echo 0 >expect &&
153 git cat-file blob HEAD^^:file1 >actual &&
154 test_cmp expect actual &&
155 echo 1 >expect &&
156 git cat-file blob HEAD^:file1 >actual &&
157 test_cmp expect actual &&
158 git cat-file commit HEAD >commit &&
159 grep third commit >actual &&
160 test_line_count = 1 actual &&
161 git cat-file commit HEAD^ >commit &&
162 grep third commit >actual &&
163 test_line_count = 1 actual
d3d7a421 164'
68d5d03b
KB
165test_expect_success 'auto squash that matches a sha1' '
166 git reset --hard base &&
167 echo 1 >file1 &&
168 git add -u &&
169 test_tick &&
a781cd6f
DL
170 oid=$(git rev-parse --short HEAD^) &&
171 git commit -m "squash! $oid" &&
68d5d03b
KB
172 git tag final-shasquash &&
173 test_tick &&
174 git rebase --autosquash -i HEAD^^^ &&
175 git log --oneline >actual &&
3fb0459b 176 test_line_count = 3 actual &&
68d5d03b 177 git diff --exit-code final-shasquash &&
a781cd6f
DL
178 echo 1 >expect &&
179 git cat-file blob HEAD^:file1 >actual &&
180 test_cmp expect actual &&
181 git cat-file commit HEAD^ >commit &&
182 grep squash commit >actual &&
183 test_line_count = 1 actual
68d5d03b
KB
184'
185
186test_expect_success 'auto squash that matches longer sha1' '
187 git reset --hard base &&
188 echo 1 >file1 &&
189 git add -u &&
190 test_tick &&
a781cd6f
DL
191 oid=$(git rev-parse --short=11 HEAD^) &&
192 git commit -m "squash! $oid" &&
68d5d03b
KB
193 git tag final-longshasquash &&
194 test_tick &&
195 git rebase --autosquash -i HEAD^^^ &&
196 git log --oneline >actual &&
3fb0459b 197 test_line_count = 3 actual &&
68d5d03b 198 git diff --exit-code final-longshasquash &&
a781cd6f
DL
199 echo 1 >expect &&
200 git cat-file blob HEAD^:file1 >actual &&
201 test_cmp expect actual &&
202 git cat-file commit HEAD^ >commit &&
203 grep squash commit >actual &&
204 test_line_count = 1 actual
68d5d03b 205'
d3d7a421 206
b1a6c0a9
PN
207test_auto_commit_flags () {
208 git reset --hard base &&
209 echo 1 >file1 &&
210 git add -u &&
211 test_tick &&
212 git commit --$1 first-commit &&
213 git tag final-commit-$1 &&
214 test_tick &&
215 git rebase --autosquash -i HEAD^^^ &&
216 git log --oneline >actual &&
3fb0459b 217 test_line_count = 3 actual &&
b1a6c0a9 218 git diff --exit-code final-commit-$1 &&
a781cd6f
DL
219 echo 1 >expect &&
220 git cat-file blob HEAD^:file1 >actual &&
221 test_cmp expect actual &&
222 git cat-file commit HEAD^ >commit &&
223 grep first commit >actual &&
224 test_line_count = $2 actual
b1a6c0a9
PN
225}
226
227test_expect_success 'use commit --fixup' '
228 test_auto_commit_flags fixup 1
229'
230
7951bd30
PN
231test_expect_success 'use commit --squash' '
232 test_auto_commit_flags squash 2
233'
234
22c5b136
AP
235test_auto_fixup_fixup () {
236 git reset --hard base &&
237 echo 1 >file1 &&
238 git add -u &&
239 test_tick &&
240 git commit -m "$1! first" &&
241 echo 2 >file1 &&
242 git add -u &&
243 test_tick &&
244 git commit -m "$1! $2! first" &&
245 git tag "final-$1-$2" &&
246 test_tick &&
247 (
248 set_cat_todo_editor &&
249 test_must_fail git rebase --autosquash -i HEAD^^^^ >actual &&
a781cd6f
DL
250 head=$(git rev-parse --short HEAD) &&
251 parent1=$(git rev-parse --short HEAD^) &&
252 parent2=$(git rev-parse --short HEAD^^) &&
253 parent3=$(git rev-parse --short HEAD^^^) &&
22c5b136 254 cat >expected <<-EOF &&
a781cd6f
DL
255 pick $parent3 first commit
256 $1 $parent1 $1! first
257 $1 $head $1! $2! first
258 pick $parent2 second commit
22c5b136
AP
259 EOF
260 test_cmp expected actual
261 ) &&
262 git rebase --autosquash -i HEAD^^^^ &&
263 git log --oneline >actual &&
264 test_line_count = 3 actual
265 git diff --exit-code "final-$1-$2" &&
a781cd6f
DL
266 echo 2 >expect &&
267 git cat-file blob HEAD^:file1 >actual &&
268 test_cmp expect actual &&
269 git cat-file commit HEAD^ >commit &&
270 grep first commit >actual &&
22c5b136
AP
271 if test "$1" = "fixup"
272 then
a781cd6f 273 test_line_count = 1 actual
22c5b136
AP
274 elif test "$1" = "squash"
275 then
a781cd6f 276 test_line_count = 3 actual
22c5b136
AP
277 else
278 false
279 fi
280}
281
0d75bfe6 282test_expect_success C_LOCALE_OUTPUT 'fixup! fixup!' '
22c5b136
AP
283 test_auto_fixup_fixup fixup fixup
284'
285
0d75bfe6 286test_expect_success C_LOCALE_OUTPUT 'fixup! squash!' '
22c5b136
AP
287 test_auto_fixup_fixup fixup squash
288'
289
0d75bfe6 290test_expect_success C_LOCALE_OUTPUT 'squash! squash!' '
22c5b136
AP
291 test_auto_fixup_fixup squash squash
292'
293
0d75bfe6 294test_expect_success C_LOCALE_OUTPUT 'squash! fixup!' '
22c5b136
AP
295 test_auto_fixup_fixup squash fixup
296'
297
0d75bfe6 298test_expect_success C_LOCALE_OUTPUT 'autosquash with custom inst format' '
16cf51c7
MR
299 git reset --hard base &&
300 git config --add rebase.instructionFormat "[%an @ %ar] %s" &&
301 echo 2 >file1 &&
302 git add -u &&
303 test_tick &&
a781cd6f
DL
304 oid=$(git rev-parse --short HEAD^) &&
305 git commit -m "squash! $oid" &&
16cf51c7
MR
306 echo 1 >file1 &&
307 git add -u &&
308 test_tick &&
a781cd6f
DL
309 subject=$(git log -n 1 --format=%s HEAD~2) &&
310 git commit -m "squash! $subject" &&
16cf51c7
MR
311 git tag final-squash-instFmt &&
312 test_tick &&
313 git rebase --autosquash -i HEAD~4 &&
314 git log --oneline >actual &&
315 test_line_count = 3 actual &&
316 git diff --exit-code final-squash-instFmt &&
a781cd6f
DL
317 echo 1 >expect &&
318 git cat-file blob HEAD^:file1 >actual &&
319 test_cmp expect actual &&
320 git cat-file commit HEAD^ >commit &&
321 grep squash commit >actual &&
322 test_line_count = 2 actual
16cf51c7
MR
323'
324
4e7524e0
JS
325test_expect_success 'autosquash with empty custom instructionFormat' '
326 git reset --hard base &&
327 test_commit empty-instructionFormat-test &&
328 (
329 set_cat_todo_editor &&
330 test_must_fail git -c rebase.instructionFormat= \
f927ae6c 331 rebase --autosquash --force-rebase -i HEAD^ >actual &&
4e7524e0
JS
332 git log -1 --format="pick %h %s" >expect &&
333 test_cmp expect actual
334 )
335'
336
c94e963b
JS
337set_backup_editor () {
338 write_script backup-editor.sh <<-\EOF
339 cp "$1" .git/backup-"$(basename "$1")"
340 EOF
341 test_set_editor "$PWD/backup-editor.sh"
342}
343
c44a4c65 344test_expect_success 'autosquash with multiple empty patches' '
c94e963b
JS
345 test_tick &&
346 git commit --allow-empty -m "empty" &&
347 test_tick &&
348 git commit --allow-empty -m "empty2" &&
349 test_tick &&
350 >fixup &&
351 git add fixup &&
352 git commit --fixup HEAD^^ &&
353 (
354 set_backup_editor &&
355 GIT_USE_REBASE_HELPER=false \
356 git rebase -i --force-rebase --autosquash HEAD~4 &&
357 grep empty2 .git/backup-git-rebase-todo
358 )
359'
360
cbcd2cbd
JS
361test_expect_success 'extra spaces after fixup!' '
362 base=$(git rev-parse HEAD) &&
363 test_commit to-fixup &&
364 git commit --allow-empty -m "fixup! to-fixup" &&
365 git rebase -i --autosquash --keep-empty HEAD~2 &&
366 parent=$(git rev-parse HEAD^) &&
367 test $base = $parent
368'
369
b174ae7d
JS
370test_expect_success 'wrapped original subject' '
371 if test -d .git/rebase-merge; then git rebase --abort; fi &&
372 base=$(git rev-parse HEAD) &&
373 echo "wrapped subject" >wrapped &&
374 git add wrapped &&
375 test_tick &&
376 git commit --allow-empty -m "$(printf "To\nfixup")" &&
377 test_tick &&
378 git commit --allow-empty -m "fixup! To fixup" &&
379 git rebase -i --autosquash --keep-empty HEAD~2 &&
380 parent=$(git rev-parse HEAD^) &&
381 test $base = $parent
382'
383
10d2f354 384test_expect_success 'abort last squash' '
2f3eb68f
JS
385 test_when_finished "test_might_fail git rebase --abort" &&
386 test_when_finished "git checkout master" &&
387
388 git checkout -b some-squashes &&
389 git commit --allow-empty -m first &&
390 git commit --allow-empty --squash HEAD &&
391 git commit --allow-empty -m second &&
392 git commit --allow-empty --squash HEAD &&
393
394 test_must_fail git -c core.editor="grep -q ^pick" \
395 rebase -ki --autosquash HEAD~4 &&
396 : do not finish the squash, but resolve it manually &&
397 git commit --allow-empty --amend -m edited-first &&
398 git rebase --skip &&
399 git show >actual &&
400 ! grep first actual
401'
402
f59baa50 403test_done