8d7b558bae
Introduce a checkout.defaultRemote setting which can be used to designate a remote to prefer (via checkout.defaultRemote=origin) when running e.g. "git checkout master" to mean origin/master, even though there's other remotes that have the "master" branch. I want this because it's very handy to use this workflow to checkout a repository and create a topic branch, then get back to a "master" as retrieved from upstream: ( cd /tmp && rm -rf tbdiff && git clone git@github.com:trast/tbdiff.git && cd tbdiff && git branch -m topic && git checkout master ) That will output: Branch 'master' set up to track remote branch 'master' from 'origin'. Switched to a new branch 'master' But as soon as a new remote is added (e.g. just to inspect something from someone else) the DWIMery goes away: ( cd /tmp && rm -rf tbdiff && git clone git@github.com:trast/tbdiff.git && cd tbdiff && git branch -m topic && git remote add avar git@github.com:avar/tbdiff.git && git fetch avar && git checkout master ) Will output (without the advice output added earlier in this series): error: pathspec 'master' did not match any file(s) known to git. The new checkout.defaultRemote config allows me to say that whenever that ambiguity comes up I'd like to prefer "origin", and it'll still work as though the only remote I had was "origin". Also adjust the advice.checkoutAmbiguousRemoteBranchName message to mention this new config setting to the user, the full output on my git.git is now (the last paragraph is new): $ ./git --exec-path=$PWD checkout master error: pathspec 'master' did not match any file(s) known to git. hint: 'master' matched more than one remote tracking branch. hint: We found 26 remotes with a reference that matched. So we fell back hint: on trying to resolve the argument as a path, but failed there too! hint: hint: If you meant to check out a remote tracking branch on, e.g. 'origin', hint: you can do so by fully qualifying the name with the --track option: hint: hint: git checkout --track origin/<name> hint: hint: If you'd like to always have checkouts of an ambiguous <name> prefer hint: one remote, e.g. the 'origin' remote, consider setting hint: checkout.defaultRemote=origin in your config. I considered splitting this into checkout.defaultRemote and worktree.defaultRemote, but it's probably less confusing to break our own rules that anything shared between config should live in core.* than have two config settings, and I couldn't come up with a short name under core.* that made sense (core.defaultRemoteForCheckout?). See also70c9ac2f19
("DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"", 2009-10-18) which introduced this DWIM feature to begin with, and4e85333197
("worktree: make add <path> <branch> dwim", 2017-11-26) which added it to git-worktree. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
278 lines
7.2 KiB
Bash
Executable File
278 lines
7.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
test_description='checkout <branch>
|
|
|
|
Ensures that checkout on an unborn branch does what the user expects'
|
|
|
|
. ./test-lib.sh
|
|
|
|
# Is the current branch "refs/heads/$1"?
|
|
test_branch () {
|
|
printf "%s\n" "refs/heads/$1" >expect.HEAD &&
|
|
git symbolic-ref HEAD >actual.HEAD &&
|
|
test_cmp expect.HEAD actual.HEAD
|
|
}
|
|
|
|
# Is branch "refs/heads/$1" set to pull from "$2/$3"?
|
|
test_branch_upstream () {
|
|
printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
|
|
{
|
|
git config "branch.$1.remote" &&
|
|
git config "branch.$1.merge"
|
|
} >actual.upstream &&
|
|
test_cmp expect.upstream actual.upstream
|
|
}
|
|
|
|
status_uno_is_clean () {
|
|
>status.expect &&
|
|
git status -uno --porcelain >status.actual &&
|
|
test_cmp status.expect status.actual
|
|
}
|
|
|
|
test_expect_success 'setup' '
|
|
test_commit my_master &&
|
|
git init repo_a &&
|
|
(
|
|
cd repo_a &&
|
|
test_commit a_master &&
|
|
git checkout -b foo &&
|
|
test_commit a_foo &&
|
|
git checkout -b bar &&
|
|
test_commit a_bar
|
|
) &&
|
|
git init repo_b &&
|
|
(
|
|
cd repo_b &&
|
|
test_commit b_master &&
|
|
git checkout -b foo &&
|
|
test_commit b_foo &&
|
|
git checkout -b baz &&
|
|
test_commit b_baz
|
|
) &&
|
|
git remote add repo_a repo_a &&
|
|
git remote add repo_b repo_b &&
|
|
git config remote.repo_b.fetch \
|
|
"+refs/heads/*:refs/remotes/other_b/*" &&
|
|
git fetch --all
|
|
'
|
|
|
|
test_expect_success 'checkout of non-existing branch fails' '
|
|
git checkout -B master &&
|
|
test_might_fail git branch -D xyzzy &&
|
|
|
|
test_must_fail git checkout xyzzy &&
|
|
status_uno_is_clean &&
|
|
test_must_fail git rev-parse --verify refs/heads/xyzzy &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from multiple remotes fails #1' '
|
|
git checkout -B master &&
|
|
test_might_fail git branch -D foo &&
|
|
|
|
test_must_fail git checkout foo &&
|
|
status_uno_is_clean &&
|
|
test_must_fail git rev-parse --verify refs/heads/foo &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from multiple remotes fails with advice' '
|
|
git checkout -B master &&
|
|
test_might_fail git branch -D foo &&
|
|
test_must_fail git checkout foo 2>stderr &&
|
|
test_branch master &&
|
|
status_uno_is_clean &&
|
|
test_i18ngrep "^hint: " stderr &&
|
|
test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
|
|
checkout foo 2>stderr &&
|
|
test_branch master &&
|
|
status_uno_is_clean &&
|
|
test_i18ngrep ! "^hint: " stderr &&
|
|
# Make sure the likes of checkout -p do not print this hint
|
|
git checkout -p foo 2>stderr &&
|
|
test_i18ngrep ! "^hint: " stderr &&
|
|
status_uno_is_clean
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from multiple remotes succeeds with checkout.defaultRemote #1' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D foo &&
|
|
|
|
git -c checkout.defaultRemote=repo_a checkout foo &&
|
|
status_uno_is_clean &&
|
|
test_branch foo &&
|
|
test_cmp_rev remotes/repo_a/foo HEAD &&
|
|
test_branch_upstream foo repo_a foo
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from a single remote succeeds #1' '
|
|
git checkout -B master &&
|
|
test_might_fail git branch -D bar &&
|
|
|
|
git checkout bar &&
|
|
status_uno_is_clean &&
|
|
test_branch bar &&
|
|
test_cmp_rev remotes/repo_a/bar HEAD &&
|
|
test_branch_upstream bar repo_a bar
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from a single remote succeeds #2' '
|
|
git checkout -B master &&
|
|
test_might_fail git branch -D baz &&
|
|
|
|
git checkout baz &&
|
|
status_uno_is_clean &&
|
|
test_branch baz &&
|
|
test_cmp_rev remotes/other_b/baz HEAD &&
|
|
test_branch_upstream baz repo_b baz
|
|
'
|
|
|
|
test_expect_success '--no-guess suppresses branch auto-vivification' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D bar &&
|
|
|
|
test_must_fail git checkout --no-guess bar &&
|
|
test_must_fail git rev-parse --verify refs/heads/bar &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'setup more remotes with unconventional refspecs' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
git init repo_c &&
|
|
(
|
|
cd repo_c &&
|
|
test_commit c_master &&
|
|
git checkout -b bar &&
|
|
test_commit c_bar &&
|
|
git checkout -b spam &&
|
|
test_commit c_spam
|
|
) &&
|
|
git init repo_d &&
|
|
(
|
|
cd repo_d &&
|
|
test_commit d_master &&
|
|
git checkout -b baz &&
|
|
test_commit d_baz &&
|
|
git checkout -b eggs &&
|
|
test_commit d_eggs
|
|
) &&
|
|
git remote add repo_c repo_c &&
|
|
git config remote.repo_c.fetch \
|
|
"+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
|
|
git remote add repo_d repo_d &&
|
|
git config remote.repo_d.fetch \
|
|
"+refs/heads/*:refs/repo_d/*" &&
|
|
git fetch --all
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from multiple remotes fails #2' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D bar &&
|
|
|
|
test_must_fail git checkout bar &&
|
|
status_uno_is_clean &&
|
|
test_must_fail git rev-parse --verify refs/heads/bar &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from multiple remotes fails #3' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D baz &&
|
|
|
|
test_must_fail git checkout baz &&
|
|
status_uno_is_clean &&
|
|
test_must_fail git rev-parse --verify refs/heads/baz &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from a single remote succeeds #3' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D spam &&
|
|
|
|
git checkout spam &&
|
|
status_uno_is_clean &&
|
|
test_branch spam &&
|
|
test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
|
|
test_branch_upstream spam repo_c spam
|
|
'
|
|
|
|
test_expect_success 'checkout of branch from a single remote succeeds #4' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D eggs &&
|
|
|
|
git checkout eggs &&
|
|
status_uno_is_clean &&
|
|
test_branch eggs &&
|
|
test_cmp_rev refs/repo_d/eggs HEAD &&
|
|
test_branch_upstream eggs repo_d eggs
|
|
'
|
|
|
|
test_expect_success 'checkout of branch with a file having the same name fails' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D spam &&
|
|
|
|
>spam &&
|
|
test_must_fail git checkout spam &&
|
|
status_uno_is_clean &&
|
|
test_must_fail git rev-parse --verify refs/heads/spam &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D spam &&
|
|
|
|
>spam &&
|
|
mkdir sub &&
|
|
mv spam sub/spam &&
|
|
test_must_fail git -C sub checkout spam &&
|
|
status_uno_is_clean &&
|
|
test_must_fail git rev-parse --verify refs/heads/spam &&
|
|
test_branch master
|
|
'
|
|
|
|
test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
|
|
git checkout -B master &&
|
|
status_uno_is_clean &&
|
|
test_might_fail git branch -D spam &&
|
|
|
|
>spam &&
|
|
git checkout spam -- &&
|
|
status_uno_is_clean &&
|
|
test_branch spam &&
|
|
test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
|
|
test_branch_upstream spam repo_c spam
|
|
'
|
|
|
|
test_expect_success 'loosely defined local base branch is reported correctly' '
|
|
|
|
git checkout master &&
|
|
status_uno_is_clean &&
|
|
git branch strict &&
|
|
git branch loose &&
|
|
git commit --allow-empty -m "a bit more" &&
|
|
|
|
test_config branch.strict.remote . &&
|
|
test_config branch.loose.remote . &&
|
|
test_config branch.strict.merge refs/heads/master &&
|
|
test_config branch.loose.merge master &&
|
|
|
|
git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
|
|
status_uno_is_clean &&
|
|
git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
|
|
status_uno_is_clean &&
|
|
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_done
|