]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4108-apply-threeway.sh
The third batch
[thirdparty/git.git] / t / t4108-apply-threeway.sh
1 #!/bin/sh
2
3 test_description='git apply --3way'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 print_sanitized_conflicted_diff () {
11 git diff HEAD >diff.raw &&
12 sed -e '
13 /^index /d
14 s/^\(+[<>|][<>|][<>|][<>|]*\) .*/\1/
15 ' diff.raw
16 }
17
18 test_expect_success setup '
19 test_tick &&
20 test_write_lines 1 2 3 4 5 6 7 >one &&
21 cat one >two &&
22 git add one two &&
23 git commit -m initial &&
24
25 git branch side &&
26
27 test_tick &&
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 &&
31
32 git checkout side &&
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 &&
36
37 git checkout main
38 '
39
40 test_expect_success 'apply without --3way' '
41 git diff side^ side >P.diff &&
42
43 # should fail to apply
44 git reset --hard &&
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
50 '
51
52 test_apply_with_3way () {
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 &&
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 &&
62
63 # should fail to apply
64 git reset --hard &&
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 &&
69
70 # The result should resemble the corresponding merge
71 test_cmp expect.ls actual.ls &&
72 test_cmp expect.diff actual.diff
73 }
74
75 test_expect_success 'apply with --3way' '
76 test_apply_with_3way
77 '
78
79 test_expect_success 'apply with --3way with merge.conflictStyle = diff3' '
80 test_config merge.conflictStyle diff3 &&
81 test_apply_with_3way
82 '
83
84 test_expect_success 'apply with --3way with rerere enabled' '
85 test_config rerere.enabled true &&
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 &&
92 git checkout main^0 &&
93 test_must_fail git merge --no-commit side &&
94
95 # Manually resolve and record the resolution
96 test_write_lines 1 two 3 4 five six 7 >one &&
97 git rerere &&
98 cat one >expect &&
99
100 # should fail to apply
101 git reset --hard &&
102 git checkout main^0 &&
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
109 test_expect_success 'apply -3 with add/add conflict setup' '
110 git reset --hard &&
111
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" &&
117
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" &&
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 &&
130 print_sanitized_conflicted_diff >expect.diff
131 '
132
133 test_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 &&
140 print_sanitized_conflicted_diff >actual.diff &&
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
147 test_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
163 test_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
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 &&
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
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 &&
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
233 test_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
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 &&
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
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 &&
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
278 test_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
296 test_done