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
163 test_expect_success
'apply -3 with ambiguous repeating file' '
165 test_write_lines 1 2 1 2 1 2 1 2 1 2 1 >one_two_repeat &&
166 git add one_two_repeat &&
167 git commit -m "init one" &&
168 test_write_lines 1 2 1 2 1 2 1 2 one 2 1 >one_two_repeat &&
169 git commit -a -m "change one" &&
171 git diff HEAD~ >Repeat.diff &&
172 git reset --hard HEAD~ &&
174 test_write_lines 1 2 1 2 1 2 one 2 1 2 one >one_two_repeat &&
175 git commit -a -m "change surrounding one" &&
177 git apply --index --3way Repeat.diff &&
178 test_write_lines 1 2 1 2 1 2 one 2 one 2 one >expect &&
180 test_cmp expect one_two_repeat
183 test_expect_success
'apply with --3way --cached clean apply' '
184 # Merging side should be similar to applying this patch
185 git diff ...side >P.diff &&
187 # The corresponding cleanly applied merge
189 git checkout main~ &&
190 git merge --no-commit side &&
191 git ls-files -s >expect.ls &&
195 git checkout main~ &&
196 git apply --cached --3way P.diff &&
197 git ls-files -s >actual.ls &&
198 print_sanitized_conflicted_diff >actual.diff &&
200 # The cache should resemble the corresponding merge
201 # (both files at stage #0)
202 test_cmp expect.ls actual.ls &&
203 # However the working directory should not change
205 test_cmp expect.diff actual.diff
208 test_expect_success
'apply with --3way --cached and conflicts' '
209 # Merging side should be similar to applying this patch
210 git diff ...side >P.diff &&
212 # The corresponding conflicted merge
214 git checkout main^0 &&
215 test_must_fail git merge --no-commit side &&
216 git ls-files -s >expect.ls &&
218 # should fail to apply
220 git checkout main^0 &&
221 test_must_fail git apply --cached --3way P.diff &&
222 git ls-files -s >actual.ls &&
223 print_sanitized_conflicted_diff >actual.diff &&
225 # The cache should resemble the corresponding merge
226 # (one file at stage #0, one file at stages #1 #2 #3)
227 test_cmp expect.ls actual.ls &&
228 # However the working directory should not change
230 test_cmp expect.diff actual.diff
233 test_expect_success
'apply binary file patch' '
234 git reset --hard main &&
235 cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
237 git commit -m "add binary file" &&
239 cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
241 git diff --binary >bin.diff &&
244 # Apply must succeed.
248 test_expect_success
'apply binary file patch with 3way' '
249 git reset --hard main &&
250 cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
252 git commit -m "add binary file" &&
254 cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
256 git diff --binary >bin.diff &&
259 # Apply must succeed.
260 git apply --3way --index bin.diff
263 test_expect_success
'apply full-index patch with 3way' '
264 git reset --hard main &&
265 cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
267 git commit -m "add binary file" &&
269 cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
271 git diff --full-index >bin.diff &&
274 # Apply must succeed.
275 git apply --3way --index bin.diff