1#!/bin/sh23test_description='fetch handles conflicting refspecs correctly'45. ./test-lib.sh67D=$(pwd)89setup_repository () {10git init "$1" && (11cd "$1" &&12git config remote.origin.url "$D" &&13shift &&14for refspec in "$@"15do16git config --add remote.origin.fetch "$refspec"17done18)19}2021test_expect_success 'setup' '22git commit --allow-empty -m "Initial" &&23git branch branch1 &&24git tag tag1 &&25git commit --allow-empty -m "First" &&26git branch branch2 &&27git tag tag228'2930test_expect_success 'fetch with no conflict' '31setup_repository ok "+refs/heads/*:refs/remotes/origin/*" && (32cd ok &&33git fetch origin34)35'3637test_expect_success 'fetch conflict: config vs. config' '38setup_repository ccc \39"+refs/heads/branch1:refs/remotes/origin/branch1" \40"+refs/heads/branch2:refs/remotes/origin/branch1" && (41cd ccc &&42test_must_fail git fetch origin 2>error &&43test_i18ngrep "fatal: Cannot fetch both refs/heads/branch1 and refs/heads/branch2 to refs/remotes/origin/branch1" error44)45'4647test_expect_success 'fetch duplicate: config vs. config' '48setup_repository dcc \49"+refs/heads/*:refs/remotes/origin/*" \50"+refs/heads/branch1:refs/remotes/origin/branch1" && (51cd dcc &&52git fetch origin53)54'5556test_expect_success 'fetch conflict: arg overrides config' '57setup_repository aoc \58"+refs/heads/*:refs/remotes/origin/*" && (59cd aoc &&60git fetch origin refs/heads/branch2:refs/remotes/origin/branch161)62'6364test_expect_success 'fetch conflict: arg vs. arg' '65setup_repository caa && (66cd caa &&67test_must_fail git fetch origin \68refs/heads/*:refs/remotes/origin/* \69refs/heads/branch2:refs/remotes/origin/branch1 2>error &&70test_i18ngrep "fatal: Cannot fetch both refs/heads/branch1 and refs/heads/branch2 to refs/remotes/origin/branch1" error71)72'7374test_expect_success 'fetch conflict: criss-cross args' '75setup_repository xaa \76"+refs/heads/*:refs/remotes/origin/*" && (77cd xaa &&78git fetch origin \79refs/heads/branch1:refs/remotes/origin/branch2 \80refs/heads/branch2:refs/remotes/origin/branch1 2>error &&81test_i18ngrep "warning: refs/remotes/origin/branch1 usually tracks refs/heads/branch1, not refs/heads/branch2" error &&82test_i18ngrep "warning: refs/remotes/origin/branch2 usually tracks refs/heads/branch2, not refs/heads/branch1" error83)84'8586test_done