Skip to content

Commit c9a6bc9

Browse files
committed
Merge branch 'dl/checkout-guess' into next
"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
2 parents ad60933 + 64f1f58 commit c9a6bc9

File tree

8 files changed

+109
-20
lines changed

8 files changed

+109
-20
lines changed

Documentation/config/checkout.txt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
checkout.defaultRemote::
2-
When you run 'git checkout <something>'
3-
or 'git switch <something>' and only have one
2+
When you run `git checkout <something>`
3+
or `git switch <something>` and only have one
44
remote, it may implicitly fall back on checking out and
5-
tracking e.g. 'origin/<something>'. This stops working as soon
6-
as you have more than one remote with a '<something>'
5+
tracking e.g. `origin/<something>`. This stops working as soon
6+
as you have more than one remote with a `<something>`
77
reference. This setting allows for setting the name of a
88
preferred remote that should always win when it comes to
99
disambiguation. The typical use-case is to set this to
1010
`origin`.
1111
+
1212
Currently this is used by linkgit:git-switch[1] and
13-
linkgit:git-checkout[1] when 'git checkout <something>'
14-
or 'git switch <something>'
15-
will checkout the '<something>' branch on another remote,
16-
and by linkgit:git-worktree[1] when 'git worktree add' refers to a
13+
linkgit:git-checkout[1] when `git checkout <something>`
14+
or `git switch <something>`
15+
will checkout the `<something>` branch on another remote,
16+
and by linkgit:git-worktree[1] when `git worktree add` refers to a
1717
remote branch. This setting might be used for other checkout-like
1818
commands or functionality in the future.
1919

20+
checkout.guess::
21+
Provides the default value for the `--guess` or `--no-guess`
22+
option in `git checkout` and `git switch`. See
23+
linkgit:git-switch[1] and linkgit:git-checkout[1].
24+
2025
checkout.workers::
2126
The number of parallel workers to use when updating the working tree.
2227
The default is one, i.e. sequential execution. If set to a value less

Documentation/git-checkout.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ branches from there if `<branch>` is ambiguous but exists on the
192192
'origin' remote. See also `checkout.defaultRemote` in
193193
linkgit:git-config[1].
194194
+
195-
Use `--no-guess` to disable this.
195+
`--guess` is the default behavior. Use `--no-guess` to disable it.
196+
+
197+
The default behavior can be set via the `checkout.guess` configuration
198+
variable.
196199

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

Documentation/git-switch.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ ambiguous but exists on the 'origin' remote. See also
103103
`checkout.defaultRemote` in linkgit:git-config[1].
104104
+
105105
`--guess` is the default behavior. Use `--no-guess` to disable it.
106+
+
107+
The default behavior can be set via the `checkout.guess` configuration
108+
variable.
106109

107110
-f::
108111
--force::

builtin/checkout.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,11 +1119,16 @@ static int switch_branches(const struct checkout_opts *opts,
11191119

11201120
static int git_checkout_config(const char *var, const char *value, void *cb)
11211121
{
1122+
struct checkout_opts *opts = cb;
1123+
11221124
if (!strcmp(var, "diff.ignoresubmodules")) {
1123-
struct checkout_opts *opts = cb;
11241125
handle_ignore_submodules_arg(&opts->diff_options, value);
11251126
return 0;
11261127
}
1128+
if (!strcmp(var, "checkout.guess")) {
1129+
opts->dwim_new_local_branch = git_config_bool(var, value);
1130+
return 0;
1131+
}
11271132

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

contrib/completion/git-completion.bash

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,14 +1467,15 @@ _git_bundle ()
14671467
# Helper function to decide whether or not we should enable DWIM logic for
14681468
# git-switch and git-checkout.
14691469
#
1470-
# To decide between the following rules in priority order
1471-
# 1) the last provided of "--guess" or "--no-guess" explicitly enable or
1472-
# disable completion of DWIM logic respectively.
1473-
# 2) If the --no-track option is provided, take this as a hint to disable the
1474-
# DWIM completion logic
1475-
# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1476-
# logic, as requested by the user.
1477-
# 4) Enable DWIM logic otherwise.
1470+
# To decide between the following rules in decreasing priority order:
1471+
# - the last provided of "--guess" or "--no-guess" explicitly enable or
1472+
# disable completion of DWIM logic respectively.
1473+
# - If checkout.guess is false, disable completion of DWIM logic.
1474+
# - If the --no-track option is provided, take this as a hint to disable the
1475+
# DWIM completion logic
1476+
# - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1477+
# logic, as requested by the user.
1478+
# - Enable DWIM logic otherwise.
14781479
#
14791480
__git_checkout_default_dwim_mode ()
14801481
{
@@ -1485,11 +1486,17 @@ __git_checkout_default_dwim_mode ()
14851486
fi
14861487

14871488
# --no-track disables DWIM, but with lower priority than
1488-
# --guess/--no-guess
1489+
# --guess/--no-guess/checkout.guess
14891490
if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
14901491
dwim_opt=""
14911492
fi
14921493

1494+
# checkout.guess = false disables DWIM, but with lower priority than
1495+
# --guess/--no-guess
1496+
if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1497+
dwim_opt=""
1498+
fi
1499+
14931500
# Find the last provided --guess or --no-guess
14941501
last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
14951502
case "$last_option" in

t/t2024-checkout-dwim.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ test_expect_success '--no-guess suppresses branch auto-vivification' '
166166
test_branch master
167167
'
168168

169+
test_expect_success 'checkout.guess = false suppresses branch auto-vivification' '
170+
git checkout -B master &&
171+
status_uno_is_clean &&
172+
test_might_fail git branch -D bar &&
173+
174+
test_config checkout.guess false &&
175+
test_must_fail git checkout bar &&
176+
test_must_fail git rev-parse --verify refs/heads/bar &&
177+
test_branch master
178+
'
179+
169180
test_expect_success 'setup more remotes with unconventional refspecs' '
170181
git checkout -B master &&
171182
status_uno_is_clean &&

t/t2060-switch.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ test_expect_success 'switching ignores file of same branch name' '
8585
test_cmp expected actual
8686
'
8787

88-
test_expect_success 'guess and create branch ' '
88+
test_expect_success 'guess and create branch' '
8989
test_when_finished git switch master &&
9090
test_must_fail git switch --no-guess foo &&
91+
test_config checkout.guess false &&
92+
test_must_fail git switch foo &&
93+
test_config checkout.guess true &&
9194
git switch foo &&
9295
echo refs/heads/foo >expected &&
9396
git symbolic-ref HEAD >actual &&

t/t9902-completion.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,58 @@ test_expect_success 'git checkout - a later --no-guess overrides previous --gues
13601360
EOF
13611361
'
13621362

1363+
test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
1364+
test_config checkout.guess false &&
1365+
test_completion "git checkout " <<-\EOF
1366+
HEAD Z
1367+
master Z
1368+
matching-branch Z
1369+
matching-tag Z
1370+
other/branch-in-other Z
1371+
other/master-in-other Z
1372+
EOF
1373+
'
1374+
1375+
test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
1376+
test_config checkout.guess true &&
1377+
test_completion "git checkout " <<-\EOF
1378+
HEAD Z
1379+
branch-in-other Z
1380+
master Z
1381+
master-in-other Z
1382+
matching-branch Z
1383+
matching-tag Z
1384+
other/branch-in-other Z
1385+
other/master-in-other Z
1386+
EOF
1387+
'
1388+
1389+
test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
1390+
test_config checkout.guess false &&
1391+
test_completion "git checkout --guess " <<-\EOF
1392+
HEAD Z
1393+
branch-in-other Z
1394+
master Z
1395+
master-in-other Z
1396+
matching-branch Z
1397+
matching-tag Z
1398+
other/branch-in-other Z
1399+
other/master-in-other Z
1400+
EOF
1401+
'
1402+
1403+
test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
1404+
test_config checkout.guess true &&
1405+
test_completion "git checkout --no-guess " <<-\EOF
1406+
HEAD Z
1407+
master Z
1408+
matching-branch Z
1409+
matching-tag Z
1410+
other/branch-in-other Z
1411+
other/master-in-other Z
1412+
EOF
1413+
'
1414+
13631415
test_expect_success 'git switch - with --detach, complete all references' '
13641416
test_completion "git switch --detach " <<-\EOF
13651417
HEAD Z

0 commit comments

Comments
 (0)