Skip to contentSkip to conversation

checkout: learn the checkout.guess config variable

PATCH Superseded Denton Liu 3 participants 14 messages Oct 4, 2020 – Oct 8, 2020 Documentation

lore
Share & tools
lore

Denton Liu

author
#0 lore raw

cover checkout: learn the checkout.guess config variable

If a user does not wish to use DWIM behaviour, they must manually specify git checkout --no-guess each time. Teach checkout and switch to honour the checkout.guess config variable instead.

Denton Liu (2):
Documentation/config/checkout: replace sq with backticks
checkout: learn to respect checkout.guess

 Documentation/config/checkout.txt      | 21 +++++++----
 Documentation/git-checkout.txt         |  3 ++
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 15 ++++++--
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 103 insertions(+), 14 deletions(-)
Signature · 1 line
2.28.0.942.g77c4c6094c
author
#1 lore raw

PATCH 1/2 Documentation/config/checkout: replace sq with backticks

The modern style for Git documentation is to use backticks to quote any command-line documenation so that it is typeset in monospace. Replace all single quotes with backticks to conform to this.

---
 Documentation/config/checkout.txt | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Documentation/config/checkout.txt +8 -8
index 6b646813ab..e3684a5459 100644
@@ -1,18 +1,18 @@
 checkout.defaultRemote::
-	When you run 'git checkout <something>'
-	or 'git switch <something>' and only have one
+	When you run `git checkout <something>`
+	or `git switch <something>` and only have one
 	remote, it may implicitly fall back on checking out and
-	tracking e.g. 'origin/<something>'. This stops working as soon
-	as you have more than one remote with a '<something>'
+	tracking e.g. `origin/<something>`. This stops working as soon
+	as you have more than one remote with a `<something>`
 	reference. This setting allows for setting the name of a
 	preferred remote that should always win when it comes to
 	disambiguation. The typical use-case is to set this to
 	`origin`.
 +
 Currently this is used by linkgit:git-switch[1] and
-linkgit:git-checkout[1] when 'git checkout <something>'
-or 'git switch <something>'
-will checkout the '<something>' branch on another remote,
-and by linkgit:git-worktree[1] when 'git worktree add' refers to a
+linkgit:git-checkout[1] when `git checkout <something>`
+or `git switch <something>`
+will checkout the `<something>` branch on another remote,
+and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 commands or functionality in the future.
Signature · 1 line
2.28.0.942.g77c4c6094c
author
#2 lore raw

PATCH 2/2 checkout: learn to respect checkout.guess

In reply to Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

---
 Documentation/config/checkout.txt      |  5 +++
 Documentation/git-checkout.txt         |  3 ++
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 15 ++++++--
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 95 insertions(+), 6 deletions(-)
Documentation/config/checkout.txt +5 -0
index e3684a5459..2cddf7b4b4 100644
@@ -16,3 +16,8 @@ will checkout the `<something>` branch on another remote,
 and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 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].
Documentation/git-checkout.txt +3 -0
index afa5c11fd3..28f656d04e 100644
@@ -193,6 +193,9 @@ branches from there if `<branch>` is ambiguous but exists on the
 linkgit:git-config[1].
 +
 Use `--no-guess` to disable this.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -l::
 	Create the new branch's reflog; see linkgit:git-branch[1] for
Documentation/git-switch.txt +3 -0
index 3759c3a265..5c438cd505 100644
@@ -103,6 +103,9 @@ ambiguous but exists on the 'origin' remote. See also
 `checkout.defaultRemote` in linkgit:git-config[1].
 +
 `--guess` is the default behavior. Use `--no-guess` to disable it.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -f::
 --force::
builtin/checkout.c +6 -1
index 0951f8fee5..0c0394a0d6 100644
@@ -1093,11 +1093,16 @@ static int switch_branches(const struct checkout_opts *opts,
 
 static int git_checkout_config(const char *var, const char *value, void *cb)
 {
+	struct checkout_opts *opts = cb;
+
 	if (!strcmp(var, "diff.ignoresubmodules")) {
-		struct checkout_opts *opts = cb;
 		handle_ignore_submodules_arg(&opts->diff_options, value);
 		return 0;
 	}
+	if (!strcmp(var, "checkout.guess")) {
+		opts->dwim_new_local_branch = git_config_bool(var, value);
+		return 0;
+	}
 
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
contrib/completion/git-completion.bash +11 -4
index 8be4a0316e..45ecbb9849 100644
@@ -1470,11 +1470,12 @@ _git_bundle ()
 # To decide between the following rules in priority order
 # 1) the last provided of "--guess" or "--no-guess" explicitly enable or
 #    disable completion of DWIM logic respectively.
-# 2) If the --no-track option is provided, take this as a hint to disable the
+# 2) If checkout.guess is false, disable completion of DWIM logic.
+# 3) If the --no-track option is provided, take this as a hint to disable the
 #    DWIM completion logic
-# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
+# 4) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
 #    logic, as requested by the user.
-# 4) Enable DWIM logic otherwise.
+# 5) Enable DWIM logic otherwise.
 #
 __git_checkout_default_dwim_mode ()
 {
@@ -1485,11 +1486,17 @@ __git_checkout_default_dwim_mode ()
 	fi
 
 	# --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
 		dwim_opt=""
 	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
 	last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
 	case "$last_option" in
t/t2024-checkout-dwim.sh +11 -0
index accfa9aa4b..a4f8d3a67e 100755
@@ -166,6 +166,17 @@ test_expect_success '--no-guess suppresses branch auto-vivification' '
 	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' '
 	git checkout -B master &&
 	status_uno_is_clean &&
t/t2060-switch.sh +4 -1
index 2c1b8c0d6d..68c9101b02 100755
@@ -85,9 +85,12 @@ test_expect_success 'switching ignores file of same branch name' '
 	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_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 &&
 	echo refs/heads/foo >expected &&
 	git symbolic-ref HEAD >actual &&
t/t9902-completion.sh +52 -0

58 lines not on this page · raw message

Signature · 1 line
2.28.0.942.g77c4c6094c
#3 lore raw

Re: [PATCH 2/2] checkout: learn to respect checkout.guess

Quoted 11 lines · Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

---

Asking for input from Dscho on how to futureproof the tests along the same line as js/default-branch-name-part-2 topic.

Quoted 56 lines
 Documentation/config/checkout.txt      |  5 +++
 Documentation/git-checkout.txt         |  3 ++
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 15 ++++++--
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 95 insertions(+), 6 deletions(-)
Documentation/config/checkout.txt +5 -0
index e3684a5459..2cddf7b4b4 100644
@@ -16,3 +16,8 @@ will checkout the `<something>` branch on another remote,
 and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 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].
Documentation/git-checkout.txt +3 -0
index afa5c11fd3..28f656d04e 100644
@@ -193,6 +193,9 @@ branches from there if `<branch>` is ambiguous but exists on the
 linkgit:git-config[1].
 +
 Use `--no-guess` to disable this.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -l::
 	Create the new branch's reflog; see linkgit:git-branch[1] for
Documentation/git-switch.txt +3 -0
index 3759c3a265..5c438cd505 100644
@@ -103,6 +103,9 @@ ambiguous but exists on the 'origin' remote. See also
 `checkout.defaultRemote` in linkgit:git-config[1].
 +
 `--guess` is the default behavior. Use `--no-guess` to disable it.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -f::
 --force::
builtin/checkout.c +0 -0
index 0951f8fee5..0c0394a0d6 100644

builtin/checkout.c static int switch_branches(const struct checkout_opts *opts, · line 1097

Quoted 7 lines
builtin/checkout.c +2 -1
@@ -1093,11 +1093,16 @@ static int switch_branches(const struct checkout_opts *opts,
 
 static int git_checkout_config(const char *var, const char *value, void *cb)
 {
+	struct checkout_opts *opts = cb;
+
 	if (!strcmp(var, "diff.ignoresubmodules")) {
-		struct checkout_opts *opts = cb;

This change does make sense even without a new configuration variable, as 'cb' given by the caller applies to everything this function handles, not just for a particular configuration variable (not a suggestion to split this out as a separate clean-up patch; it is too small for it to be worth).

builtin/checkout.c static int switch_branches(const struct checkout_opts *opts, · line 1108

Quoted 10 lines
builtin/checkout.c +4 -0
 		handle_ignore_submodules_arg(&opts->diff_options, value);
 		return 0;
 	}
+	if (!strcmp(var, "checkout.guess")) {
+		opts->dwim_new_local_branch = git_config_bool(var, value);
+		return 0;
+	}
 
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);

OK.

Quoted 8 lines
contrib/completion/git-completion.bash +0 -0
index 8be4a0316e..45ecbb9849 100644
@@ -1470,11 +1470,12 @@ _git_bundle ()
 # To decide between the following rules in priority order
 # 1) the last provided of "--guess" or "--no-guess" explicitly enable or
 #    disable completion of DWIM logic respectively.

contrib/completion/git-completion.bash _git_bundle () · line 1479

Quoted 10 lines
contrib/completion/git-completion.bash +4 -3
-# 2) If the --no-track option is provided, take this as a hint to disable the
+# 2) If checkout.guess is false, disable completion of DWIM logic.
+# 3) If the --no-track option is provided, take this as a hint to disable the
 #    DWIM completion logic
-# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
+# 4) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
 #    logic, as requested by the user.
-# 4) Enable DWIM logic otherwise.
+# 5) Enable DWIM logic otherwise.
 #

We may went to consider dropping numbers from these entries, as nobody refers to another entry with its number (e.g. "contrary to step 2, if this condition holds, do X"), and we already say "in this order" upfront.

Quoted 124 lines
contrib/completion/git-completion.bash +7 -1
 __git_checkout_default_dwim_mode ()
 {
@@ -1485,11 +1486,17 @@ __git_checkout_default_dwim_mode ()
 	fi
 
 	# --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
 		dwim_opt=""
 	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
 	last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
 	case "$last_option" in
t/t2024-checkout-dwim.sh +11 -0
index accfa9aa4b..a4f8d3a67e 100755
@@ -166,6 +166,17 @@ test_expect_success '--no-guess suppresses branch auto-vivification' '
 	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' '
 	git checkout -B master &&
 	status_uno_is_clean &&
t/t2060-switch.sh +4 -1
index 2c1b8c0d6d..68c9101b02 100755
@@ -85,9 +85,12 @@ test_expect_success 'switching ignores file of same branch name' '
 	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_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 &&
 	echo refs/heads/foo >expected &&
 	git symbolic-ref HEAD >actual &&
t/t9902-completion.sh +52 -0
index 8425b9a531..2b469a71ca 100755
@@ -1360,6 +1360,58 @@ test_expect_success 'git checkout - a later --no-guess overrides previous --gues
 	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_completion "git switch --detach " <<-\EOF
 	HEAD Z
author
#4 lore raw

Re: [PATCH 2/2] checkout: learn to respect checkout.guess

Hi Junio,

Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Quoted 8 lines
Quoted 5 lines

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

---

Asking for input from Dscho on how to futureproof the tests along the same line as js/default-branch-name-part-2 topic.

Perhaps we could extract lists that are repeated often into common files so that they only need to be changed in one place? Something like this:

-- >8 --

t/t9902-completion.sh +6 -2
index 7b7bc6e4bd..86da44231d 100755
@@ -1240,8 +1240,8 @@ test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
 	test_cmp expected out
 '
 
-test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
-	test_completion "git switch " <<-\EOF
+test_expect_success 'prepare expect files for DWIM tests' '
+	cat >local-and-unique-remote <<-\EOF &&
 	branch-in-other Z
 	master Z
 	master-in-other Z
@@ -1249,6 +1249,10 @@ test_expect_success 'git switch - with no options, complete local branches and u
 	EOF
 '
 
+test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
+	test_completion "git switch " <local-and-unique-remote
+'
+
 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
 	test_completion "git checkout " <<-\EOF
 	HEAD Z
#5 lore raw

Re: [PATCH 2/2] checkout: learn to respect checkout.guess

Quoted 14 lines · Denton Liu

Hi Junio,

Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

---

Asking for input from Dscho on how to futureproof the tests along the same line as js/default-branch-name-part-2 topic.

Perhaps we could extract lists that are repeated often into common files so that they only need to be changed in one place? Something like this:

or we can feed a prefix that is not 'm' so that master, main, or maint branches are not involved at all in the completion?

Quoted 28 lines

-- >8 --

t/t9902-completion.sh +6 -2
index 7b7bc6e4bd..86da44231d 100755
@@ -1240,8 +1240,8 @@ test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
 	test_cmp expected out
 '
 
-test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
-	test_completion "git switch " <<-\EOF
+test_expect_success 'prepare expect files for DWIM tests' '
+	cat >local-and-unique-remote <<-\EOF &&
 	branch-in-other Z
 	master Z
 	master-in-other Z
@@ -1249,6 +1249,10 @@ test_expect_success 'git switch - with no options, complete local branches and u
 	EOF
 '
 
+test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
+	test_completion "git switch " <local-and-unique-remote
+'
+
 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
 	test_completion "git checkout " <<-\EOF
 	HEAD Z
#6 lore raw

Re: [PATCH 2/2] checkout: learn to respect checkout.guess

Hi,

Quoted 17 lines · Junio C Hamano
Quoted 15 lines · Denton Liu
Quoted 14 lines · Junio C Hamano
Quoted 11 lines · Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

---

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Asking for input from Dscho on how to futureproof the tests along the same line as js/default-branch-name-part-2 topic.

Perhaps we could extract lists that are repeated often into common files so that they only need to be changed in one place? Something like this:

or we can feed a prefix that is not 'm' so that master, main, or maint branches are not involved at all in the completion?

While I appreciate the consideration, the issue should be easy enough to address. I don't think we need to worry too much about that in the context of this here patch series.

Ciao,
Dscho

Quoted 30 lines
Quoted 29 lines

-- >8 --

t/t9902-completion.sh +6 -2
index 7b7bc6e4bd..86da44231d 100755
@@ -1240,8 +1240,8 @@ test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
 	test_cmp expected out
 '
 
-test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
-	test_completion "git switch " <<-\EOF
+test_expect_success 'prepare expect files for DWIM tests' '
+	cat >local-and-unique-remote <<-\EOF &&
 	branch-in-other Z
 	master Z
 	master-in-other Z
@@ -1249,6 +1249,10 @@ test_expect_success 'git switch - with no options, complete local branches and u
 	EOF
 '
 
+test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
+	test_completion "git switch " <local-and-unique-remote
+'
+
 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
 	test_completion "git checkout " <<-\EOF
 	HEAD Z
author
#7 lore raw

v2 cover checkout: learn the checkout.guess config variable

In reply to Denton Liu

If a user does not wish to use DWIM behaviour, they must manually specify git checkout --no-guess each time. Teach checkout and switch to honour the checkout.guess config variable instead.

Changes since v1:

* Replace enumerated priority list with bullet points

Denton Liu (2):
Documentation/config/checkout: replace sq with backticks
checkout: learn to respect checkout.guess

 Documentation/config/checkout.txt      | 21 +++++++----
 Documentation/git-checkout.txt         |  3 ++
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 25 ++++++++-----
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 108 insertions(+), 19 deletions(-)

Range-diff against v1:

1:  f08d3f796a = 1:  f08d3f796a Documentation/config/checkout: replace sq with backticks
2:  8172602f6f ! 2:  2678680a08 checkout: learn to respect checkout.guess

@@ builtin/checkout.c: static int switch_branches(const struct checkout_opts *opts,

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

Signature · 1 line
2.28.0.942.g77c4c6094c
author
#8 lore raw

PATCH v2 1/2 Documentation/config/checkout: replace sq with backticks

The modern style for Git documentation is to use backticks to quote any command-line documenation so that it is typeset in monospace. Replace all single quotes with backticks to conform to this.

---
 Documentation/config/checkout.txt | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Documentation/config/checkout.txt +8 -8
index 6b646813ab..e3684a5459 100644
@@ -1,18 +1,18 @@
 checkout.defaultRemote::
-	When you run 'git checkout <something>'
-	or 'git switch <something>' and only have one
+	When you run `git checkout <something>`
+	or `git switch <something>` and only have one
 	remote, it may implicitly fall back on checking out and
-	tracking e.g. 'origin/<something>'. This stops working as soon
-	as you have more than one remote with a '<something>'
+	tracking e.g. `origin/<something>`. This stops working as soon
+	as you have more than one remote with a `<something>`
 	reference. This setting allows for setting the name of a
 	preferred remote that should always win when it comes to
 	disambiguation. The typical use-case is to set this to
 	`origin`.
 +
 Currently this is used by linkgit:git-switch[1] and
-linkgit:git-checkout[1] when 'git checkout <something>'
-or 'git switch <something>'
-will checkout the '<something>' branch on another remote,
-and by linkgit:git-worktree[1] when 'git worktree add' refers to a
+linkgit:git-checkout[1] when `git checkout <something>`
+or `git switch <something>`
+will checkout the `<something>` branch on another remote,
+and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 commands or functionality in the future.
Signature · 1 line
2.28.0.942.g77c4c6094c
author
#9 lore raw

PATCH v2 2/2 checkout: learn to respect checkout.guess

In reply to Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

---
 Documentation/config/checkout.txt      |  5 +++
 Documentation/git-checkout.txt         |  3 ++
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 25 ++++++++-----
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 100 insertions(+), 11 deletions(-)
Documentation/config/checkout.txt +5 -0
index e3684a5459..2cddf7b4b4 100644
@@ -16,3 +16,8 @@ will checkout the `<something>` branch on another remote,
 and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 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].
Documentation/git-checkout.txt +3 -0
index afa5c11fd3..28f656d04e 100644
@@ -193,6 +193,9 @@ branches from there if `<branch>` is ambiguous but exists on the
 linkgit:git-config[1].
 +
 Use `--no-guess` to disable this.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -l::
 	Create the new branch's reflog; see linkgit:git-branch[1] for
Documentation/git-switch.txt +3 -0
index 3759c3a265..5c438cd505 100644
@@ -103,6 +103,9 @@ ambiguous but exists on the 'origin' remote. See also
 `checkout.defaultRemote` in linkgit:git-config[1].
 +
 `--guess` is the default behavior. Use `--no-guess` to disable it.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -f::
 --force::
builtin/checkout.c +6 -1
index 0951f8fee5..0c0394a0d6 100644
@@ -1093,11 +1093,16 @@ static int switch_branches(const struct checkout_opts *opts,
 
 static int git_checkout_config(const char *var, const char *value, void *cb)
 {
+	struct checkout_opts *opts = cb;
+
 	if (!strcmp(var, "diff.ignoresubmodules")) {
-		struct checkout_opts *opts = cb;
 		handle_ignore_submodules_arg(&opts->diff_options, value);
 		return 0;
 	}
+	if (!strcmp(var, "checkout.guess")) {
+		opts->dwim_new_local_branch = git_config_bool(var, value);
+		return 0;
+	}
 
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
contrib/completion/git-completion.bash +16 -9
index 8be4a0316e..851c358cfc 100644
@@ -1467,14 +1467,15 @@ _git_bundle ()
 # Helper function to decide whether or not we should enable DWIM logic for
 # git-switch and git-checkout.
 #
-# To decide between the following rules in priority order
-# 1) the last provided of "--guess" or "--no-guess" explicitly enable or
-#    disable completion of DWIM logic respectively.
-# 2) If the --no-track option is provided, take this as a hint to disable the
-#    DWIM completion logic
-# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
-#    logic, as requested by the user.
-# 4) Enable DWIM logic otherwise.
+# To decide between the following rules in decreasing priority order:
+# - the last provided of "--guess" or "--no-guess" explicitly enable or
+#   disable completion of DWIM logic respectively.
+# - If checkout.guess is false, disable completion of DWIM logic.
+# - If the --no-track option is provided, take this as a hint to disable the
+#   DWIM completion logic
+# - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
+#   logic, as requested by the user.
+# - Enable DWIM logic otherwise.
 #
 __git_checkout_default_dwim_mode ()
 {
@@ -1485,11 +1486,17 @@ __git_checkout_default_dwim_mode ()
 	fi
 
 	# --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
 		dwim_opt=""
 	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
 	last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
 	case "$last_option" in
t/t2024-checkout-dwim.sh +11 -0
index accfa9aa4b..a4f8d3a67e 100755
@@ -166,6 +166,17 @@ test_expect_success '--no-guess suppresses branch auto-vivification' '
 	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' '
 	git checkout -B master &&
 	status_uno_is_clean &&
t/t2060-switch.sh +4 -1
index 2c1b8c0d6d..68c9101b02 100755
@@ -85,9 +85,12 @@ test_expect_success 'switching ignores file of same branch name' '
 	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_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 &&
 	echo refs/heads/foo >expected &&
 	git symbolic-ref HEAD >actual &&
t/t9902-completion.sh +52 -0

58 lines not on this page · raw message

Signature · 1 line
2.28.0.942.g77c4c6094c
#10 lore raw

Re: [PATCH v2 2/2] checkout: learn to respect checkout.guess

Quoted 19 lines · Denton Liu
Documentation/git-checkout.txt +3 -0
index afa5c11fd3..28f656d04e 100644
@@ -193,6 +193,9 @@ branches from there if `<branch>` is ambiguous but exists on the
 linkgit:git-config[1].
 +
 Use `--no-guess` to disable this.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -l::
 	Create the new branch's reflog; see linkgit:git-branch[1] for
Documentation/git-switch.txt +0 -0
index 3759c3a265..5c438cd505 100644

Documentation/git-switch.txt ambiguous but exists on the 'origin' remote. See also · line 108

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

Contrasting the above two, one cannot help but notice that it is totally unclear how "git checkout" that is not explicitly told by command line or configuration behaves. Perhaps "--guess is the default behaviour." needs to be transplated from the description for "git switch" to clarify?

This is not a new issue, but is made stand out even more prominently due to the addition.

Thanks.

author
#11 lore raw

v3 cover checkout: learn the checkout.guess config variable

In reply to Denton Liu

If a user does not wish to use DWIM behaviour, they must manually specify git checkout --no-guess each time. Teach checkout and switch to honour the checkout.guess config variable instead.

Changes since v1:

* Replace enumerated priority list with bullet points

Changes since v2:

* Clarify that --guess is the default for checkout in the docs

Denton Liu (2):
Documentation/config/checkout: replace sq with backticks
checkout: learn to respect checkout.guess

 Documentation/config/checkout.txt      | 21 +++++++----
 Documentation/git-checkout.txt         |  5 ++-
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 25 ++++++++-----
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 109 insertions(+), 20 deletions(-)

Range-diff against v2:

1:  f08d3f796a = 1:  f08d3f796a Documentation/config/checkout: replace sq with backticks
2:  2678680a08 ! 2:  eb9daf4bcd checkout: learn to respect checkout.guess

@@ Documentation/config/checkout.txt: will checkout the <something> branch on ano

## Documentation/git-checkout.txt ##
@@ Documentation/git-checkout.txt: branches from there if <branch> is ambiguous but exists on the
+ 'origin' remote. See also checkout.defaultRemote in
linkgit:git-config[1].
+
- Use --no-guess to disable this.
+-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.

Signature · 1 line
2.28.0.942.g77c4c6094c
author
#12 lore raw

PATCH v3 1/2 Documentation/config/checkout: replace sq with backticks

The modern style for Git documentation is to use backticks to quote any command-line documenation so that it is typeset in monospace. Replace all single quotes with backticks to conform to this.

---
 Documentation/config/checkout.txt | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Documentation/config/checkout.txt +8 -8
index 6b646813ab..e3684a5459 100644
@@ -1,18 +1,18 @@
 checkout.defaultRemote::
-	When you run 'git checkout <something>'
-	or 'git switch <something>' and only have one
+	When you run `git checkout <something>`
+	or `git switch <something>` and only have one
 	remote, it may implicitly fall back on checking out and
-	tracking e.g. 'origin/<something>'. This stops working as soon
-	as you have more than one remote with a '<something>'
+	tracking e.g. `origin/<something>`. This stops working as soon
+	as you have more than one remote with a `<something>`
 	reference. This setting allows for setting the name of a
 	preferred remote that should always win when it comes to
 	disambiguation. The typical use-case is to set this to
 	`origin`.
 +
 Currently this is used by linkgit:git-switch[1] and
-linkgit:git-checkout[1] when 'git checkout <something>'
-or 'git switch <something>'
-will checkout the '<something>' branch on another remote,
-and by linkgit:git-worktree[1] when 'git worktree add' refers to a
+linkgit:git-checkout[1] when `git checkout <something>`
+or `git switch <something>`
+will checkout the `<something>` branch on another remote,
+and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 commands or functionality in the future.
Signature · 1 line
2.28.0.942.g77c4c6094c
author
#13 lore raw

PATCH v3 2/2 checkout: learn to respect checkout.guess

In reply to Denton Liu

The current behavior of git checkout/switch is that --guess is currently enabled by default. However, some users may not wish for this to happen automatically. Instead of forcing users to specify --no-guess manually each time, teach these commands the checkout.guess configuration variable that gives users the option to set a default behavior.

Teach the completion script to recognize the new config variable and disable DWIM logic if it is set to false.

---
 Documentation/config/checkout.txt      |  5 +++
 Documentation/git-checkout.txt         |  5 ++-
 Documentation/git-switch.txt           |  3 ++
 builtin/checkout.c                     |  7 +++-
 contrib/completion/git-completion.bash | 25 ++++++++-----
 t/t2024-checkout-dwim.sh               | 11 ++++++
 t/t2060-switch.sh                      |  5 ++-
 t/t9902-completion.sh                  | 52 ++++++++++++++++++++++++++
 8 files changed, 101 insertions(+), 12 deletions(-)
Documentation/config/checkout.txt +5 -0
index e3684a5459..2cddf7b4b4 100644
@@ -16,3 +16,8 @@ will checkout the `<something>` branch on another remote,
 and by linkgit:git-worktree[1] when `git worktree add` refers to a
 remote branch. This setting might be used for other checkout-like
 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].
Documentation/git-checkout.txt +4 -1
index afa5c11fd3..1435d3de9e 100644
@@ -192,7 +192,10 @@ branches from there if `<branch>` is ambiguous but exists on the
 'origin' remote. See also `checkout.defaultRemote` in
 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::
 	Create the new branch's reflog; see linkgit:git-branch[1] for
Documentation/git-switch.txt +3 -0
index 3759c3a265..5c438cd505 100644
@@ -103,6 +103,9 @@ ambiguous but exists on the 'origin' remote. See also
 `checkout.defaultRemote` in linkgit:git-config[1].
 +
 `--guess` is the default behavior. Use `--no-guess` to disable it.
++
+The default behavior can be set via the `checkout.guess` configuration
+variable.
 
 -f::
 --force::
builtin/checkout.c +6 -1
index 0951f8fee5..0c0394a0d6 100644
@@ -1093,11 +1093,16 @@ static int switch_branches(const struct checkout_opts *opts,
 
 static int git_checkout_config(const char *var, const char *value, void *cb)
 {
+	struct checkout_opts *opts = cb;
+
 	if (!strcmp(var, "diff.ignoresubmodules")) {
-		struct checkout_opts *opts = cb;
 		handle_ignore_submodules_arg(&opts->diff_options, value);
 		return 0;
 	}
+	if (!strcmp(var, "checkout.guess")) {
+		opts->dwim_new_local_branch = git_config_bool(var, value);
+		return 0;
+	}
 
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
contrib/completion/git-completion.bash +16 -9
index 8be4a0316e..851c358cfc 100644
@@ -1467,14 +1467,15 @@ _git_bundle ()
 # Helper function to decide whether or not we should enable DWIM logic for
 # git-switch and git-checkout.
 #
-# To decide between the following rules in priority order
-# 1) the last provided of "--guess" or "--no-guess" explicitly enable or
-#    disable completion of DWIM logic respectively.
-# 2) If the --no-track option is provided, take this as a hint to disable the
-#    DWIM completion logic
-# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
-#    logic, as requested by the user.
-# 4) Enable DWIM logic otherwise.
+# To decide between the following rules in decreasing priority order:
+# - the last provided of "--guess" or "--no-guess" explicitly enable or
+#   disable completion of DWIM logic respectively.
+# - If checkout.guess is false, disable completion of DWIM logic.
+# - If the --no-track option is provided, take this as a hint to disable the
+#   DWIM completion logic
+# - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
+#   logic, as requested by the user.
+# - Enable DWIM logic otherwise.
 #
 __git_checkout_default_dwim_mode ()
 {
@@ -1485,11 +1486,17 @@ __git_checkout_default_dwim_mode ()
 	fi
 
 	# --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
 		dwim_opt=""
 	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
 	last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
 	case "$last_option" in
t/t2024-checkout-dwim.sh +11 -0
index accfa9aa4b..a4f8d3a67e 100755
@@ -166,6 +166,17 @@ test_expect_success '--no-guess suppresses branch auto-vivification' '
 	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' '
 	git checkout -B master &&
 	status_uno_is_clean &&
t/t2060-switch.sh +4 -1
index 2c1b8c0d6d..68c9101b02 100755
@@ -85,9 +85,12 @@ test_expect_success 'switching ignores file of same branch name' '
 	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_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 &&
 	echo refs/heads/foo >expected &&
 	git symbolic-ref HEAD >actual &&
t/t9902-completion.sh +52 -0

58 lines not on this page · raw message

Signature · 1 line
2.28.0.942.g77c4c6094c