]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4108-apply-threeway.sh
The third batch
[thirdparty/git.git] / t / t4108-apply-threeway.sh
CommitLineData
4f4a6cb9
JH
1#!/bin/sh
2
3test_description='git apply --3way'
4
8f37854b 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
4f4a6cb9
JH
8. ./test-lib.sh
9
b0069684
DL
10print_sanitized_conflicted_diff () {
11 git diff HEAD >diff.raw &&
4f4a6cb9
JH
12 sed -e '
13 /^index /d
aa76ae49 14 s/^\(+[<>|][<>|][<>|][<>|]*\) .*/\1/
b0069684 15 ' diff.raw
4f4a6cb9
JH
16}
17
18test_expect_success setup '
19 test_tick &&
fa87b813 20 test_write_lines 1 2 3 4 5 6 7 >one &&
4f4a6cb9
JH
21 cat one >two &&
22 git add one two &&
23 git commit -m initial &&
24
25 git branch side &&
26
27 test_tick &&
fa87b813
DL
28 test_write_lines 1 two 3 4 5 six 7 >one &&
29 test_write_lines 1 two 3 4 5 6 7 >two &&
8f37854b 30 git commit -a -m main &&
4f4a6cb9
JH
31
32 git checkout side &&
fa87b813
DL
33 test_write_lines 1 2 3 4 five 6 7 >one &&
34 test_write_lines 1 2 3 4 five 6 7 >two &&
4f4a6cb9
JH
35 git commit -a -m side &&
36
8f37854b 37 git checkout main
4f4a6cb9
JH
38'
39
40test_expect_success 'apply without --3way' '
41 git diff side^ side >P.diff &&
42
43 # should fail to apply
44 git reset --hard &&
8f37854b 45 git checkout main^0 &&
4f4a6cb9
JH
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
50'
51
aa76ae49 52test_apply_with_3way () {
4f4a6cb9
JH
53 # Merging side should be similar to applying this patch
54 git diff ...side >P.diff &&
55
56 # The corresponding conflicted merge
57 git reset --hard &&
8f37854b 58 git checkout main^0 &&
4f4a6cb9
JH
59 test_must_fail git merge --no-commit side &&
60 git ls-files -s >expect.ls &&
b0069684 61 print_sanitized_conflicted_diff >expect.diff &&
4f4a6cb9
JH
62
63 # should fail to apply
64 git reset --hard &&
8f37854b 65 git checkout main^0 &&
4f4a6cb9
JH
66 test_must_fail git apply --index --3way P.diff &&
67 git ls-files -s >actual.ls &&
b0069684 68 print_sanitized_conflicted_diff >actual.diff &&
4f4a6cb9
JH
69
70 # The result should resemble the corresponding merge
71 test_cmp expect.ls actual.ls &&
72 test_cmp expect.diff actual.diff
aa76ae49
DL
73}
74
75test_expect_success 'apply with --3way' '
76 test_apply_with_3way
77'
78
091489d0 79test_expect_success 'apply with --3way with merge.conflictStyle = diff3' '
aa76ae49
DL
80 test_config merge.conflictStyle diff3 &&
81 test_apply_with_3way
4f4a6cb9
JH
82'
83
f2633ebd 84test_expect_success 'apply with --3way with rerere enabled' '
95806205 85 test_config rerere.enabled true &&
f2633ebd
JH
86
87 # Merging side should be similar to applying this patch
88 git diff ...side >P.diff &&
89
90 # The corresponding conflicted merge
91 git reset --hard &&
8f37854b 92 git checkout main^0 &&
f2633ebd
JH
93 test_must_fail git merge --no-commit side &&
94
95 # Manually resolve and record the resolution
fa87b813 96 test_write_lines 1 two 3 4 five six 7 >one &&
f2633ebd
JH
97 git rerere &&
98 cat one >expect &&
99
100 # should fail to apply
101 git reset --hard &&
8f37854b 102 git checkout main^0 &&
f2633ebd
JH
103 test_must_fail git apply --index --3way P.diff &&
104
105 # but rerere should have replayed the recorded resolution
106 test_cmp expect one
107'
108
fdac5089
JH
109test_expect_success 'apply -3 with add/add conflict setup' '
110 git reset --hard &&
111
112 git checkout -b adder &&
fa87b813
DL
113 test_write_lines 1 2 3 4 5 6 7 >three &&
114 test_write_lines 1 2 3 4 5 6 7 >four &&
fdac5089
JH
115 git add three four &&
116 git commit -m "add three and four" &&
117
118 git checkout -b another adder^ &&
fa87b813
DL
119 test_write_lines 1 2 3 4 5 6 7 >three &&
120 test_write_lines 1 2 3 four 5 6 7 >four &&
fdac5089
JH
121 git add three four &&
122 git commit -m "add three and four" &&
123
124 # Merging another should be similar to applying this patch
125 git diff adder...another >P.diff &&
126
127 git checkout adder^0 &&
128 test_must_fail git merge --no-commit another &&
129 git ls-files -s >expect.ls &&
b0069684 130 print_sanitized_conflicted_diff >expect.diff
fdac5089
JH
131'
132
133test_expect_success 'apply -3 with add/add conflict' '
134 # should fail to apply ...
135 git reset --hard &&
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 &&
b0069684 140 print_sanitized_conflicted_diff >actual.diff &&
fdac5089
JH
141
142 # The result should resemble the corresponding merge
143 test_cmp expect.ls actual.ls &&
144 test_cmp expect.diff actual.diff
145'
146
147test_expect_success 'apply -3 with add/add conflict (dirty working tree)' '
148 # should fail to apply ...
149 git reset --hard &&
150 git checkout adder^0 &&
151 echo >>four &&
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
161'
162
923cd87a
JZ
163test_expect_success 'apply -3 with ambiguous repeating file' '
164 git reset --hard &&
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" &&
170
171 git diff HEAD~ >Repeat.diff &&
172 git reset --hard HEAD~ &&
173
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" &&
176
177 git apply --index --3way Repeat.diff &&
178 test_write_lines 1 2 1 2 1 2 one 2 one 2 one >expect &&
179
180 test_cmp expect one_two_repeat
181'
182
c0c2a37a
JZ
183test_expect_success 'apply with --3way --cached clean apply' '
184 # Merging side should be similar to applying this patch
185 git diff ...side >P.diff &&
186
187 # The corresponding cleanly applied merge
188 git reset --hard &&
189 git checkout main~ &&
190 git merge --no-commit side &&
191 git ls-files -s >expect.ls &&
192
193 # should succeed
194 git reset --hard &&
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 &&
199
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
204 >expect.diff &&
205 test_cmp expect.diff actual.diff
206'
207
208test_expect_success 'apply with --3way --cached and conflicts' '
209 # Merging side should be similar to applying this patch
210 git diff ...side >P.diff &&
211
212 # The corresponding conflicted merge
213 git reset --hard &&
214 git checkout main^0 &&
215 test_must_fail git merge --no-commit side &&
216 git ls-files -s >expect.ls &&
217
218 # should fail to apply
219 git reset --hard &&
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 &&
224
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
229 >expect.diff &&
230 test_cmp expect.diff actual.diff
231'
232
57f183b6
JH
233test_expect_success 'apply binary file patch' '
234 git reset --hard main &&
235 cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
236 git add bin.png &&
237 git commit -m "add binary file" &&
238
239 cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
240
241 git diff --binary >bin.diff &&
242 git reset --hard &&
243
244 # Apply must succeed.
245 git apply bin.diff
246'
247
248test_expect_success 'apply binary file patch with 3way' '
249 git reset --hard main &&
250 cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
251 git add bin.png &&
252 git commit -m "add binary file" &&
253
254 cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
255
256 git diff --binary >bin.diff &&
257 git reset --hard &&
258
259 # Apply must succeed.
260 git apply --3way --index bin.diff
261'
262
263test_expect_success 'apply full-index patch with 3way' '
264 git reset --hard main &&
265 cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
266 git add bin.png &&
267 git commit -m "add binary file" &&
268
269 cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
270
271 git diff --full-index >bin.diff &&
272 git reset --hard &&
273
274 # Apply must succeed.
275 git apply --3way --index bin.diff
276'
277
34d60703
JZ
278test_expect_success 'apply delete then new patch with 3way' '
279 git reset --hard main &&
280 test_write_lines 2 > delnew &&
281 git add delnew &&
282 git diff --cached >> new.patch &&
283 git reset --hard &&
284 test_write_lines 1 > delnew &&
285 git add delnew &&
286 git commit -m "delnew" &&
287 rm delnew &&
288 git diff >> delete-then-new.patch &&
289 cat new.patch >> delete-then-new.patch &&
290
291 git checkout -- . &&
292 # Apply must succeed.
293 git apply --3way delete-then-new.patch
294'
295
4f4a6cb9 296test_done