merge-recursive: Move handling of double rename of one file to other file
[git/jnareb-git.git] / t / t6022-merge-rename.sh
blob1f29c0a94d672d75b76743ba8a8edf1558401987
1 #!/bin/sh
3 test_description='Merge-recursive merging renames'
4 . ./test-lib.sh
6 modify () {
7 sed -e "$1" <"$2" >"$2.x" &&
8 mv "$2.x" "$2"
11 test_expect_success setup \
13 cat >A <<\EOF &&
14 a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
15 b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
16 c cccccccccccccccccccccccccccccccccccccccccccccccc
17 d dddddddddddddddddddddddddddddddddddddddddddddddd
18 e eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
19 f ffffffffffffffffffffffffffffffffffffffffffffffff
20 g gggggggggggggggggggggggggggggggggggggggggggggggg
21 h hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
22 i iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
23 j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
24 k kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
25 l llllllllllllllllllllllllllllllllllllllllllllllll
26 m mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
27 n nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
28 o oooooooooooooooooooooooooooooooooooooooooooooooo
29 EOF
31 cat >M <<\EOF &&
32 A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
33 B BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
34 C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
35 D DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
36 E EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
37 F FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
38 G GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
39 H HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
40 I IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
41 J JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
42 K KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
43 L LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
44 M MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
45 N NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
46 O OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
47 EOF
49 git add A M &&
50 git commit -m "initial has A and M" &&
51 git branch white &&
52 git branch red &&
53 git branch blue &&
54 git branch yellow &&
55 git branch change &&
56 git branch change+rename &&
58 sed -e "/^g /s/.*/g : master changes a line/" <A >A+ &&
59 mv A+ A &&
60 git commit -a -m "master updates A" &&
62 git checkout yellow &&
63 rm -f M &&
64 git commit -a -m "yellow removes M" &&
66 git checkout white &&
67 sed -e "/^g /s/.*/g : white changes a line/" <A >B &&
68 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
69 rm -f A M &&
70 git update-index --add --remove A B M N &&
71 git commit -m "white renames A->B, M->N" &&
73 git checkout red &&
74 sed -e "/^g /s/.*/g : red changes a line/" <A >B &&
75 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
76 rm -f A M &&
77 git update-index --add --remove A B M N &&
78 git commit -m "red renames A->B, M->N" &&
80 git checkout blue &&
81 sed -e "/^g /s/.*/g : blue changes a line/" <A >C &&
82 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
83 rm -f A M &&
84 git update-index --add --remove A C M N &&
85 git commit -m "blue renames A->C, M->N" &&
87 git checkout change &&
88 sed -e "/^g /s/.*/g : changed line/" <A >A+ &&
89 mv A+ A &&
90 git commit -q -a -m "changed" &&
92 git checkout change+rename &&
93 sed -e "/^g /s/.*/g : changed line/" <A >B &&
94 rm A &&
95 git update-index --add B &&
96 git commit -q -a -m "changed and renamed" &&
98 git checkout master'
100 test_expect_success 'pull renaming branch into unrenaming one' \
102 git show-branch
103 git pull . white && {
104 echo "BAD: should have conflicted"
105 return 1
107 git ls-files -s
108 test "$(git ls-files -u B | wc -l)" -eq 3 || {
109 echo "BAD: should have left stages for B"
110 return 1
112 test "$(git ls-files -s N | wc -l)" -eq 1 || {
113 echo "BAD: should have merged N"
114 return 1
116 sed -ne "/^g/{
119 }" B | grep master || {
120 echo "BAD: should have listed our change first"
121 return 1
123 test "$(git diff white N | wc -l)" -eq 0 || {
124 echo "BAD: should have taken colored branch"
125 return 1
129 test_expect_success 'pull renaming branch into another renaming one' \
131 rm -f B
132 git reset --hard
133 git checkout red
134 git pull . white && {
135 echo "BAD: should have conflicted"
136 return 1
138 test "$(git ls-files -u B | wc -l)" -eq 3 || {
139 echo "BAD: should have left stages"
140 return 1
142 test "$(git ls-files -s N | wc -l)" -eq 1 || {
143 echo "BAD: should have merged N"
144 return 1
146 sed -ne "/^g/{
149 }" B | grep red || {
150 echo "BAD: should have listed our change first"
151 return 1
153 test "$(git diff white N | wc -l)" -eq 0 || {
154 echo "BAD: should have taken colored branch"
155 return 1
159 test_expect_success 'pull unrenaming branch into renaming one' \
161 git reset --hard
162 git show-branch
163 git pull . master && {
164 echo "BAD: should have conflicted"
165 return 1
167 test "$(git ls-files -u B | wc -l)" -eq 3 || {
168 echo "BAD: should have left stages"
169 return 1
171 test "$(git ls-files -s N | wc -l)" -eq 1 || {
172 echo "BAD: should have merged N"
173 return 1
175 sed -ne "/^g/{
178 }" B | grep red || {
179 echo "BAD: should have listed our change first"
180 return 1
182 test "$(git diff white N | wc -l)" -eq 0 || {
183 echo "BAD: should have taken colored branch"
184 return 1
188 test_expect_success 'pull conflicting renames' \
190 git reset --hard
191 git show-branch
192 git pull . blue && {
193 echo "BAD: should have conflicted"
194 return 1
196 test "$(git ls-files -u A | wc -l)" -eq 1 || {
197 echo "BAD: should have left a stage"
198 return 1
200 test "$(git ls-files -u B | wc -l)" -eq 1 || {
201 echo "BAD: should have left a stage"
202 return 1
204 test "$(git ls-files -u C | wc -l)" -eq 1 || {
205 echo "BAD: should have left a stage"
206 return 1
208 test "$(git ls-files -s N | wc -l)" -eq 1 || {
209 echo "BAD: should have merged N"
210 return 1
212 sed -ne "/^g/{
215 }" B | grep red || {
216 echo "BAD: should have listed our change first"
217 return 1
219 test "$(git diff white N | wc -l)" -eq 0 || {
220 echo "BAD: should have taken colored branch"
221 return 1
225 test_expect_success 'interference with untracked working tree file' '
227 git reset --hard
228 git show-branch
229 echo >A this file should not matter
230 git pull . white && {
231 echo "BAD: should have conflicted"
232 return 1
234 test -f A || {
235 echo "BAD: should have left A intact"
236 return 1
240 test_expect_success 'interference with untracked working tree file' '
242 git reset --hard
243 git checkout white
244 git show-branch
245 rm -f A
246 echo >A this file should not matter
247 git pull . red && {
248 echo "BAD: should have conflicted"
249 return 1
251 test -f A || {
252 echo "BAD: should have left A intact"
253 return 1
257 test_expect_success 'interference with untracked working tree file' '
259 git reset --hard
260 rm -f A M
261 git checkout -f master
262 git tag -f anchor
263 git show-branch
264 git pull . yellow || {
265 echo "BAD: should have cleanly merged"
266 return 1
268 test -f M && {
269 echo "BAD: should have removed M"
270 return 1
272 git reset --hard anchor
275 test_expect_success 'updated working tree file should prevent the merge' '
277 git reset --hard
278 rm -f A M
279 git checkout -f master
280 git tag -f anchor
281 git show-branch
282 echo >>M one line addition
283 cat M >M.saved
284 git pull . yellow && {
285 echo "BAD: should have complained"
286 return 1
288 test_cmp M M.saved || {
289 echo "BAD: should have left M intact"
290 return 1
292 rm -f M.saved
295 test_expect_success 'updated working tree file should prevent the merge' '
297 git reset --hard
298 rm -f A M
299 git checkout -f master
300 git tag -f anchor
301 git show-branch
302 echo >>M one line addition
303 cat M >M.saved
304 git update-index M
305 git pull . yellow && {
306 echo "BAD: should have complained"
307 return 1
309 test_cmp M M.saved || {
310 echo "BAD: should have left M intact"
311 return 1
313 rm -f M.saved
316 test_expect_success 'interference with untracked working tree file' '
318 git reset --hard
319 rm -f A M
320 git checkout -f yellow
321 git tag -f anchor
322 git show-branch
323 echo >M this file should not matter
324 git pull . master || {
325 echo "BAD: should have cleanly merged"
326 return 1
328 test -f M || {
329 echo "BAD: should have left M intact"
330 return 1
332 git ls-files -s | grep M && {
333 echo "BAD: M must be untracked in the result"
334 return 1
336 git reset --hard anchor
339 test_expect_success 'merge of identical changes in a renamed file' '
340 rm -f A M N
341 git reset --hard &&
342 git checkout change+rename &&
343 GIT_MERGE_VERBOSITY=3 git merge change | grep "^Skipped B" &&
344 git reset --hard HEAD^ &&
345 git checkout change &&
346 GIT_MERGE_VERBOSITY=3 git merge change+rename | grep "^Skipped B"
349 test_expect_success 'setup for rename + d/f conflicts' '
350 git reset --hard &&
351 git checkout --orphan dir-in-way &&
352 git rm -rf . &&
354 mkdir sub &&
355 mkdir dir &&
356 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >sub/file &&
357 echo foo >dir/file-in-the-way &&
358 git add -A &&
359 git commit -m "Common commmit" &&
361 echo 11 >>sub/file &&
362 echo more >>dir/file-in-the-way &&
363 git add -u &&
364 git commit -m "Commit to merge, with dir in the way" &&
366 git checkout -b dir-not-in-way &&
367 git reset --soft HEAD^ &&
368 git rm -rf dir &&
369 git commit -m "Commit to merge, with dir removed" -- dir sub/file &&
371 git checkout -b renamed-file-has-no-conflicts dir-in-way~1 &&
372 git rm -rf dir &&
373 git rm sub/file &&
374 printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n" >dir &&
375 git add dir &&
376 git commit -m "Independent change" &&
378 git checkout -b renamed-file-has-conflicts dir-in-way~1 &&
379 git rm -rf dir &&
380 git mv sub/file dir &&
381 echo 12 >>dir &&
382 git add dir &&
383 git commit -m "Conflicting change"
386 printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n11\n" >expected
388 test_expect_success 'Rename+D/F conflict; renamed file merges + dir not in way' '
389 git reset --hard &&
390 git checkout -q renamed-file-has-no-conflicts^0 &&
391 git merge --strategy=recursive dir-not-in-way &&
392 git diff --quiet &&
393 test -f dir &&
394 test_cmp expected dir
397 test_expect_failure 'Rename+D/F conflict; renamed file merges but dir in way' '
398 git reset --hard &&
399 rm -rf dir~* &&
400 git checkout -q renamed-file-has-no-conflicts^0 &&
401 test_must_fail git merge --strategy=recursive dir-in-way >output &&
403 grep "CONFLICT (delete/modify): dir/file-in-the-way" output &&
404 grep "Auto-merging dir" output &&
405 grep "Adding as dir~HEAD instead" output &&
407 test 2 = "$(git ls-files -u | wc -l)" &&
408 test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
410 test_must_fail git diff --quiet &&
411 test_must_fail git diff --cached --quiet &&
413 test -f dir/file-in-the-way &&
414 test -f dir~HEAD &&
415 test_cmp expected dir~HEAD
418 test_expect_failure 'Same as previous, but merged other way' '
419 git reset --hard &&
420 rm -rf dir~* &&
421 git checkout -q dir-in-way^0 &&
422 test_must_fail git merge --strategy=recursive renamed-file-has-no-conflicts >output 2>errors &&
424 ! grep "error: refusing to lose untracked file at" errors &&
425 grep "CONFLICT (delete/modify): dir/file-in-the-way" output &&
426 grep "Auto-merging dir" output &&
427 grep "Adding as dir~renamed-file-has-no-conflicts instead" output &&
429 test 2 = "$(git ls-files -u | wc -l)" &&
430 test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
432 test_must_fail git diff --quiet &&
433 test_must_fail git diff --cached --quiet &&
435 test -f dir/file-in-the-way &&
436 test -f dir~renamed-file-has-no-conflicts &&
437 test_cmp expected dir~renamed-file-has-no-conflicts
440 cat >expected <<\EOF &&
451 <<<<<<< HEAD
453 =======
455 >>>>>>> dir-not-in-way
458 test_expect_failure 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
459 git reset --hard &&
460 rm -rf dir~* &&
461 git checkout -q renamed-file-has-conflicts^0 &&
462 test_must_fail git merge --strategy=recursive dir-not-in-way &&
464 test 3 = "$(git ls-files -u | wc -l)" &&
465 test 3 = "$(git ls-files -u dir | wc -l)" &&
467 test_must_fail git diff --quiet &&
468 test_must_fail git diff --cached --quiet &&
470 test -f dir &&
471 test_cmp expected dir
474 test_expect_failure 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
475 modify s/dir-not-in-way/dir-in-way/ expected &&
477 git reset --hard &&
478 rm -rf dir~* &&
479 git checkout -q renamed-file-has-conflicts^0 &&
480 test_must_fail git merge --strategy=recursive dir-in-way &&
482 test 5 = "$(git ls-files -u | wc -l)" &&
483 test 3 = "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)" &&
484 test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
486 test_must_fail git diff --quiet &&
487 test_must_fail git diff --cached --quiet &&
489 test -f dir/file-in-the-way &&
490 test -f dir~HEAD &&
491 test_cmp expected dir~HEAD
494 cat >expected <<\EOF &&
505 <<<<<<< HEAD
507 =======
509 >>>>>>> renamed-file-has-conflicts
512 test_expect_failure 'Same as previous, but merged other way' '
513 git reset --hard &&
514 rm -rf dir~* &&
515 git checkout -q dir-in-way^0 &&
516 test_must_fail git merge --strategy=recursive renamed-file-has-conflicts &&
518 test 5 = "$(git ls-files -u | wc -l)" &&
519 test 3 = "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)" &&
520 test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
522 test_must_fail git diff --quiet &&
523 test_must_fail git diff --cached --quiet &&
525 test -f dir/file-in-the-way &&
526 test -f dir~renamed-file-has-conflicts &&
527 test_cmp expected dir~renamed-file-has-conflicts
530 test_expect_success 'setup both rename source and destination involved in D/F conflict' '
531 git reset --hard &&
532 git checkout --orphan rename-dest &&
533 git rm -rf . &&
534 git clean -fdqx &&
536 mkdir one &&
537 echo stuff >one/file &&
538 git add -A &&
539 git commit -m "Common commmit" &&
541 git mv one/file destdir &&
542 git commit -m "Renamed to destdir" &&
544 git checkout -b source-conflict HEAD~1 &&
545 git rm -rf one &&
546 mkdir destdir &&
547 touch one destdir/foo &&
548 git add -A &&
549 git commit -m "Conflicts in the way"
552 test_expect_failure 'both rename source and destination involved in D/F conflict' '
553 git reset --hard &&
554 rm -rf dir~* &&
555 git checkout -q rename-dest^0 &&
556 test_must_fail git merge --strategy=recursive source-conflict &&
558 test 1 = "$(git ls-files -u | wc -l)" &&
560 test_must_fail git diff --quiet &&
562 test -f destdir/foo &&
563 test -f one &&
564 test -f destdir~HEAD &&
565 test "stuff" = "$(cat destdir~HEAD)"
568 test_expect_success 'setup pair rename to parent of other (D/F conflicts)' '
569 git reset --hard &&
570 git checkout --orphan rename-two &&
571 git rm -rf . &&
572 git clean -fdqx &&
574 mkdir one &&
575 mkdir two &&
576 echo stuff >one/file &&
577 echo other >two/file &&
578 git add -A &&
579 git commit -m "Common commmit" &&
581 git rm -rf one &&
582 git mv two/file one &&
583 git commit -m "Rename two/file -> one" &&
585 git checkout -b rename-one HEAD~1 &&
586 git rm -rf two &&
587 git mv one/file two &&
588 rm -r one &&
589 git commit -m "Rename one/file -> two"
592 test_expect_failure 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
593 git checkout -q rename-one^0 &&
594 mkdir one &&
595 test_must_fail git merge --strategy=recursive rename-two &&
597 test 2 = "$(git ls-files -u | wc -l)" &&
598 test 1 = "$(git ls-files -u one | wc -l)" &&
599 test 1 = "$(git ls-files -u two | wc -l)" &&
601 test_must_fail git diff --quiet &&
603 test 4 = $(find . | grep -v .git | wc -l) &&
605 test -d one &&
606 test -f one~rename-two &&
607 test -f two &&
608 test "other" = $(cat one~rename-two) &&
609 test "stuff" = $(cat two)
612 test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean start' '
613 git reset --hard &&
614 git clean -fdqx &&
615 test_must_fail git merge --strategy=recursive rename-two &&
617 test 2 = "$(git ls-files -u | wc -l)" &&
618 test 1 = "$(git ls-files -u one | wc -l)" &&
619 test 1 = "$(git ls-files -u two | wc -l)" &&
621 test_must_fail git diff --quiet &&
623 test 3 = $(find . | grep -v .git | wc -l) &&
625 test -f one &&
626 test -f two &&
627 test "other" = $(cat one) &&
628 test "stuff" = $(cat two)
631 test_expect_success 'setup rename of one file to two, with directories in the way' '
632 git reset --hard &&
633 git checkout --orphan first-rename &&
634 git rm -rf . &&
635 git clean -fdqx &&
637 echo stuff >original &&
638 git add -A &&
639 git commit -m "Common commmit" &&
641 mkdir two &&
642 >two/file &&
643 git add two/file &&
644 git mv original one &&
645 git commit -m "Put two/file in the way, rename to one" &&
647 git checkout -b second-rename HEAD~1 &&
648 mkdir one &&
649 >one/file &&
650 git add one/file &&
651 git mv original two &&
652 git commit -m "Put one/file in the way, rename to two"
655 test_expect_success 'check handling of differently renamed file with D/F conflicts' '
656 git checkout -q first-rename^0 &&
657 test_must_fail git merge --strategy=recursive second-rename &&
659 test 5 = "$(git ls-files -s | wc -l)" &&
660 test 3 = "$(git ls-files -u | wc -l)" &&
661 test 1 = "$(git ls-files -u one | wc -l)" &&
662 test 1 = "$(git ls-files -u two | wc -l)" &&
663 test 1 = "$(git ls-files -u original | wc -l)" &&
664 test 2 = "$(git ls-files -o | wc -l)" &&
666 test -f one/file &&
667 test -f two/file &&
668 test -f one~HEAD &&
669 test -f two~second-rename &&
670 ! test -f original
673 test_expect_success 'setup rename one file to two; directories moving out of the way' '
674 git reset --hard &&
675 git checkout --orphan first-rename-redo &&
676 git rm -rf . &&
677 git clean -fdqx &&
679 echo stuff >original &&
680 mkdir one two &&
681 touch one/file two/file &&
682 git add -A &&
683 git commit -m "Common commmit" &&
685 git rm -rf one &&
686 git mv original one &&
687 git commit -m "Rename to one" &&
689 git checkout -b second-rename-redo HEAD~1 &&
690 git rm -rf two &&
691 git mv original two &&
692 git commit -m "Rename to two"
695 test_expect_failure 'check handling of differently renamed file with D/F conflicts' '
696 git checkout -q first-rename-redo^0 &&
697 test_must_fail git merge --strategy=recursive second-rename-redo &&
699 test 3 = "$(git ls-files -u | wc -l)" &&
700 test 1 = "$(git ls-files -u one | wc -l)" &&
701 test 1 = "$(git ls-files -u two | wc -l)" &&
702 test 1 = "$(git ls-files -u original | wc -l)" &&
703 test 0 = "$(git ls-files -o | wc -l)" &&
705 test -f one &&
706 test -f two &&
707 ! test -f original
710 test_done