git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Blain via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>,
	Philippe Blain <levraiphilippeblain@gmail.com>,
	Philippe Blain <levraiphilippeblain@gmail.com>,
	Philippe Blain <levraiphilippeblain@gmail.com>
Subject: [PATCH v3 3/4] completion: add and use __git_compute_first_level_config_vars_for_section
Date: Sat, 10 Feb 2024 18:32:22 +0000	[thread overview]
Message-ID: <fb210325394af2cc3389a780736d236bf9ac4f93.1707589943.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1660.v3.git.git.1707589943.gitgitgadget@gmail.com>

From: Philippe Blain <levraiphilippeblain@gmail.com>

The function __git_complete_config_variable_name in the Bash completion
script hardcodes several config variable names. These variables are
those in config sections where user-defined names can appear, such as
"branch.<name>". These sections are treated first by the case statement,
and the two last "catch all" cases are used for other sections, making
use of the __git_compute_config_vars and __git_compute_config_sections
function, which omit listing any variables containing wildcards or
placeholders. Having hardcoded config variables introduces the risk of
the completion code becoming out of sync with the actual config
variables accepted by Git.

To avoid these hardcoded config variables, introduce a new function,
__git_compute_first_level_config_vars_for_section, making use of the
existing __git_config_vars variable. This function takes as argument a
config section name and computes the matching "first level" config
variables for that section, i.e. those _not_ containing any placeholder,
like 'branch.autoSetupMerge, 'remote.pushDefault', etc.  Use this
function and the variables it defines in the 'branch.*', 'remote.*' and
'submodule.*' switches of the case statement instead of hardcoding the
corresponding config variables.  Note that we use indirect expansion to
create a variable for each section, instead of using a single
associative array indexed by section names, because associative arrays
are not supported in Bash 3, on which macOS is stuck for licensing
reasons.

Use the existing pattern in the completion script of using global
variables to cache the list of config variables for each section. The
rationale for such caching is explained in eaa4e6ee2a (Speed up bash
completion loading, 2009-11-17), and the current approach to using and
defining them via 'test -n' is explained in cf0ff02a38 (completion: work
around zsh option propagation bug, 2012-02-02).

Adjust the name of one of the tests added in the previous commit,
reflecting that it now also tests the new function.

Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
---
 contrib/completion/git-completion.bash | 24 +++++++++++++++++++++---
 t/t9902-completion.sh                  |  2 +-
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 8af9bc3f4e1..57a8da7ca1a 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2596,6 +2596,15 @@ __git_compute_config_vars ()
 	__git_config_vars="$(git help --config-for-completion)"
 }
 
+__git_compute_first_level_config_vars_for_section ()
+{
+	local section="$1"
+	__git_compute_config_vars
+	local this_section="__git_first_level_config_vars_for_section_${section}"
+	test -n "${!this_section}" ||
+	printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
+}
+
 __git_config_sections=
 __git_compute_config_sections ()
 {
@@ -2749,8 +2758,11 @@ __git_complete_config_variable_name ()
 	branch.*)
 		local pfx="${cur_%.*}."
 		cur_="${cur_#*.}"
+		local section="${pfx%.}"
 		__gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
-		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx:- }"
+		__git_compute_first_level_config_vars_for_section "${section}"
+		local this_section="__git_first_level_config_vars_for_section_${section}"
+		__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
 		return
 		;;
 	guitool.*.*)
@@ -2799,8 +2811,11 @@ __git_complete_config_variable_name ()
 	remote.*)
 		local pfx="${cur_%.*}."
 		cur_="${cur_#*.}"
+		local section="${pfx%.}"
 		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
-		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx:- }"
+		__git_compute_first_level_config_vars_for_section "${section}"
+		local this_section="__git_first_level_config_vars_for_section_${section}"
+		__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
 		return
 		;;
 	submodule.*.*)
@@ -2812,8 +2827,11 @@ __git_complete_config_variable_name ()
 	submodule.*)
 		local pfx="${cur_%.*}."
 		cur_="${cur_#*.}"
+		local section="${pfx%.}"
 		__gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
-		__gitcomp_nl_append $'alternateErrorStrategy\nfetchJobs\nactive\nalternateLocation\nrecurse\npropagateBranches' "$pfx" "$cur_" "${sfx:- }"
+		__git_compute_first_level_config_vars_for_section "${section}"
+		local this_section="__git_first_level_config_vars_for_section_${section}"
+		__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
 		return
 		;;
 	url.*.*)
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 23d0e71324c..8600b9e0dd9 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2589,7 +2589,7 @@ test_expect_success 'setup for git config submodule tests' '
 	git submodule add ./sub
 '
 
-test_expect_success 'git config - variable name - submodule' '
+test_expect_success 'git config - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
 	test_completion "git config submodule." <<-\EOF
 	submodule.active Z
 	submodule.alternateErrorStrategy Z
-- 
gitgitgadget


  parent reply	other threads:[~2024-02-10 18:32 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-28 20:02 [PATCH 0/5] completion: remove hardcoded config variable names Philippe Blain via GitGitGadget
2024-01-28 20:02 ` [PATCH 1/5] completion: add space after config variable names also in Bash 3 Philippe Blain via GitGitGadget
2024-01-28 20:02 ` [PATCH 2/5] completion: complete 'submodule.*' config variables Philippe Blain via GitGitGadget
2024-01-28 20:02 ` [PATCH 3/5] completion: add and use __git_compute_first_level_config_vars_for_section Philippe Blain via GitGitGadget
2024-01-28 20:02 ` [PATCH 4/5] builtin/help: add --config-all-for-completion Philippe Blain via GitGitGadget
2024-01-28 20:02 ` [PATCH 5/5] completion: add an use __git_compute_second_level_config_vars_for_section Philippe Blain via GitGitGadget
2024-01-29 13:27 ` [PATCH v2 0/5] completion: remove hardcoded config variable names Philippe Blain via GitGitGadget
2024-01-29 13:27   ` [PATCH v2 1/5] completion: add space after config variable names also in Bash 3 Philippe Blain via GitGitGadget
2024-01-29 13:27   ` [PATCH v2 2/5] completion: complete 'submodule.*' config variables Philippe Blain via GitGitGadget
2024-02-08  7:42     ` Patrick Steinhardt
2024-02-10 15:39       ` Philippe Blain
2024-01-29 13:27   ` [PATCH v2 3/5] completion: add and use __git_compute_first_level_config_vars_for_section Philippe Blain via GitGitGadget
2024-02-08  7:42     ` Patrick Steinhardt
2024-02-10 16:06       ` Philippe Blain
2024-02-10 17:15         ` Junio C Hamano
2024-02-10 17:27           ` Philippe Blain
2024-02-14  0:24             ` Junio C Hamano
2024-01-29 13:28   ` [PATCH v2 4/5] builtin/help: add --config-all-for-completion Philippe Blain via GitGitGadget
2024-02-08  7:42     ` Patrick Steinhardt
2024-02-10 16:13       ` Philippe Blain
2024-01-29 13:28   ` [PATCH v2 5/5] completion: add an use __git_compute_second_level_config_vars_for_section Philippe Blain via GitGitGadget
2024-02-08  7:42     ` Patrick Steinhardt
2024-02-10 16:19       ` Philippe Blain
2024-02-07 22:08   ` [PATCH v2 0/5] completion: remove hardcoded config variable names Junio C Hamano
2024-02-08  7:42     ` Patrick Steinhardt
2024-02-10 18:32   ` [PATCH v3 0/4] " Philippe Blain via GitGitGadget
2024-02-10 18:32     ` [PATCH v3 1/4] completion: add space after config variable names also in Bash 3 Philippe Blain via GitGitGadget
2024-02-10 18:32     ` [PATCH v3 2/4] completion: complete 'submodule.*' config variables Philippe Blain via GitGitGadget
2024-02-10 18:32     ` Philippe Blain via GitGitGadget [this message]
2024-02-10 18:32     ` [PATCH v3 4/4] completion: add and use __git_compute_second_level_config_vars_for_section Philippe Blain via GitGitGadget
2024-02-13  9:35     ` [PATCH v3 0/4] completion: remove hardcoded config variable names Patrick Steinhardt
2024-02-13 17:09       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fb210325394af2cc3389a780736d236bf9ac4f93.1707589943.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=levraiphilippeblain@gmail.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).