summaryrefslogtreecommitdiff
path: root/t/t3415-rebase-autosquash.sh
blob: e7087befd4e300edbd9b10d6935befbb1de23cc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/sh

test_description='auto squash'

GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME

. ./test-lib.sh

. "$TEST_DIRECTORY"/lib-rebase.sh

test_expect_success setup '
	echo 0 >file0 &&
	git add . &&
	test_tick &&
	git commit -m "initial commit" &&
	echo 0 >file1 &&
	echo 2 >file2 &&
	git add . &&
	test_tick &&
	git commit -m "first commit" &&
	git tag first-commit &&
	echo 3 >file3 &&
	git add . &&
	test_tick &&
	git commit -m "second commit" &&
	git tag base
'

test_auto_fixup () {
	no_squash= &&
	if test "x$1" = 'x!'
	then
		no_squash=true
		shift
	fi &&

	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "fixup! first" &&

	git tag $1 &&
	test_tick &&
	git rebase $2 -i HEAD^^^ &&
	git log --oneline >actual &&
	if test -n "$no_squash"
	then
		test_line_count = 4 actual
	else
		test_line_count = 3 actual &&
		git diff --exit-code $1 &&
		echo 1 >expect &&
		git cat-file blob HEAD^:file1 >actual &&
		test_cmp expect actual &&
		git cat-file commit HEAD^ >commit &&
		grep first commit >actual &&
		test_line_count = 1 actual
	fi
}

test_expect_success 'auto fixup (option)' '
	test_auto_fixup final-fixup-option --autosquash
'

test_expect_success 'auto fixup (config)' '
	git config rebase.autosquash true &&
	test_auto_fixup final-fixup-config-true &&
	test_auto_fixup ! fixup-config-true-no --no-autosquash &&
	git config rebase.autosquash false &&
	test_auto_fixup ! final-fixup-config-false
'

test_auto_squash () {
	no_squash= &&
	if test "x$1" = 'x!'
	then
		no_squash=true
		shift
	fi &&

	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "squash! first" &&

	git tag $1 &&
	test_tick &&
	git rebase $2 -i HEAD^^^ &&
	git log --oneline >actual &&
	if test -n "$no_squash"
	then
		test_line_count = 4 actual
	else
		test_line_count = 3 actual &&
		git diff --exit-code $1 &&
		echo 1 >expect &&
		git cat-file blob HEAD^:file1 >actual &&
		test_cmp expect actual &&
		git cat-file commit HEAD^ >commit &&
		grep first commit >actual &&
		test_line_count = 2 actual
	fi
}

test_expect_success 'auto squash (option)' '
	test_auto_squash final-squash --autosquash
'

test_expect_success 'auto squash (config)' '
	git config rebase.autosquash true &&
	test_auto_squash final-squash-config-true &&
	test_auto_squash ! squash-config-true-no --no-autosquash &&
	git config rebase.autosquash false &&
	test_auto_squash ! final-squash-config-false
'

test_expect_success 'misspelled auto squash' '
	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "squash! forst" &&
	git tag final-missquash &&
	test_tick &&
	git rebase --autosquash -i HEAD^^^ &&
	git log --oneline >actual &&
	test_line_count = 4 actual &&
	git diff --exit-code final-missquash &&
	git rev-list final-missquash...HEAD >list &&
	test_must_be_empty list
'

test_expect_success 'auto squash that matches 2 commits' '
	git reset --hard base &&
	echo 4 >file4 &&
	git add file4 &&
	test_tick &&
	git commit -m "first new commit" &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "squash! first" &&
	git tag final-multisquash &&
	test_tick &&
	git rebase --autosquash -i HEAD~4 &&
	git log --oneline >actual &&
	test_line_count = 4 actual &&
	git diff --exit-code final-multisquash &&
	echo 1 >expect &&
	git cat-file blob HEAD^^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD^^ >commit &&
	grep first commit >actual &&
	test_line_count = 2 actual &&
	git cat-file commit HEAD >commit &&
	grep first commit >actual &&
	test_line_count = 1 actual
'

test_expect_success 'auto squash that matches a commit after the squash' '
	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "squash! third" &&
	echo 4 >file4 &&
	git add file4 &&
	test_tick &&
	git commit -m "third commit" &&
	git tag final-presquash &&
	test_tick &&
	git rebase --autosquash -i HEAD~4 &&
	git log --oneline >actual &&
	test_line_count = 5 actual &&
	git diff --exit-code final-presquash &&
	echo 0 >expect &&
	git cat-file blob HEAD^^:file1 >actual &&
	test_cmp expect actual &&
	echo 1 >expect &&
	git cat-file blob HEAD^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD >commit &&
	grep third commit >actual &&
	test_line_count = 1 actual &&
	git cat-file commit HEAD^ >commit &&
	grep third commit >actual &&
	test_line_count = 1 actual
'
test_expect_success 'auto squash that matches a sha1' '
	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	oid=$(git rev-parse --short HEAD^) &&
	git commit -m "squash! $oid" &&
	git tag final-shasquash &&
	test_tick &&
	git rebase --autosquash -i HEAD^^^ &&
	git log --oneline >actual &&
	test_line_count = 3 actual &&
	git diff --exit-code final-shasquash &&
	echo 1 >expect &&
	git cat-file blob HEAD^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD^ >commit &&
	grep squash commit >actual &&
	test_line_count = 1 actual
'

test_expect_success 'auto squash that matches longer sha1' '
	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	oid=$(git rev-parse --short=11 HEAD^) &&
	git commit -m "squash! $oid" &&
	git tag final-longshasquash &&
	test_tick &&
	git rebase --autosquash -i HEAD^^^ &&
	git log --oneline >actual &&
	test_line_count = 3 actual &&
	git diff --exit-code final-longshasquash &&
	echo 1 >expect &&
	git cat-file blob HEAD^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD^ >commit &&
	grep squash commit >actual &&
	test_line_count = 1 actual
'

test_auto_commit_flags () {
	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit --$1 first-commit &&
	git tag final-commit-$1 &&
	test_tick &&
	git rebase --autosquash -i HEAD^^^ &&
	git log --oneline >actual &&
	test_line_count = 3 actual &&
	git diff --exit-code final-commit-$1 &&
	echo 1 >expect &&
	git cat-file blob HEAD^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD^ >commit &&
	grep first commit >actual &&
	test_line_count = $2 actual
}

test_expect_success 'use commit --fixup' '
	test_auto_commit_flags fixup 1
'

test_expect_success 'use commit --squash' '
	test_auto_commit_flags squash 2
'

test_auto_fixup_fixup () {
	git reset --hard base &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "$1! first" &&
	echo 2 >file1 &&
	git add -u &&
	test_tick &&
	git commit -m "$1! $2! first" &&
	git tag "final-$1-$2" &&
	test_tick &&
	(
		set_cat_todo_editor &&
		test_must_fail git rebase --autosquash -i HEAD^^^^ >actual &&
		head=$(git rev-parse --short HEAD) &&
		parent1=$(git rev-parse --short HEAD^) &&
		parent2=$(git rev-parse --short HEAD^^) &&
		parent3=$(git rev-parse --short HEAD^^^) &&
		cat >expected <<-EOF &&
		pick $parent3 first commit
		$1 $parent1 $1! first
		$1 $head $1! $2! first
		pick $parent2 second commit
		EOF
		test_cmp expected actual
	) &&
	git rebase --autosquash -i HEAD^^^^ &&
	git log --oneline >actual &&
	test_line_count = 3 actual
	git diff --exit-code "final-$1-$2" &&
	echo 2 >expect &&
	git cat-file blob HEAD^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD^ >commit &&
	grep first commit >actual &&
	if test "$1" = "fixup"
	then
		test_line_count = 1 actual
	elif test "$1" = "squash"
	then
		test_line_count = 3 actual
	else
		false
	fi
}

test_expect_success C_LOCALE_OUTPUT 'fixup! fixup!' '
	test_auto_fixup_fixup fixup fixup
'

test_expect_success C_LOCALE_OUTPUT 'fixup! squash!' '
	test_auto_fixup_fixup fixup squash
'

test_expect_success C_LOCALE_OUTPUT 'squash! squash!' '
	test_auto_fixup_fixup squash squash
'

test_expect_success C_LOCALE_OUTPUT 'squash! fixup!' '
	test_auto_fixup_fixup squash fixup
'

test_expect_success C_LOCALE_OUTPUT 'autosquash with custom inst format' '
	git reset --hard base &&
	git config --add rebase.instructionFormat "[%an @ %ar] %s"  &&
	echo 2 >file1 &&
	git add -u &&
	test_tick &&
	oid=$(git rev-parse --short HEAD^) &&
	git commit -m "squash! $oid" &&
	echo 1 >file1 &&
	git add -u &&
	test_tick &&
	subject=$(git log -n 1 --format=%s HEAD~2) &&
	git commit -m "squash! $subject" &&
	git tag final-squash-instFmt &&
	test_tick &&
	git rebase --autosquash -i HEAD~4 &&
	git log --oneline >actual &&
	test_line_count = 3 actual &&
	git diff --exit-code final-squash-instFmt &&
	echo 1 >expect &&
	git cat-file blob HEAD^:file1 >actual &&
	test_cmp expect actual &&
	git cat-file commit HEAD^ >commit &&
	grep squash commit >actual &&
	test_line_count = 2 actual
'

test_expect_success 'autosquash with empty custom instructionFormat' '
	git reset --hard base &&
	test_commit empty-instructionFormat-test &&
	(
		set_cat_todo_editor &&
		test_must_fail git -c rebase.instructionFormat= \
			rebase --autosquash  --force-rebase -i HEAD^ >actual &&
		git log -1 --format="pick %h %s" >expect &&
		test_cmp expect actual
	)
'

set_backup_editor () {
	write_script backup-editor.sh <<-\EOF
	cp "$1" .git/backup-"$(basename "$1")"
	EOF
	test_set_editor "$PWD/backup-editor.sh"
}

test_expect_success 'autosquash with multiple empty patches' '
	test_tick &&
	git commit --allow-empty -m "empty" &&
	test_tick &&
	git commit --allow-empty -m "empty2" &&
	test_tick &&
	>fixup &&
	git add fixup &&
	git commit --fixup HEAD^^ &&
	(
		set_backup_editor &&
		GIT_USE_REBASE_HELPER=false \
		git rebase -i --force-rebase --autosquash HEAD~4 &&
		grep empty2 .git/backup-git-rebase-todo
	)
'

test_expect_success 'extra spaces after fixup!' '
	base=$(git rev-parse HEAD) &&
	test_commit to-fixup &&
	git commit --allow-empty -m "fixup!  to-fixup" &&
	git rebase -i --autosquash --keep-empty HEAD~2 &&
	parent=$(git rev-parse HEAD^) &&
	test $base = $parent
'

test_expect_success 'wrapped original subject' '
	if test -d .git/rebase-merge; then git rebase --abort; fi &&
	base=$(git rev-parse HEAD) &&
	echo "wrapped subject" >wrapped &&
	git add wrapped &&
	test_tick &&
	git commit --allow-empty -m "$(printf "To\nfixup")" &&
	test_tick &&
	git commit --allow-empty -m "fixup! To fixup" &&
	git rebase -i --autosquash --keep-empty HEAD~2 &&
	parent=$(git rev-parse HEAD^) &&
	test $base = $parent
'

test_expect_success 'abort last squash' '
	test_when_finished "test_might_fail git rebase --abort" &&
	test_when_finished "git checkout main" &&

	git checkout -b some-squashes &&
	git commit --allow-empty -m first &&
	git commit --allow-empty --squash HEAD &&
	git commit --allow-empty -m second &&
	git commit --allow-empty --squash HEAD &&

	test_must_fail git -c core.editor="grep -q ^pick" \
		rebase -ki --autosquash HEAD~4 &&
	: do not finish the squash, but resolve it manually &&
	git commit --allow-empty --amend -m edited-first &&
	git rebase --skip &&
	git show >actual &&
	! grep first actual
'

test_expect_success 'fixup a fixup' '
	echo 0to-fixup >file0 &&
	test_tick &&
	git commit -m "to-fixup" file0 &&
	test_tick &&
	git commit --squash HEAD -m X --allow-empty &&
	test_tick &&
	git commit --squash HEAD^ -m Y --allow-empty &&
	test_tick &&
	git commit -m "squash! $(git rev-parse HEAD^)" -m Z --allow-empty &&
	test_tick &&
	git commit -m "squash! $(git rev-parse HEAD^^)" -m W --allow-empty &&
	git rebase -ki --autosquash HEAD~5 &&
	test XZWY = $(git show | tr -cd W-Z)
'

test_done