Skip to content

Commit 1e0ee40

Browse files
phil-blaingitster
authored andcommitted
completion: add and use __git_compute_first_level_config_vars_for_section
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 eaa4e6e (Speed up bash completion loading, 2009-11-17), and the current approach to using and defining them via 'test -n' is explained in cf0ff02 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1d0cc6 commit 1e0ee40

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

contrib/completion/git-completion.bash

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,15 @@ __git_compute_config_vars ()
25962596
__git_config_vars="$(git help --config-for-completion)"
25972597
}
25982598

2599+
__git_compute_first_level_config_vars_for_section ()
2600+
{
2601+
local section="$1"
2602+
__git_compute_config_vars
2603+
local this_section="__git_first_level_config_vars_for_section_${section}"
2604+
test -n "${!this_section}" ||
2605+
printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
2606+
}
2607+
25992608
__git_config_sections=
26002609
__git_compute_config_sections ()
26012610
{
@@ -2749,8 +2758,11 @@ __git_complete_config_variable_name ()
27492758
branch.*)
27502759
local pfx="${cur_%.*}."
27512760
cur_="${cur_#*.}"
2761+
local section="${pfx%.}"
27522762
__gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2753-
__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx:- }"
2763+
__git_compute_first_level_config_vars_for_section "${section}"
2764+
local this_section="__git_first_level_config_vars_for_section_${section}"
2765+
__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
27542766
return
27552767
;;
27562768
guitool.*.*)
@@ -2799,8 +2811,11 @@ __git_complete_config_variable_name ()
27992811
remote.*)
28002812
local pfx="${cur_%.*}."
28012813
cur_="${cur_#*.}"
2814+
local section="${pfx%.}"
28022815
__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2803-
__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx:- }"
2816+
__git_compute_first_level_config_vars_for_section "${section}"
2817+
local this_section="__git_first_level_config_vars_for_section_${section}"
2818+
__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
28042819
return
28052820
;;
28062821
submodule.*.*)
@@ -2812,8 +2827,11 @@ __git_complete_config_variable_name ()
28122827
submodule.*)
28132828
local pfx="${cur_%.*}."
28142829
cur_="${cur_#*.}"
2830+
local section="${pfx%.}"
28152831
__gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2816-
__gitcomp_nl_append $'alternateErrorStrategy\nfetchJobs\nactive\nalternateLocation\nrecurse\npropagateBranches' "$pfx" "$cur_" "${sfx:- }"
2832+
__git_compute_first_level_config_vars_for_section "${section}"
2833+
local this_section="__git_first_level_config_vars_for_section_${section}"
2834+
__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
28172835
return
28182836
;;
28192837
url.*.*)

t/t9902-completion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,7 @@ test_expect_success 'setup for git config submodule tests' '
25892589
git submodule add ./sub
25902590
'
25912591

2592-
test_expect_success 'git config - variable name - submodule' '
2592+
test_expect_success 'git config - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
25932593
test_completion "git config submodule." <<-\EOF
25942594
submodule.active Z
25952595
submodule.alternateErrorStrategy Z

0 commit comments

Comments
 (0)