Git 2.34-rc0
[alt-git.git] / t / t7601-merge-pull-config.sh
blob1f652f433ee276b1420324f6609ae51c5b02863c
1 #!/bin/sh
3 test_description='git merge
5 Testing pull.* configuration parsing.'
7 . ./test-lib.sh
9 test_expect_success 'setup' '
10 echo c0 >c0.c &&
11 git add c0.c &&
12 git commit -m c0 &&
13 git tag c0 &&
14 echo c1 >c1.c &&
15 git add c1.c &&
16 git commit -m c1 &&
17 git tag c1 &&
18 git reset --hard c0 &&
19 echo c2 >c2.c &&
20 git add c2.c &&
21 git commit -m c2 &&
22 git tag c2 &&
23 git reset --hard c0 &&
24 echo c3 >c3.c &&
25 git add c3.c &&
26 git commit -m c3 &&
27 git tag c3
30 test_expect_success 'pull.rebase not set, ff possible' '
31 git reset --hard c0 &&
32 git pull . c1 2>err &&
33 test_i18ngrep ! "You have divergent branches" err
36 test_expect_success 'pull.rebase not set and pull.ff=true' '
37 git reset --hard c0 &&
38 test_config pull.ff true &&
39 git pull . c1 2>err &&
40 test_i18ngrep ! "You have divergent branches" err
43 test_expect_success 'pull.rebase not set and pull.ff=false' '
44 git reset --hard c0 &&
45 test_config pull.ff false &&
46 git pull . c1 2>err &&
47 test_i18ngrep ! "You have divergent branches" err
50 test_expect_success 'pull.rebase not set and pull.ff=only' '
51 git reset --hard c0 &&
52 test_config pull.ff only &&
53 git pull . c1 2>err &&
54 test_i18ngrep ! "You have divergent branches" err
57 test_expect_success 'pull.rebase not set and --rebase given' '
58 git reset --hard c0 &&
59 git pull --rebase . c1 2>err &&
60 test_i18ngrep ! "You have divergent branches" err
63 test_expect_success 'pull.rebase not set and --no-rebase given' '
64 git reset --hard c0 &&
65 git pull --no-rebase . c1 2>err &&
66 test_i18ngrep ! "You have divergent branches" err
69 test_expect_success 'pull.rebase not set and --ff given' '
70 git reset --hard c0 &&
71 git pull --ff . c1 2>err &&
72 test_i18ngrep ! "You have divergent branches" err
75 test_expect_success 'pull.rebase not set and --no-ff given' '
76 git reset --hard c0 &&
77 git pull --no-ff . c1 2>err &&
78 test_i18ngrep ! "You have divergent branches" err
81 test_expect_success 'pull.rebase not set and --ff-only given' '
82 git reset --hard c0 &&
83 git pull --ff-only . c1 2>err &&
84 test_i18ngrep ! "You have divergent branches" err
87 test_expect_success 'pull.rebase not set (not-fast-forward)' '
88 git reset --hard c2 &&
89 test_must_fail git -c color.advice=always pull . c1 2>err &&
90 test_decode_color <err >decoded &&
91 test_i18ngrep "<YELLOW>hint: " decoded &&
92 test_i18ngrep "You have divergent branches" decoded
95 test_expect_success 'pull.rebase not set and pull.ff=true (not-fast-forward)' '
96 git reset --hard c2 &&
97 test_config pull.ff true &&
98 git pull . c1 2>err &&
99 test_i18ngrep ! "You have divergent branches" err
102 test_expect_success 'pull.rebase not set and pull.ff=false (not-fast-forward)' '
103 git reset --hard c2 &&
104 test_config pull.ff false &&
105 git pull . c1 2>err &&
106 test_i18ngrep ! "You have divergent branches" err
109 test_expect_success 'pull.rebase not set and pull.ff=only (not-fast-forward)' '
110 git reset --hard c2 &&
111 test_config pull.ff only &&
112 test_must_fail git pull . c1 2>err &&
113 test_i18ngrep ! "You have divergent branches" err
116 test_expect_success 'pull.rebase not set and --rebase given (not-fast-forward)' '
117 git reset --hard c2 &&
118 git pull --rebase . c1 2>err &&
119 test_i18ngrep ! "You have divergent branches" err
122 test_expect_success 'pull.rebase not set and --no-rebase given (not-fast-forward)' '
123 git reset --hard c2 &&
124 git pull --no-rebase . c1 2>err &&
125 test_i18ngrep ! "You have divergent branches" err
128 test_expect_success 'pull.rebase not set and --ff given (not-fast-forward)' '
129 git reset --hard c2 &&
130 git pull --ff . c1 2>err &&
131 test_i18ngrep ! "You have divergent branches" err
134 test_expect_success 'pull.rebase not set and --no-ff given (not-fast-forward)' '
135 git reset --hard c2 &&
136 git pull --no-ff . c1 2>err &&
137 test_i18ngrep ! "You have divergent branches" err
140 test_expect_success 'pull.rebase not set and --ff-only given (not-fast-forward)' '
141 git reset --hard c2 &&
142 test_must_fail git pull --ff-only . c1 2>err &&
143 test_i18ngrep ! "You have divergent branches" err
146 test_does_rebase () {
147 git reset --hard c2 &&
148 git "$@" . c1 &&
149 # Check that we actually did a rebase
150 git rev-list --count HEAD >actual &&
151 git rev-list --merges --count HEAD >>actual &&
152 test_write_lines 3 0 >expect &&
153 test_cmp expect actual &&
154 rm actual expect
157 # Prefers merge over fast-forward
158 test_does_merge_when_ff_possible () {
159 git reset --hard c0 &&
160 git "$@" . c1 &&
161 # Check that we actually did a merge
162 git rev-list --count HEAD >actual &&
163 git rev-list --merges --count HEAD >>actual &&
164 test_write_lines 3 1 >expect &&
165 test_cmp expect actual &&
166 rm actual expect
169 # Prefers fast-forward over merge or rebase
170 test_does_fast_forward () {
171 git reset --hard c0 &&
172 git "$@" . c1 &&
174 # Check that we did not get any merges
175 git rev-list --count HEAD >actual &&
176 git rev-list --merges --count HEAD >>actual &&
177 test_write_lines 2 0 >expect &&
178 test_cmp expect actual &&
180 # Check that we ended up at c1
181 git rev-parse HEAD >actual &&
182 git rev-parse c1^{commit} >expect &&
183 test_cmp actual expect &&
185 # Remove temporary files
186 rm actual expect
189 # Doesn't fail when fast-forward not possible; does a merge
190 test_falls_back_to_full_merge () {
191 git reset --hard c2 &&
192 git "$@" . c1 &&
193 # Check that we actually did a merge
194 git rev-list --count HEAD >actual &&
195 git rev-list --merges --count HEAD >>actual &&
196 test_write_lines 4 1 >expect &&
197 test_cmp expect actual &&
198 rm actual expect
201 # Attempts fast forward, which is impossible, and bails
202 test_attempts_fast_forward () {
203 git reset --hard c2 &&
204 test_must_fail git "$@" . c1 2>err &&
205 test_i18ngrep "Not possible to fast-forward, aborting" err
209 # Group 1: Interaction of --ff-only with --[no-]rebase
210 # (And related interaction of pull.ff=only with pull.rebase)
212 test_expect_success '--ff-only overrides --rebase' '
213 test_attempts_fast_forward pull --rebase --ff-only
216 test_expect_success '--ff-only overrides --rebase even if first' '
217 test_attempts_fast_forward pull --ff-only --rebase
220 test_expect_success '--ff-only overrides --no-rebase' '
221 test_attempts_fast_forward pull --ff-only --no-rebase
224 test_expect_success 'pull.ff=only overrides pull.rebase=true' '
225 test_attempts_fast_forward -c pull.ff=only -c pull.rebase=true pull
228 test_expect_success 'pull.ff=only overrides pull.rebase=false' '
229 test_attempts_fast_forward -c pull.ff=only -c pull.rebase=false pull
232 # Group 2: --rebase=[!false] overrides --no-ff and --ff
233 # (And related interaction of pull.rebase=!false and pull.ff=!only)
234 test_expect_success '--rebase overrides --no-ff' '
235 test_does_rebase pull --rebase --no-ff
238 test_expect_success '--rebase overrides --ff' '
239 test_does_rebase pull --rebase --ff
242 test_expect_success '--rebase fast-forwards when possible' '
243 test_does_fast_forward pull --rebase --ff
246 test_expect_success 'pull.rebase=true overrides pull.ff=false' '
247 test_does_rebase -c pull.rebase=true -c pull.ff=false pull
250 test_expect_success 'pull.rebase=true overrides pull.ff=true' '
251 test_does_rebase -c pull.rebase=true -c pull.ff=true pull
254 # Group 3: command line flags take precedence over config
255 test_expect_success '--ff-only takes precedence over pull.rebase=true' '
256 test_attempts_fast_forward -c pull.rebase=true pull --ff-only
259 test_expect_success '--ff-only takes precedence over pull.rebase=false' '
260 test_attempts_fast_forward -c pull.rebase=false pull --ff-only
263 test_expect_success '--no-rebase takes precedence over pull.ff=only' '
264 test_falls_back_to_full_merge -c pull.ff=only pull --no-rebase
267 test_expect_success '--rebase takes precedence over pull.ff=only' '
268 test_does_rebase -c pull.ff=only pull --rebase
271 test_expect_success '--rebase overrides pull.ff=true' '
272 test_does_rebase -c pull.ff=true pull --rebase
275 test_expect_success '--rebase overrides pull.ff=false' '
276 test_does_rebase -c pull.ff=false pull --rebase
279 test_expect_success '--rebase overrides pull.ff unset' '
280 test_does_rebase pull --rebase
283 # Group 4: --no-rebase heeds pull.ff=!only or explict --ff or --no-ff
285 test_expect_success '--no-rebase works with --no-ff' '
286 test_does_merge_when_ff_possible pull --no-rebase --no-ff
289 test_expect_success '--no-rebase works with --ff' '
290 test_does_fast_forward pull --no-rebase --ff
293 test_expect_success '--no-rebase does ff if pull.ff unset' '
294 test_does_fast_forward pull --no-rebase
297 test_expect_success '--no-rebase heeds pull.ff=true' '
298 test_does_fast_forward -c pull.ff=true pull --no-rebase
301 test_expect_success '--no-rebase heeds pull.ff=false' '
302 test_does_merge_when_ff_possible -c pull.ff=false pull --no-rebase
305 # Group 5: pull.rebase=!false in combination with --no-ff or --ff
306 test_expect_success 'pull.rebase=true and --no-ff' '
307 test_does_rebase -c pull.rebase=true pull --no-ff
310 test_expect_success 'pull.rebase=true and --ff' '
311 test_does_rebase -c pull.rebase=true pull --ff
314 test_expect_success 'pull.rebase=false and --no-ff' '
315 test_does_merge_when_ff_possible -c pull.rebase=false pull --no-ff
318 test_expect_success 'pull.rebase=false and --ff, ff possible' '
319 test_does_fast_forward -c pull.rebase=false pull --ff
322 test_expect_success 'pull.rebase=false and --ff, ff not possible' '
323 test_falls_back_to_full_merge -c pull.rebase=false pull --ff
326 # End of groupings for conflicting merge vs. rebase flags/options
328 test_expect_success 'Multiple heads warns about inability to fast forward' '
329 git reset --hard c1 &&
330 test_must_fail git pull . c2 c3 2>err &&
331 test_i18ngrep "You have divergent branches" err
334 test_expect_success 'Multiple can never be fast forwarded' '
335 git reset --hard c0 &&
336 test_must_fail git -c pull.ff=only pull . c1 c2 c3 2>err &&
337 test_i18ngrep ! "You have divergent branches" err &&
338 # In addition to calling out "cannot fast-forward", we very much
339 # want the "multiple branches" piece to be called out to users.
340 test_i18ngrep "Cannot fast-forward to multiple branches" err
343 test_expect_success 'Cannot rebase with multiple heads' '
344 git reset --hard c0 &&
345 test_must_fail git -c pull.rebase=true pull . c1 c2 c3 2>err &&
346 test_i18ngrep ! "You have divergent branches" err &&
347 test_i18ngrep "Cannot rebase onto multiple branches." err
350 test_expect_success 'merge c1 with c2' '
351 git reset --hard c1 &&
352 test -f c0.c &&
353 test -f c1.c &&
354 test ! -f c2.c &&
355 test ! -f c3.c &&
356 git merge c2 &&
357 test -f c1.c &&
358 test -f c2.c
361 test_expect_success 'fast-forward pull succeeds with "true" in pull.ff' '
362 git reset --hard c0 &&
363 test_config pull.ff true &&
364 git pull . c1 &&
365 test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
368 test_expect_success 'pull.ff=true overrides merge.ff=false' '
369 git reset --hard c0 &&
370 test_config merge.ff false &&
371 test_config pull.ff true &&
372 git pull . c1 &&
373 test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
376 test_expect_success 'fast-forward pull creates merge with "false" in pull.ff' '
377 git reset --hard c0 &&
378 test_config pull.ff false &&
379 git pull . c1 &&
380 test "$(git rev-parse HEAD^1)" = "$(git rev-parse c0)" &&
381 test "$(git rev-parse HEAD^2)" = "$(git rev-parse c1)"
384 test_expect_success 'pull prevents non-fast-forward with "only" in pull.ff' '
385 git reset --hard c1 &&
386 test_config pull.ff only &&
387 test_must_fail git pull . c3
390 test_expect_success 'merge c1 with c2 (ours in pull.twohead)' '
391 git reset --hard c1 &&
392 git config pull.twohead ours &&
393 git merge c2 &&
394 test -f c1.c &&
395 ! test -f c2.c
398 test_expect_success 'merge c1 with c2 and c3 (recursive in pull.octopus)' '
399 git reset --hard c1 &&
400 git config pull.octopus "recursive" &&
401 test_must_fail git merge c2 c3 &&
402 test "$(git rev-parse c1)" = "$(git rev-parse HEAD)"
405 test_expect_success 'merge c1 with c2 and c3 (recursive and octopus in pull.octopus)' '
406 git reset --hard c1 &&
407 git config pull.octopus "recursive octopus" &&
408 git merge c2 c3 &&
409 test "$(git rev-parse c1)" != "$(git rev-parse HEAD)" &&
410 test "$(git rev-parse c1)" = "$(git rev-parse HEAD^1)" &&
411 test "$(git rev-parse c2)" = "$(git rev-parse HEAD^2)" &&
412 test "$(git rev-parse c3)" = "$(git rev-parse HEAD^3)" &&
413 git diff --exit-code &&
414 test -f c0.c &&
415 test -f c1.c &&
416 test -f c2.c &&
417 test -f c3.c
420 conflict_count()
423 git diff-files --name-only
424 git ls-files --unmerged
425 } | wc -l
428 # c4 - c5
429 # \ c6
431 # There are two conflicts here:
433 # 1) Because foo.c is renamed to bar.c, recursive will handle this,
434 # resolve won't.
436 # 2) One in conflict.c and that will always fail.
438 test_expect_success 'setup conflicted merge' '
439 git reset --hard c0 &&
440 echo A >conflict.c &&
441 git add conflict.c &&
442 echo contents >foo.c &&
443 git add foo.c &&
444 git commit -m c4 &&
445 git tag c4 &&
446 echo B >conflict.c &&
447 git add conflict.c &&
448 git mv foo.c bar.c &&
449 git commit -m c5 &&
450 git tag c5 &&
451 git reset --hard c4 &&
452 echo C >conflict.c &&
453 git add conflict.c &&
454 echo secondline >> foo.c &&
455 git add foo.c &&
456 git commit -m c6 &&
457 git tag c6
460 # First do the merge with resolve and recursive then verify that
461 # recursive is chosen.
463 test_expect_success 'merge picks up the best result' '
464 git config --unset-all pull.twohead &&
465 git reset --hard c5 &&
466 test_must_fail git merge -s resolve c6 &&
467 resolve_count=$(conflict_count) &&
468 git reset --hard c5 &&
469 test_must_fail git merge -s recursive c6 &&
470 recursive_count=$(conflict_count) &&
471 git reset --hard c5 &&
472 test_must_fail git merge -s recursive -s resolve c6 &&
473 auto_count=$(conflict_count) &&
474 test $auto_count = $recursive_count &&
475 test $auto_count != $resolve_count
478 test_expect_success 'merge picks up the best result (from config)' '
479 git config pull.twohead "recursive resolve" &&
480 git reset --hard c5 &&
481 test_must_fail git merge -s resolve c6 &&
482 resolve_count=$(conflict_count) &&
483 git reset --hard c5 &&
484 test_must_fail git merge -s recursive c6 &&
485 recursive_count=$(conflict_count) &&
486 git reset --hard c5 &&
487 test_must_fail git merge c6 &&
488 auto_count=$(conflict_count) &&
489 test $auto_count = $recursive_count &&
490 test $auto_count != $resolve_count
493 test_expect_success 'merge errors out on invalid strategy' '
494 git config pull.twohead "foobar" &&
495 git reset --hard c5 &&
496 test_must_fail git merge c6
499 test_expect_success 'merge errors out on invalid strategy' '
500 git config --unset-all pull.twohead &&
501 git reset --hard c5 &&
502 test_must_fail git merge -s "resolve recursive" c6
505 test_done