3 test_description='git apply --3way'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 print_sanitized_conflicted_diff () {
11 git diff HEAD >diff.raw &&
14 s/^\(+[<>|][<>|][<>|][<>|]*\) .*/\1/
18 test_expect_success setup '
20 test_write_lines 1 2 3 4 5 6 7 >one &&
23 git commit -m initial &&
28 test_write_lines 1 two 3 4 5 six 7 >one &&
29 test_write_lines 1 two 3 4 5 6 7 >two &&
30 git commit -a -m main &&
33 test_write_lines 1 2 3 4 five 6 7 >one &&
34 test_write_lines 1 2 3 4 five 6 7 >two &&
35 git commit -a -m side &&
40 test_expect_success 'apply without --3way' '
41 git diff side^ side >P.diff &&
43 # should fail to apply
45 git checkout main^0 &&
46 test_must_fail git apply --index P.diff &&
47 # should leave things intact
48 git diff-files --exit-code &&
49 git diff-index --exit-code --cached HEAD
52 test_apply_with_3way () {
53 # Merging side should be similar to applying this patch
54 git diff ...side >P.diff &&
56 # The corresponding conflicted merge
58 git checkout main^0 &&
59 test_must_fail git merge --no-commit side &&
60 git ls-files -s >expect.ls &&
61 print_sanitized_conflicted_diff >expect.diff &&
63 # should fail to apply
65 git checkout main^0 &&
66 test_must_fail git apply --index --3way P.diff &&
67 git ls-files -s >actual.ls &&
68 print_sanitized_conflicted_diff >actual.diff &&
70 # The result should resemble the corresponding merge
71 test_cmp expect.ls actual.ls &&
72 test_cmp expect.diff actual.diff
75 test_expect_success 'apply with --3way' '
79 test_expect_success 'apply with --3way with merge.conflictStyle = diff3' '
80 test_config merge.conflictStyle diff3 &&
84 test_expect_success 'apply with --3way with rerere enabled' '
85 test_config rerere.enabled true &&
87 # Merging side should be similar to applying this patch
88 git diff ...side >P.diff &&
90 # The corresponding conflicted merge
92 git checkout main^0 &&
93 test_must_fail git merge --no-commit side &&
95 # Manually resolve and record the resolution
96 test_write_lines 1 two 3 4 five six 7 >one &&
100 # should fail to apply
102 git checkout main^0 &&
103 test_must_fail git apply --index --3way P.diff &&
105 # but rerere should have replayed the recorded resolution
109 test_expect_success 'apply -3 with add/add conflict setup' '
112 git checkout -b adder &&
113 test_write_lines 1 2 3 4 5 6 7 >three &&
114 test_write_lines 1 2 3 4 5 6 7 >four &&
115 git add three four &&
116 git commit -m "add three and four" &&
118 git checkout -b another adder^ &&
119 test_write_lines 1 2 3 4 5 6 7 >three &&
120 test_write_lines 1 2 3 four 5 6 7 >four &&
121 git add three four &&
122 git commit -m "add three and four" &&
124 # Merging another should be similar to applying this patch
125 git diff adder...another >P.diff &&
127 git checkout adder^0 &&
128 test_must_fail git merge --no-commit another &&
129 git ls-files -s >expect.ls &&
130 print_sanitized_conflicted_diff >expect.diff
133 test_expect_success 'apply -3 with add/add conflict' '
134 # should fail to apply ...
136 git checkout adder^0 &&
137 test_must_fail git apply --index --3way P.diff &&
138 # ... and leave conflicts in the index and in the working tree
139 git ls-files -s >actual.ls &&
140 print_sanitized_conflicted_diff >actual.diff &&
142 # The result should resemble the corresponding merge
143 test_cmp expect.ls actual.ls &&
144 test_cmp expect.diff actual.diff
147 test_expect_success 'apply -3 with add/add conflict (dirty working tree)' '
148 # should fail to apply ...
150 git checkout adder^0 &&
152 cat four >four.save &&
153 cat three >three.save &&
154 git ls-files -s >expect.ls &&
155 test_must_fail git apply --index --3way P.diff &&
156 # ... and should not touch anything
157 git ls-files -s >actual.ls &&
158 test_cmp expect.ls actual.ls &&
159 test_cmp four.save four &&
160 test_cmp three.save three