Browse Source

Merge branch 'dl/checkout-guess'

"git checkout" learned to use checkout.guess configuration variable
and enable/disable its "--[no-]guess" option accordingly.

* dl/checkout-guess:
  checkout: learn to respect checkout.guess
  Documentation/config/checkout: replace sq with backticks
maint
Junio C Hamano 4 years ago
parent
commit
0e41cfad62
  1. 21
      Documentation/config/checkout.txt
  2. 5
      Documentation/git-checkout.txt
  3. 3
      Documentation/git-switch.txt
  4. 7
      builtin/checkout.c
  5. 25
      contrib/completion/git-completion.bash
  6. 11
      t/t2024-checkout-dwim.sh
  7. 5
      t/t2060-switch.sh
  8. 52
      t/t9902-completion.sh

21
Documentation/config/checkout.txt

@ -1,18 +1,23 @@
checkout.defaultRemote:: checkout.defaultRemote::
When you run 'git checkout <something>' When you run `git checkout <something>`
or 'git switch <something>' and only have one or `git switch <something>` and only have one
remote, it may implicitly fall back on checking out and remote, it may implicitly fall back on checking out and
tracking e.g. 'origin/<something>'. This stops working as soon tracking e.g. `origin/<something>`. This stops working as soon
as you have more than one remote with a '<something>' as you have more than one remote with a `<something>`
reference. This setting allows for setting the name of a reference. This setting allows for setting the name of a
preferred remote that should always win when it comes to preferred remote that should always win when it comes to
disambiguation. The typical use-case is to set this to disambiguation. The typical use-case is to set this to
`origin`. `origin`.
+ +
Currently this is used by linkgit:git-switch[1] and Currently this is used by linkgit:git-switch[1] and
linkgit:git-checkout[1] when 'git checkout <something>' linkgit:git-checkout[1] when `git checkout <something>`
or 'git switch <something>' or `git switch <something>`
will checkout the '<something>' branch on another remote, will checkout the `<something>` branch on another remote,
and by linkgit:git-worktree[1] when 'git worktree add' refers to a and by linkgit:git-worktree[1] when `git worktree add` refers to a
remote branch. This setting might be used for other checkout-like remote branch. This setting might be used for other checkout-like
commands or functionality in the future. commands or functionality in the future.

checkout.guess::
Provides the default value for the `--guess` or `--no-guess`
option in `git checkout` and `git switch`. See
linkgit:git-switch[1] and linkgit:git-checkout[1].

5
Documentation/git-checkout.txt

@ -192,7 +192,10 @@ branches from there if `<branch>` is ambiguous but exists on the
'origin' remote. See also `checkout.defaultRemote` in 'origin' remote. See also `checkout.defaultRemote` in
linkgit:git-config[1]. linkgit:git-config[1].
+ +
Use `--no-guess` to disable this. `--guess` is the default behavior. Use `--no-guess` to disable it.
+
The default behavior can be set via the `checkout.guess` configuration
variable.


-l:: -l::
Create the new branch's reflog; see linkgit:git-branch[1] for Create the new branch's reflog; see linkgit:git-branch[1] for

3
Documentation/git-switch.txt

@ -103,6 +103,9 @@ ambiguous but exists on the 'origin' remote. See also
`checkout.defaultRemote` in linkgit:git-config[1]. `checkout.defaultRemote` in linkgit:git-config[1].
+ +
`--guess` is the default behavior. Use `--no-guess` to disable it. `--guess` is the default behavior. Use `--no-guess` to disable it.
+
The default behavior can be set via the `checkout.guess` configuration
variable.


-f:: -f::
--force:: --force::

7
builtin/checkout.c

@ -1106,11 +1106,16 @@ static int switch_branches(const struct checkout_opts *opts,


static int git_checkout_config(const char *var, const char *value, void *cb) static int git_checkout_config(const char *var, const char *value, void *cb)
{ {
struct checkout_opts *opts = cb;

if (!strcmp(var, "diff.ignoresubmodules")) { if (!strcmp(var, "diff.ignoresubmodules")) {
struct checkout_opts *opts = cb;
handle_ignore_submodules_arg(&opts->diff_options, value); handle_ignore_submodules_arg(&opts->diff_options, value);
return 0; return 0;
} }
if (!strcmp(var, "checkout.guess")) {
opts->dwim_new_local_branch = git_config_bool(var, value);
return 0;
}


if (starts_with(var, "submodule.")) if (starts_with(var, "submodule."))
return git_default_submodule_config(var, value, NULL); return git_default_submodule_config(var, value, NULL);

25
contrib/completion/git-completion.bash

@ -1467,14 +1467,15 @@ _git_bundle ()
# Helper function to decide whether or not we should enable DWIM logic for # Helper function to decide whether or not we should enable DWIM logic for
# git-switch and git-checkout. # git-switch and git-checkout.
# #
# To decide between the following rules in priority order # To decide between the following rules in decreasing priority order:
# 1) the last provided of "--guess" or "--no-guess" explicitly enable or # - the last provided of "--guess" or "--no-guess" explicitly enable or
# disable completion of DWIM logic respectively. # disable completion of DWIM logic respectively.
# 2) If the --no-track option is provided, take this as a hint to disable the # - If checkout.guess is false, disable completion of DWIM logic.
# DWIM completion logic # - If the --no-track option is provided, take this as a hint to disable the
# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion # DWIM completion logic
# logic, as requested by the user. # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
# 4) Enable DWIM logic otherwise. # logic, as requested by the user.
# - Enable DWIM logic otherwise.
# #
__git_checkout_default_dwim_mode () __git_checkout_default_dwim_mode ()
{ {
@ -1485,11 +1486,17 @@ __git_checkout_default_dwim_mode ()
fi fi


# --no-track disables DWIM, but with lower priority than # --no-track disables DWIM, but with lower priority than
# --guess/--no-guess # --guess/--no-guess/checkout.guess
if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
dwim_opt="" dwim_opt=""
fi fi


# checkout.guess = false disables DWIM, but with lower priority than
# --guess/--no-guess
if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
dwim_opt=""
fi

# Find the last provided --guess or --no-guess # Find the last provided --guess or --no-guess
last_option="$(__git_find_last_on_cmdline "--guess --no-guess")" last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
case "$last_option" in case "$last_option" in

11
t/t2024-checkout-dwim.sh

@ -166,6 +166,17 @@ test_expect_success '--no-guess suppresses branch auto-vivification' '
test_branch master test_branch master
' '


test_expect_success 'checkout.guess = false suppresses branch auto-vivification' '
git checkout -B master &&
status_uno_is_clean &&
test_might_fail git branch -D bar &&

test_config checkout.guess false &&
test_must_fail git checkout bar &&
test_must_fail git rev-parse --verify refs/heads/bar &&
test_branch master
'

test_expect_success 'setup more remotes with unconventional refspecs' ' test_expect_success 'setup more remotes with unconventional refspecs' '
git checkout -B master && git checkout -B master &&
status_uno_is_clean && status_uno_is_clean &&

5
t/t2060-switch.sh

@ -85,9 +85,12 @@ test_expect_success 'switching ignores file of same branch name' '
test_cmp expected actual test_cmp expected actual
' '


test_expect_success 'guess and create branch ' ' test_expect_success 'guess and create branch' '
test_when_finished git switch master && test_when_finished git switch master &&
test_must_fail git switch --no-guess foo && test_must_fail git switch --no-guess foo &&
test_config checkout.guess false &&
test_must_fail git switch foo &&
test_config checkout.guess true &&
git switch foo && git switch foo &&
echo refs/heads/foo >expected && echo refs/heads/foo >expected &&
git symbolic-ref HEAD >actual && git symbolic-ref HEAD >actual &&

52
t/t9902-completion.sh

@ -1360,6 +1360,58 @@ test_expect_success 'git checkout - a later --no-guess overrides previous --gues
EOF EOF
' '


test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
test_config checkout.guess false &&
test_completion "git checkout " <<-\EOF
HEAD Z
master Z
matching-branch Z
matching-tag Z
other/branch-in-other Z
other/master-in-other Z
EOF
'

test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
test_config checkout.guess true &&
test_completion "git checkout " <<-\EOF
HEAD Z
branch-in-other Z
master Z
master-in-other Z
matching-branch Z
matching-tag Z
other/branch-in-other Z
other/master-in-other Z
EOF
'

test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
test_config checkout.guess false &&
test_completion "git checkout --guess " <<-\EOF
HEAD Z
branch-in-other Z
master Z
master-in-other Z
matching-branch Z
matching-tag Z
other/branch-in-other Z
other/master-in-other Z
EOF
'

test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
test_config checkout.guess true &&
test_completion "git checkout --no-guess " <<-\EOF
HEAD Z
master Z
matching-branch Z
matching-tag Z
other/branch-in-other Z
other/master-in-other Z
EOF
'

test_expect_success 'git switch - with --detach, complete all references' ' test_expect_success 'git switch - with --detach, complete all references' '
test_completion "git switch --detach " <<-\EOF test_completion "git switch --detach " <<-\EOF
HEAD Z HEAD Z

Loading…
Cancel
Save