summaryrefslogtreecommitdiff
path: root/t/t3452-history-split.sh
blob: 8ed0cebb5002960e0ee23e0b7037626b8e11041b (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#!/bin/sh

test_description='tests for git-history split subcommand'

. ./test-lib.sh
. "$TEST_DIRECTORY/lib-log-graph.sh"

# The fake editor takes multiple arguments, each of which represents a commit
# message. Subsequent invocations of the editor will then yield those messages
# in order.
#
set_fake_editor () {
	printf "%s\n" "$@" >fake-input &&
	write_script fake-editor.sh <<-\EOF &&
	head -n1 fake-input >"$1"
	sed 1d fake-input >fake-input.trimmed &&
	mv fake-input.trimmed fake-input
	EOF
	test_set_editor "$(pwd)"/fake-editor.sh
}

expect_graph () {
	cat >expect &&
	lib_test_cmp_graph --graph --format=%s "$@"
}

expect_log () {
	git log --format="%s" >actual &&
	cat >expect &&
	test_cmp expect actual
}

expect_tree_entries () {
	git ls-tree --name-only "$1" >actual &&
	cat >expect &&
	test_cmp expect actual
}

test_expect_success 'refuses to work with merge commits' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit base &&
		git branch branch &&
		test_commit ours &&
		git switch branch &&
		test_commit theirs &&
		git switch - &&
		git merge theirs &&
		test_must_fail git history split HEAD 2>err &&
		test_grep "cannot split up merge commit" err &&
		test_must_fail git history split HEAD~ 2>err &&
		test_grep "replaying merge commits is not supported yet" err
	)
'

test_expect_success 'errors on missing commit argument' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		test_must_fail git history split 2>err &&
		test_grep "command expects a committish" err
	)
'

test_expect_success 'errors on unknown revision' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		test_must_fail git history split does-not-exist 2>err &&
		test_grep "commit cannot be found" err
	)
'

test_expect_success '--dry-run does not modify any refs' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit base &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		git refs list --include-root-refs >before &&

		set_fake_editor "first" "second" &&
		git history split --dry-run HEAD <<-EOF &&
		y
		n
		EOF

		git refs list --include-root-refs >after &&
		test_cmp before after
	)
'

test_expect_success 'can split up tip commit' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		git symbolic-ref HEAD >expect &&
		set_fake_editor "first" "second" &&
		git history split HEAD <<-EOF &&
		y
		n
		EOF
		git symbolic-ref HEAD >actual &&
		test_cmp expect actual &&

		expect_log <<-EOF &&
		second
		first
		initial
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		bar
		initial.t
		EOF

		expect_tree_entries HEAD <<-EOF &&
		bar
		foo
		initial.t
		EOF

		git reflog >reflog &&
		test_grep "split: updating HEAD" reflog
	)
'

test_expect_success 'can split up root commit' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m root &&
		test_commit tip &&

		set_fake_editor "first" "second" &&
		git history split HEAD~ <<-EOF &&
		y
		n
		EOF

		expect_log <<-EOF &&
		tip
		second
		first
		EOF

		expect_tree_entries HEAD~2 <<-EOF &&
		bar
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		bar
		foo
		EOF

		expect_tree_entries HEAD <<-EOF
		bar
		foo
		tip.t
		EOF
	)
'

test_expect_success 'can split up in-between commit' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&
		test_commit tip &&

		set_fake_editor "first" "second" &&
		git history split HEAD~ <<-EOF &&
		y
		n
		EOF

		expect_log <<-EOF &&
		tip
		second
		first
		initial
		EOF

		expect_tree_entries HEAD~2 <<-EOF &&
		bar
		initial.t
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		bar
		foo
		initial.t
		EOF

		expect_tree_entries HEAD <<-EOF
		bar
		foo
		initial.t
		tip.t
		EOF
	)
'

test_expect_success 'can split HEAD only' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit base &&
		touch a b &&
		git add . &&
		git commit -m split-me &&
		git branch unrelated &&

		set_fake_editor "ours-a" "ours-b" &&
		git history split --update-refs=head HEAD <<-EOF &&
		y
		n
		EOF
		expect_graph --branches <<-EOF
		* ours-b
		* ours-a
		| * split-me
		|/
		* base
		EOF
	)
'

test_expect_success 'can split detached HEAD' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&
		git checkout --detach HEAD &&

		set_fake_editor "first" "second" &&
		git history split --update-refs=head HEAD <<-EOF &&
		y
		n
		EOF

		# HEAD should be detached and updated.
		test_must_fail git symbolic-ref HEAD &&

		expect_log <<-EOF
		second
		first
		initial
		EOF
	)
'

test_expect_success 'can split commit in unrelated branch' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit base &&
		git branch ours &&
		git switch --create theirs &&
		touch theirs-a theirs-b &&
		git add . &&
		git commit -m theirs &&
		git switch ours &&
		test_commit ours &&

		# With --update-refs=head it is not possible to split up a
		# commit that is unrelated to HEAD.
		test_must_fail git history split --update-refs=head theirs 2>err &&
		test_grep "rewritten commit must be an ancestor of HEAD" err &&

		set_fake_editor "theirs-rewritten-a" "theirs-rewritten-b" &&
		git history split theirs <<-EOF &&
		y
		n
		EOF
		expect_graph --branches <<-EOF &&
		* ours
		| * theirs-rewritten-b
		| * theirs-rewritten-a
		|/
		* base
		EOF

		expect_tree_entries theirs~ <<-EOF &&
		base.t
		theirs-a
		EOF

		expect_tree_entries theirs <<-EOF
		base.t
		theirs-a
		theirs-b
		EOF
	)
'

test_expect_success 'updates multiple descendant branches' '
	test_when_finished "rm -rf repo" &&
	git init repo --initial-branch=main &&
	(
		cd repo &&
		test_commit base &&
		touch file-a file-b &&
		git add . &&
		git commit -m split-me &&
		git branch branch &&
		test_commit on-main &&
		git switch branch &&
		test_commit on-branch &&
		git switch main &&

		set_fake_editor "split-a" "split-b" &&
		git history split HEAD~ <<-EOF &&
		y
		n
		EOF

		# Both branches should now descend from the split commits.
		expect_graph --branches <<-EOF
		* on-branch
		| * on-main
		|/
		* split-b
		* split-a
		* base
		EOF
	)
'

test_expect_success 'can pick multiple hunks' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar baz foo qux &&
		git add . &&
		git commit -m split-me &&

		set_fake_editor "first" "second" &&
		git history split HEAD <<-EOF &&
		y
		n
		y
		n
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		bar
		foo
		EOF

		expect_tree_entries HEAD <<-EOF
		bar
		baz
		foo
		qux
		EOF
	)
'

test_expect_success 'can use only last hunk' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		set_fake_editor "first" "second" &&
		git history split HEAD <<-EOF &&
		n
		y
		EOF

		expect_log <<-EOF &&
		second
		first
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		foo
		EOF

		expect_tree_entries HEAD <<-EOF
		bar
		foo
		EOF
	)
'

test_expect_success 'can split commit with file deletions' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		echo a >a &&
		echo b >b &&
		echo c >c &&
		git add . &&
		git commit -m base &&
		git rm a b &&
		git commit -m delete-both &&

		set_fake_editor "delete-a" "delete-b" &&
		git history split HEAD <<-EOF &&
		y
		n
		EOF

		expect_log <<-EOF &&
		delete-b
		delete-a
		base
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		b
		c
		EOF

		expect_tree_entries HEAD <<-EOF
		c
		EOF
	)
'

test_expect_success 'preserves original authorship' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		touch bar foo &&
		git add . &&
		GIT_AUTHOR_NAME="Other Author" \
		GIT_AUTHOR_EMAIL="other@example.com" \
		git commit -m split-me &&

		set_fake_editor "first" "second" &&
		git history split HEAD <<-EOF &&
		y
		n
		EOF

		git log -1 --format="%an <%ae>" HEAD~ >actual &&
		echo "Other Author <other@example.com>" >expect &&
		test_cmp expect actual &&

		git log -1 --format="%an <%ae>" HEAD >actual &&
		test_cmp expect actual
	)
'

test_expect_success 'aborts with empty commit message' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		set_fake_editor "" &&
		test_must_fail git history split HEAD <<-EOF 2>err &&
		y
		n
		EOF
		test_grep "Aborting commit due to empty commit message." err
	)
'

test_expect_success 'commit message editor sees split-out changes' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		write_script fake-editor.sh <<-\EOF &&
		cat "$1" >>MESSAGES &&
		echo "some commit message" >"$1"
		EOF
		test_set_editor "$(pwd)"/fake-editor.sh &&

		git history split HEAD <<-EOF &&
		y
		n
		EOF

		# Note that we expect to see the messages twice, once for each
		# of the commits. The committed files are different though.
		cat >expect <<-EOF &&
		split-me

		# Please enter the commit message for the split-out changes. Lines starting
		# with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
		# Changes to be committed:
		#	new file:   bar
		#
		split-me

		# Please enter the commit message for the split-out changes. Lines starting
		# with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
		# Changes to be committed:
		#	new file:   foo
		#
		EOF
		test_cmp expect MESSAGES &&

		expect_log <<-EOF
		some commit message
		some commit message
		EOF
	)
'

test_expect_success 'can use pathspec to limit what gets split' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		set_fake_editor "first" "second" &&
		git history split HEAD -- foo <<-EOF &&
		y
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		foo
		EOF

		expect_tree_entries HEAD <<-EOF
		bar
		foo
		EOF
	)
'

test_expect_success 'pathspec matching no files produces empty split error' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		set_fake_editor "first" "second" &&
		test_must_fail git history split HEAD -- nonexistent 2>err &&
		test_grep "split commit is empty" err
	)
'

test_expect_success 'split with multiple pathspecs' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit initial &&
		touch a b c d &&
		git add . &&
		git commit -m split-me &&

		# Only a and c should be offered for splitting.
		set_fake_editor "split-ac" "remainder" &&
		git history split HEAD -- a c <<-EOF &&
		y
		y
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		a
		c
		initial.t
		EOF

		expect_tree_entries HEAD <<-EOF
		a
		b
		c
		d
		initial.t
		EOF
	)
'

test_expect_success 'split with file mode change' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		echo content >script &&
		git add . &&
		git commit -m base &&
		test_chmod +x script &&
		echo change >script &&
		git commit -a -m "mode and content change" &&

		set_fake_editor "mode-change" "content-change" &&
		git history split HEAD <<-EOF &&
		y
		n
		EOF

		expect_log <<-EOF
		content-change
		mode-change
		base
		EOF
	)
'

test_expect_success 'refuses to create empty split-out commit' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		test_commit base &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		test_must_fail git history split HEAD 2>err <<-EOF &&
		n
		n
		EOF
		test_grep "split commit is empty" err
	)
'

test_expect_success 'hooks are not executed for rewritten commits' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&
		old_head=$(git rev-parse HEAD) &&

		ORIG_PATH="$(pwd)" &&
		export ORIG_PATH &&
		for hook in prepare-commit-msg pre-commit post-commit post-rewrite commit-msg
		do
			write_script .git/hooks/$hook <<-\EOF || exit 1
			touch "$ORIG_PATH"/hooks.log
			EOF
		done &&

		set_fake_editor "first" "second" &&
		git history split HEAD <<-EOF &&
		y
		n
		EOF

		expect_log <<-EOF &&
		second
		first
		EOF

		test_path_is_missing hooks.log
	)
'

test_expect_success 'refuses to create empty original commit' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		touch bar foo &&
		git add . &&
		git commit -m split-me &&

		test_must_fail git history split HEAD 2>err <<-EOF &&
		y
		y
		EOF
		test_grep "split commit tree matches original commit" err
	)
'

test_expect_success 'retains changes in the worktree and index' '
	test_when_finished "rm -rf repo" &&
	git init repo &&
	(
		cd repo &&
		echo a >a &&
		echo b >b &&
		git add . &&
		git commit -m "initial commit" &&
		echo a-modified >a &&
		echo b-modified >b &&
		git add b &&
		set_fake_editor "a-only" "remainder" &&
		git history split HEAD <<-EOF &&
		y
		n
		EOF

		expect_tree_entries HEAD~ <<-EOF &&
		a
		EOF
		expect_tree_entries HEAD <<-EOF &&
		a
		b
		EOF

		cat >expect <<-\EOF &&
		 M a
		M  b
		?? actual
		?? expect
		?? fake-editor.sh
		?? fake-input
		EOF
		git status --porcelain >actual &&
		test_cmp expect actual
	)
'

test_done