Raw
1 #!/bin/sh
2
3 test_description='Merge-recursive merging renames'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 modify () {
10 sed -e "$1" <"$2" >"$2.x" &&
11 mv "$2.x" "$2"
12 }
13
14 test_expect_success 'setup' '
15 cat >A <<-\EOF &&
16 a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
17 b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
18 c cccccccccccccccccccccccccccccccccccccccccccccccc
19 d dddddddddddddddddddddddddddddddddddddddddddddddd
20 e eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
21 f ffffffffffffffffffffffffffffffffffffffffffffffff
22 g gggggggggggggggggggggggggggggggggggggggggggggggg
23 h hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
24 i iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
25 j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
26 k kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
27 l llllllllllllllllllllllllllllllllllllllllllllllll
28 m mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
29 n nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
30 o oooooooooooooooooooooooooooooooooooooooooooooooo
31 EOF
32
33 cat >M <<-\EOF &&
34 A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
35 B BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
36 C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
37 D DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
38 E EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
39 F FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
40 G GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
41 H HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
42 I IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
43 J JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
44 K KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
45 L LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
46 M MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
47 N NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
48 O OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
49 EOF
50
51 git add A M &&
52 git commit -m "initial has A and M" &&
53 git branch white &&
54 git branch red &&
55 git branch blue &&
56 git branch yellow &&
57 git branch change &&
58 git branch change+rename &&
59
60 sed -e "/^g /s/.*/g : main changes a line/" <A >A+ &&
61 mv A+ A &&
62 git commit -a -m "main updates A" &&
63
64 git checkout yellow &&
65 rm -f M &&
66 git commit -a -m "yellow removes M" &&
67
68 git checkout white &&
69 sed -e "/^g /s/.*/g : white changes a line/" <A >B &&
70 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
71 rm -f A M &&
72 git update-index --add --remove A B M N &&
73 git commit -m "white renames A->B, M->N" &&
74
75 git checkout red &&
76 sed -e "/^g /s/.*/g : red changes a line/" <A >B &&
77 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
78 rm -f A M &&
79 git update-index --add --remove A B M N &&
80 git commit -m "red renames A->B, M->N" &&
81
82 git checkout blue &&
83 sed -e "/^g /s/.*/g : blue changes a line/" <A >C &&
84 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
85 rm -f A M &&
86 git update-index --add --remove A C M N &&
87 git commit -m "blue renames A->C, M->N" &&
88
89 git checkout change &&
90 sed -e "/^g /s/.*/g : changed line/" <A >A+ &&
91 mv A+ A &&
92 git commit -q -a -m "changed" &&
93
94 git checkout change+rename &&
95 sed -e "/^g /s/.*/g : changed line/" <A >B &&
96 rm A &&
97 git update-index --add B &&
98 git commit -q -a -m "changed and renamed" &&
99
100 git checkout main
101 '
102
103 test_expect_success 'pull renaming branch into unrenaming one' \
104 '
105 git show-branch &&
106 test_expect_code 1 git pull --no-rebase . white &&
107 git ls-files -s &&
108 test_stdout_line_count = 3 git ls-files -u B &&
109 test_stdout_line_count = 1 git ls-files -s N &&
110 sed -ne "/^g/{
111 p
112 q
113 }" B | grep main &&
114 git diff --exit-code white N
115 '
116
117 test_expect_success 'pull renaming branch into another renaming one' \
118 '
119 rm -f B &&
120 git reset --hard &&
121 git checkout red &&
122 test_expect_code 1 git pull --no-rebase . white &&
123 test_stdout_line_count = 3 git ls-files -u B &&
124 test_stdout_line_count = 1 git ls-files -s N &&
125 sed -ne "/^g/{
126 p
127 q
128 }" B | grep red &&
129 git diff --exit-code white N
130 '
131
132 test_expect_success 'pull unrenaming branch into renaming one' \
133 '
134 git reset --hard &&
135 git show-branch &&
136 test_expect_code 1 git pull --no-rebase . main &&
137 test_stdout_line_count = 3 git ls-files -u B &&
138 test_stdout_line_count = 1 git ls-files -s N &&
139 sed -ne "/^g/{
140 p
141 q
142 }" B | grep red &&
143 git diff --exit-code white N
144 '
145
146 test_expect_success 'pull conflicting renames' \
147 '
148 git reset --hard &&
149 git show-branch &&
150 test_expect_code 1 git pull --no-rebase . blue &&
151 test_stdout_line_count = 1 git ls-files -u A &&
152 test_stdout_line_count = 1 git ls-files -u B &&
153 test_stdout_line_count = 1 git ls-files -u C &&
154 test_stdout_line_count = 1 git ls-files -s N &&
155 sed -ne "/^g/{
156 p
157 q
158 }" B | grep red &&
159 git diff --exit-code white N
160 '
161
162 test_expect_success 'interference with untracked working tree file' '
163 git reset --hard &&
164 git show-branch &&
165 echo >A this file should not matter &&
166 test_expect_code 1 git pull --no-rebase . white &&
167 test_path_is_file A
168 '
169
170 test_expect_success 'interference with untracked working tree file' '
171 git reset --hard &&
172 git checkout white &&
173 git show-branch &&
174 rm -f A &&
175 echo >A this file should not matter &&
176 test_expect_code 1 git pull --no-rebase . red &&
177 test_path_is_file A
178 '
179
180 test_expect_success 'interference with untracked working tree file' '
181 git reset --hard &&
182 rm -f A M &&
183 git checkout -f main &&
184 git tag -f anchor &&
185 git show-branch &&
186 git pull --no-rebase . yellow &&
187 test_path_is_missing M &&
188 git reset --hard anchor
189 '
190
191 test_expect_success 'updated working tree file should prevent the merge' '
192 git reset --hard &&
193 rm -f A M &&
194 git checkout -f main &&
195 git tag -f anchor &&
196 git show-branch &&
197 echo >>M one line addition &&
198 cat M >M.saved &&
199 test_expect_code 128 git pull . yellow &&
200 test_cmp M M.saved &&
201 rm -f M.saved
202 '
203
204 test_expect_success 'updated working tree file should prevent the merge' '
205 git reset --hard &&
206 rm -f A M &&
207 git checkout -f main &&
208 git tag -f anchor &&
209 git show-branch &&
210 echo >>M one line addition &&
211 cat M >M.saved &&
212 git update-index M &&
213 test_expect_code 2 git pull --no-rebase . yellow &&
214 test_cmp M M.saved &&
215 rm -f M.saved
216 '
217
218 test_expect_success 'interference with untracked working tree file' '
219 git reset --hard &&
220 rm -f A M &&
221 git checkout -f yellow &&
222 git tag -f anchor &&
223 git show-branch &&
224 echo >M this file should not matter &&
225 git pull --no-rebase . main &&
226 test_path_is_file M &&
227 ! {
228 git ls-files -s |
229 grep M
230 } &&
231 git reset --hard anchor
232 '
233
234 test_expect_success 'merge of identical changes in a renamed file' '
235 rm -f A M N &&
236 git reset --hard &&
237 git checkout change+rename &&
238
239 test-tool chmtime --get -3600 B >old-mtime &&
240 GIT_MERGE_VERBOSITY=3 git merge change >out &&
241
242 test-tool chmtime --get B >new-mtime &&
243 test_cmp old-mtime new-mtime &&
244
245 git reset --hard HEAD^ &&
246 git checkout change &&
247
248 # A will be renamed to B; we check mtimes and file presence
249 test_path_is_missing B &&
250 test-tool chmtime --get -3600 A >old-mtime &&
251 GIT_MERGE_VERBOSITY=3 git merge change+rename >out &&
252
253 test_path_is_missing A &&
254 test-tool chmtime --get B >new-mtime &&
255 test $(cat old-mtime) -lt $(cat new-mtime)
256 '
257
258 test_expect_success 'setup for rename + d/f conflicts' '
259 git reset --hard &&
260 git checkout --orphan dir-in-way &&
261 git rm -rf . &&
262 git clean -fdqx &&
263
264 mkdir sub &&
265 mkdir dir &&
266 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >sub/file &&
267 echo foo >dir/file-in-the-way &&
268 git add -A &&
269 git commit -m "Common commit" &&
270
271 echo 11 >>sub/file &&
272 echo more >>dir/file-in-the-way &&
273 git add -u &&
274 git commit -m "Commit to merge, with dir in the way" &&
275
276 git checkout -b dir-not-in-way &&
277 git reset --soft HEAD^ &&
278 git rm -rf dir &&
279 git commit -m "Commit to merge, with dir removed" -- dir sub/file &&
280
281 git checkout -b renamed-file-has-no-conflicts dir-in-way~1 &&
282 git rm -rf dir &&
283 git rm sub/file &&
284 printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n" >dir &&
285 git add dir &&
286 git commit -m "Independent change" &&
287
288 git checkout -b renamed-file-has-conflicts dir-in-way~1 &&
289 git rm -rf dir &&
290 git mv sub/file dir &&
291 echo 12 >>dir &&
292 git add dir &&
293 git commit -m "Conflicting change"
294 '
295
296 test_expect_success 'Rename+D/F conflict; renamed file merges + dir not in way' '
297 git reset --hard &&
298 git checkout -q renamed-file-has-no-conflicts^0 &&
299
300 git merge --strategy=recursive dir-not-in-way &&
301
302 git diff --quiet &&
303 test_path_is_file dir &&
304 test_write_lines 1 2 3 4 5555 6 7 8 9 10 11 >expected &&
305 test_cmp expected dir
306 '
307
308 test_expect_success 'Rename+D/F conflict; renamed file merges but dir in way' '
309 git reset --hard &&
310 rm -rf dir~* &&
311 git checkout -q renamed-file-has-no-conflicts^0 &&
312 test_must_fail git merge --strategy=recursive dir-in-way >output &&
313
314 test_grep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
315 test_grep "Auto-merging dir" output &&
316 test_grep "moving it to dir~HEAD instead" output &&
317
318 test_stdout_line_count = 3 git ls-files -u &&
319 test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
320
321 test_must_fail git diff --quiet &&
322 test_must_fail git diff --cached --quiet &&
323
324 test_path_is_file dir/file-in-the-way &&
325 test_path_is_file dir~HEAD &&
326 test_cmp expected dir~HEAD
327 '
328
329 test_expect_success 'Same as previous, but merged other way' '
330 git reset --hard &&
331 rm -rf dir~* &&
332 git checkout -q dir-in-way^0 &&
333 test_must_fail git merge --strategy=recursive renamed-file-has-no-conflicts >output 2>errors &&
334
335 ! grep "error: refusing to lose untracked file at" errors &&
336 test_grep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
337 test_grep "Auto-merging dir" output &&
338 test_grep "moving it to dir~renamed-file-has-no-conflicts instead" output &&
339
340 test_stdout_line_count = 3 git ls-files -u &&
341 test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
342
343 test_must_fail git diff --quiet &&
344 test_must_fail git diff --cached --quiet &&
345
346 test_path_is_file dir/file-in-the-way &&
347 test_path_is_file dir~renamed-file-has-no-conflicts &&
348 test_cmp expected dir~renamed-file-has-no-conflicts
349 '
350
351 test_expect_success 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
352 git reset --hard &&
353 rm -rf dir~* &&
354 git checkout -q renamed-file-has-conflicts^0 &&
355 test_must_fail git merge --strategy=recursive dir-not-in-way &&
356
357 test_stdout_line_count = 3 git ls-files -u &&
358 test_stdout_line_count = 3 git ls-files -u dir &&
359
360 test_must_fail git diff --quiet &&
361 test_must_fail git diff --cached --quiet &&
362
363 test_path_is_file dir &&
364 cat >expected <<-\EOF &&
365 1
366 2
367 3
368 4
369 5
370 6
371 7
372 8
373 9
374 10
375 <<<<<<< HEAD:dir
376 12
377 =======
378 11
379 >>>>>>> dir-not-in-way:sub/file
380 EOF
381 test_cmp expected dir
382 '
383
384 test_expect_success 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
385 modify s/dir-not-in-way/dir-in-way/ expected &&
386
387 git reset --hard &&
388 rm -rf dir~* &&
389 git checkout -q renamed-file-has-conflicts^0 &&
390 test_must_fail git merge --strategy=recursive dir-in-way &&
391
392 test_stdout_line_count = 5 git ls-files -u &&
393 test_stdout_line_count = 3 git ls-files -u dir~HEAD &&
394 test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
395
396 test_must_fail git diff --quiet &&
397 test_must_fail git diff --cached --quiet &&
398
399 test_path_is_file dir/file-in-the-way &&
400 test_path_is_file dir~HEAD &&
401 test_cmp expected dir~HEAD
402 '
403
404 test_expect_success 'Same as previous, but merged other way' '
405 git reset --hard &&
406 rm -rf dir~* &&
407 git checkout -q dir-in-way^0 &&
408 test_must_fail git merge --strategy=recursive renamed-file-has-conflicts &&
409
410 test_stdout_line_count = 5 git ls-files -u &&
411 test_stdout_line_count = 3 git ls-files -u dir~renamed-file-has-conflicts &&
412 test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
413
414 test_must_fail git diff --quiet &&
415 test_must_fail git diff --cached --quiet &&
416
417 test_path_is_file dir/file-in-the-way &&
418 test_path_is_file dir~renamed-file-has-conflicts &&
419 cat >expected <<-\EOF &&
420 1
421 2
422 3
423 4
424 5
425 6
426 7
427 8
428 9
429 10
430 <<<<<<< HEAD:sub/file
431 11
432 =======
433 12
434 >>>>>>> renamed-file-has-conflicts:dir
435 EOF
436 test_cmp expected dir~renamed-file-has-conflicts
437 '
438
439 test_expect_success 'setup both rename source and destination involved in D/F conflict' '
440 git reset --hard &&
441 git checkout --orphan rename-dest &&
442 git rm -rf . &&
443 git clean -fdqx &&
444
445 mkdir one &&
446 echo stuff >one/file &&
447 git add -A &&
448 git commit -m "Common commit" &&
449
450 git mv one/file destdir &&
451 git commit -m "Renamed to destdir" &&
452
453 git checkout -b source-conflict HEAD~1 &&
454 git rm -rf one &&
455 mkdir destdir &&
456 touch one destdir/foo &&
457 git add -A &&
458 git commit -m "Conflicts in the way"
459 '
460
461 test_expect_success 'both rename source and destination involved in D/F conflict' '
462 git reset --hard &&
463 rm -rf dir~* &&
464 git checkout -q rename-dest^0 &&
465 test_must_fail git merge --strategy=recursive source-conflict &&
466
467 test_stdout_line_count = 2 git ls-files -u &&
468
469 test_must_fail git diff --quiet &&
470
471 test_path_is_file destdir/foo &&
472 test_path_is_file one &&
473 test_path_is_file destdir~HEAD &&
474 test "stuff" = "$(cat destdir~HEAD)"
475 '
476
477 test_expect_success 'setup pair rename to parent of other (D/F conflicts)' '
478 git reset --hard &&
479 git checkout --orphan rename-two &&
480 git rm -rf . &&
481 git clean -fdqx &&
482
483 mkdir one &&
484 mkdir two &&
485 echo stuff >one/file &&
486 echo other >two/file &&
487 git add -A &&
488 git commit -m "Common commit" &&
489
490 git rm -rf one &&
491 git mv two/file one &&
492 git commit -m "Rename two/file -> one" &&
493
494 git checkout -b rename-one HEAD~1 &&
495 git rm -rf two &&
496 git mv one/file two &&
497 rm -r one &&
498 git commit -m "Rename one/file -> two"
499 '
500
501 test_expect_success 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
502 git checkout -q rename-one^0 &&
503 mkdir one &&
504 test_must_fail git merge --strategy=recursive rename-two &&
505
506 test_stdout_line_count = 4 git ls-files -u &&
507 test_stdout_line_count = 2 git ls-files -u one &&
508 test_stdout_line_count = 2 git ls-files -u two &&
509
510 test_must_fail git diff --quiet &&
511
512 test 3 -eq $(find . | grep -v .git | wc -l) &&
513
514 test_path_is_file one &&
515 test_path_is_file two &&
516 test "other" = $(cat one) &&
517 test "stuff" = $(cat two)
518 '
519
520 test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean start' '
521 git reset --hard &&
522 git clean -fdqx &&
523 test_must_fail git merge --strategy=recursive rename-two &&
524
525 test_stdout_line_count = 4 git ls-files -u &&
526 test_stdout_line_count = 2 git ls-files -u one &&
527 test_stdout_line_count = 2 git ls-files -u two &&
528
529 test_must_fail git diff --quiet &&
530
531 test 3 -eq $(find . | grep -v .git | wc -l) &&
532
533 test_path_is_file one &&
534 test_path_is_file two &&
535 test "other" = $(cat one) &&
536 test "stuff" = $(cat two)
537 '
538
539 test_expect_success 'setup rename of one file to two, with directories in the way' '
540 git reset --hard &&
541 git checkout --orphan first-rename &&
542 git rm -rf . &&
543 git clean -fdqx &&
544
545 echo stuff >original &&
546 git add -A &&
547 git commit -m "Common commit" &&
548
549 mkdir two &&
550 >two/file &&
551 git add two/file &&
552 git mv original one &&
553 git commit -m "Put two/file in the way, rename to one" &&
554
555 git checkout -b second-rename HEAD~1 &&
556 mkdir one &&
557 >one/file &&
558 git add one/file &&
559 git mv original two &&
560 git commit -m "Put one/file in the way, rename to two"
561 '
562
563 test_expect_success 'check handling of differently renamed file with D/F conflicts' '
564 git checkout -q first-rename^0 &&
565 test_must_fail git merge --strategy=recursive second-rename &&
566
567 test_stdout_line_count = 5 git ls-files -s &&
568 test_stdout_line_count = 3 git ls-files -u &&
569 test_stdout_line_count = 1 git ls-files -u one~HEAD &&
570 test_stdout_line_count = 1 git ls-files -u two~second-rename &&
571 test_stdout_line_count = 1 git ls-files -u original &&
572 test_stdout_line_count = 0 git ls-files -o &&
573
574 test_path_is_file one/file &&
575 test_path_is_file two/file &&
576 test_path_is_file one~HEAD &&
577 test_path_is_file two~second-rename &&
578 test_path_is_missing original
579 '
580
581 test_expect_success 'setup rename one file to two; directories moving out of the way' '
582 git reset --hard &&
583 git checkout --orphan first-rename-redo &&
584 git rm -rf . &&
585 git clean -fdqx &&
586
587 echo stuff >original &&
588 mkdir one two &&
589 touch one/file two/file &&
590 git add -A &&
591 git commit -m "Common commit" &&
592
593 git rm -rf one &&
594 git mv original one &&
595 git commit -m "Rename to one" &&
596
597 git checkout -b second-rename-redo HEAD~1 &&
598 git rm -rf two &&
599 git mv original two &&
600 git commit -m "Rename to two"
601 '
602
603 test_expect_success 'check handling of differently renamed file with D/F conflicts' '
604 git checkout -q first-rename-redo^0 &&
605 test_must_fail git merge --strategy=recursive second-rename-redo &&
606
607 test_stdout_line_count = 3 git ls-files -u &&
608 test_stdout_line_count = 1 git ls-files -u one &&
609 test_stdout_line_count = 1 git ls-files -u two &&
610 test_stdout_line_count = 1 git ls-files -u original &&
611 test_stdout_line_count = 0 git ls-files -o &&
612
613 test_path_is_file one &&
614 test_path_is_file two &&
615 test_path_is_missing original
616 '
617
618 test_expect_success 'setup avoid unnecessary update, normal rename' '
619 git reset --hard &&
620 git checkout --orphan avoid-unnecessary-update-1 &&
621 git rm -rf . &&
622 git clean -fdqx &&
623
624 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >original &&
625 git add -A &&
626 git commit -m "Common commit" &&
627
628 git mv original rename &&
629 echo 11 >>rename &&
630 git add -u &&
631 git commit -m "Renamed and modified" &&
632
633 git checkout -b merge-branch-1 HEAD~1 &&
634 echo "random content" >random-file &&
635 git add -A &&
636 git commit -m "Random, unrelated changes"
637 '
638
639 test_expect_success 'avoid unnecessary update, normal rename' '
640 git checkout -q avoid-unnecessary-update-1^0 &&
641 test-tool chmtime --get -3600 rename >expect &&
642 git merge merge-branch-1 &&
643 test-tool chmtime --get rename >actual &&
644 test_cmp expect actual # "rename" should have stayed intact
645 '
646
647 test_expect_success 'setup to test avoiding unnecessary update, with D/F conflict' '
648 git reset --hard &&
649 git checkout --orphan avoid-unnecessary-update-2 &&
650 git rm -rf . &&
651 git clean -fdqx &&
652
653 mkdir df &&
654 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >df/file &&
655 git add -A &&
656 git commit -m "Common commit" &&
657
658 git mv df/file temp &&
659 rm -rf df &&
660 git mv temp df &&
661 echo 11 >>df &&
662 git add -u &&
663 git commit -m "Renamed and modified" &&
664
665 git checkout -b merge-branch-2 HEAD~1 &&
666 >unrelated-change &&
667 git add unrelated-change &&
668 git commit -m "Only unrelated changes"
669 '
670
671 test_expect_success 'avoid unnecessary update, with D/F conflict' '
672 git checkout -q avoid-unnecessary-update-2^0 &&
673 test-tool chmtime --get -3600 df >expect &&
674 git merge merge-branch-2 &&
675 test-tool chmtime --get df >actual &&
676 test_cmp expect actual # "df" should have stayed intact
677 '
678
679 test_expect_success 'setup avoid unnecessary update, dir->(file,nothing)' '
680 git rm -rf . &&
681 git clean -fdqx &&
682 rm -rf .git &&
683 git init &&
684
685 >irrelevant &&
686 mkdir df &&
687 >df/file &&
688 git add -A &&
689 git commit -mA &&
690
691 git checkout -b side &&
692 git rm -rf df &&
693 git commit -mB &&
694
695 git checkout main &&
696 git rm -rf df &&
697 echo bla >df &&
698 git add -A &&
699 git commit -m "Add a newfile"
700 '
701
702 test_expect_success 'avoid unnecessary update, dir->(file,nothing)' '
703 git checkout -q main^0 &&
704 test-tool chmtime --get -3600 df >expect &&
705 git merge side &&
706 test-tool chmtime --get df >actual &&
707 test_cmp expect actual # "df" should have stayed intact
708 '
709
710 test_expect_success 'setup avoid unnecessary update, modify/delete' '
711 git rm -rf . &&
712 git clean -fdqx &&
713 rm -rf .git &&
714 git init &&
715
716 >irrelevant &&
717 >file &&
718 git add -A &&
719 git commit -mA &&
720
721 git checkout -b side &&
722 git rm -f file &&
723 git commit -m "Delete file" &&
724
725 git checkout main &&
726 echo bla >file &&
727 git add -A &&
728 git commit -m "Modify file"
729 '
730
731 test_expect_success 'avoid unnecessary update, modify/delete' '
732 git checkout -q main^0 &&
733 test-tool chmtime --get -3600 file >expect &&
734 test_must_fail git merge side &&
735 test-tool chmtime --get file >actual &&
736 test_cmp expect actual # "file" should have stayed intact
737 '
738
739 test_expect_success 'setup avoid unnecessary update, rename/add-dest' '
740 git rm -rf . &&
741 git clean -fdqx &&
742 rm -rf .git &&
743 git init &&
744
745 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >file &&
746 git add -A &&
747 git commit -mA &&
748
749 git checkout -b side &&
750 cp file newfile &&
751 git add -A &&
752 git commit -m "Add file copy" &&
753
754 git checkout main &&
755 git mv file newfile &&
756 git commit -m "Rename file"
757 '
758
759 test_expect_success 'avoid unnecessary update, rename/add-dest' '
760 git checkout -q main^0 &&
761 test-tool chmtime --get -3600 newfile >expect &&
762 git merge side &&
763 test-tool chmtime --get newfile >actual &&
764 test_cmp expect actual # "file" should have stayed intact
765 '
766
767 test_expect_success 'setup merge of rename + small change' '
768 git reset --hard &&
769 git checkout --orphan rename-plus-small-change &&
770 git rm -rf . &&
771 git clean -fdqx &&
772
773 echo ORIGINAL >file &&
774 git add file &&
775
776 test_tick &&
777 git commit -m Initial &&
778 git checkout -b rename_branch &&
779 git mv file renamed_file &&
780 git commit -m Rename &&
781 git checkout rename-plus-small-change &&
782 echo NEW-VERSION >file &&
783 git commit -a -m Reformat
784 '
785
786 test_expect_success 'merge rename + small change' '
787 git merge rename_branch &&
788
789 test_stdout_line_count = 1 git ls-files -s &&
790 test_stdout_line_count = 0 git ls-files -o &&
791 newhash=$(git rev-parse HEAD:renamed_file) &&
792 oldhash=$(git rev-parse HEAD~1:file) &&
793 test $newhash = $oldhash
794 '
795
796 test_expect_success 'setup for use of extended merge markers' '
797 git rm -rf . &&
798 git clean -fdqx &&
799 rm -rf .git &&
800 git init &&
801
802 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >original_file &&
803 git add original_file &&
804 git commit -mA &&
805
806 git checkout -b rename &&
807 echo 9 >>original_file &&
808 git add original_file &&
809 git mv original_file renamed_file &&
810 git commit -mB &&
811
812 git checkout main &&
813 echo 8.5 >>original_file &&
814 git add original_file &&
815 git commit -mC
816 '
817
818 test_expect_success 'merge main into rename has correct extended markers' '
819 git checkout rename^0 &&
820 test_must_fail git merge -s recursive main^0 &&
821
822 cat >expected <<-\EOF &&
823 1
824 2
825 3
826 4
827 5
828 6
829 7
830 8
831 <<<<<<< HEAD:renamed_file
832 9
833 =======
834 8.5
835 >>>>>>> main^0:original_file
836 EOF
837 test_cmp expected renamed_file
838 '
839
840 test_expect_success 'merge rename into main has correct extended markers' '
841 git reset --hard &&
842 git checkout main^0 &&
843 test_must_fail git merge -s recursive rename^0 &&
844
845 cat >expected <<-\EOF &&
846 1
847 2
848 3
849 4
850 5
851 6
852 7
853 8
854 <<<<<<< HEAD:original_file
855 8.5
856 =======
857 9
858 >>>>>>> rename^0:renamed_file
859 EOF
860 test_cmp expected renamed_file
861 '
862
863 test_expect_success 'setup spurious "refusing to lose untracked" message' '
864 git rm -rf . &&
865 git clean -fdqx &&
866 rm -rf .git &&
867 git init &&
868
869 > irrelevant_file &&
870 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >original_file &&
871 git add irrelevant_file original_file &&
872 git commit -mA &&
873
874 git checkout -b rename &&
875 git mv original_file renamed_file &&
876 git commit -mB &&
877
878 git checkout main &&
879 git rm original_file &&
880 git commit -mC
881 '
882
883 test_expect_success 'no spurious "refusing to lose untracked" message' '
884 git checkout main^0 &&
885 test_must_fail git merge rename^0 2>errors.txt &&
886 ! grep "refusing to lose untracked file" errors.txt
887 '
888
889 test_expect_success 'do not follow renames for empty files' '
890 git checkout -f -b empty-base &&
891 >empty1 &&
892 git add empty1 &&
893 git commit -m base &&
894 echo content >empty1 &&
895 git add empty1 &&
896 git commit -m fill &&
897 git checkout -b empty-topic HEAD^ &&
898 git mv empty1 empty2 &&
899 git commit -m rename &&
900 test_must_fail git merge empty-base &&
901 test_must_be_empty empty2
902 '
903
904 test_done