Skip to content

Commit 6e32f71

Browse files
phil-blaingitster
authored andcommitted
completion: add and use __git_compute_second_level_config_vars_for_section
In a previous commit we removed some hardcoded config variable names from function __git_complete_config_variable_name in the completion script by introducing a new function, __git_compute_first_level_config_vars_for_section. The remaining hardcoded config variables are "second level" configuration variables, meaning 'branch.<name>.upstream', 'remote.<name>.url', etc. where <name> is a user-defined name. Making use of the new existing --config flag to 'git help', add a new function, __git_compute_second_level_config_vars_for_section. This function takes as argument a config section name and computes the corresponding second-level config variables, i.e. those that contain a '<' which indicates the start of a placeholder. Note that as in __git_compute_first_level_config_vars_for_section added previsouly, we use indirect expansion instead of associative arrays to stay compatible with Bash 3 on which macOS is stuck for licensing reasons. As explained in the previous commit, we use the existing pattern in the completion script of using global variables to cache the list of variables for each section. Use this new function and the variables it defines in __git_complete_config_variable_name to remove hardcoded config variables, and add a test to verify the new function. Use a single 'case' for all sections with second-level variables names, since the code for each of them is now exactly the same. Adjust the name of a test added in a previous commit to reflect that it now tests the added function. Signed-off-by: Philippe Blain <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e0ee40 commit 6e32f71

File tree

2 files changed

+22
-51
lines changed

2 files changed

+22
-51
lines changed

contrib/completion/git-completion.bash

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

2599+
__git_config_vars_all=
2600+
__git_compute_config_vars_all ()
2601+
{
2602+
test -n "$__git_config_vars_all" ||
2603+
__git_config_vars_all="$(git --no-pager help --config)"
2604+
}
2605+
25992606
__git_compute_first_level_config_vars_for_section ()
26002607
{
26012608
local section="$1"
@@ -2605,6 +2612,15 @@ __git_compute_first_level_config_vars_for_section ()
26052612
printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
26062613
}
26072614

2615+
__git_compute_second_level_config_vars_for_section ()
2616+
{
2617+
local section="$1"
2618+
__git_compute_config_vars_all
2619+
local this_section="__git_second_level_config_vars_for_section_${section}"
2620+
test -n "${!this_section}" ||
2621+
printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
2622+
}
2623+
26082624
__git_config_sections=
26092625
__git_compute_config_sections ()
26102626
{
@@ -2749,10 +2765,13 @@ __git_complete_config_variable_name ()
27492765
done
27502766

27512767
case "$cur_" in
2752-
branch.*.*)
2768+
branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
27532769
local pfx="${cur_%.*}."
27542770
cur_="${cur_##*.}"
2755-
__gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2771+
local section="${pfx%.*.}"
2772+
__git_compute_second_level_config_vars_for_section "${section}"
2773+
local this_section="__git_second_level_config_vars_for_section_${section}"
2774+
__gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
27562775
return
27572776
;;
27582777
branch.*)
@@ -2765,49 +2784,13 @@ __git_complete_config_variable_name ()
27652784
__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
27662785
return
27672786
;;
2768-
guitool.*.*)
2769-
local pfx="${cur_%.*}."
2770-
cur_="${cur_##*.}"
2771-
__gitcomp "
2772-
argPrompt cmd confirm needsFile noConsole noRescan
2773-
prompt revPrompt revUnmerged title
2774-
" "$pfx" "$cur_" "$sfx"
2775-
return
2776-
;;
2777-
difftool.*.*)
2778-
local pfx="${cur_%.*}."
2779-
cur_="${cur_##*.}"
2780-
__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2781-
return
2782-
;;
2783-
man.*.*)
2784-
local pfx="${cur_%.*}."
2785-
cur_="${cur_##*.}"
2786-
__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2787-
return
2788-
;;
2789-
mergetool.*.*)
2790-
local pfx="${cur_%.*}."
2791-
cur_="${cur_##*.}"
2792-
__gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2793-
return
2794-
;;
27952787
pager.*)
27962788
local pfx="${cur_%.*}."
27972789
cur_="${cur_#*.}"
27982790
__git_compute_all_commands
27992791
__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
28002792
return
28012793
;;
2802-
remote.*.*)
2803-
local pfx="${cur_%.*}."
2804-
cur_="${cur_##*.}"
2805-
__gitcomp "
2806-
url proxy fetch push mirror skipDefaultUpdate
2807-
receivepack uploadpack tagOpt pushurl
2808-
" "$pfx" "$cur_" "$sfx"
2809-
return
2810-
;;
28112794
remote.*)
28122795
local pfx="${cur_%.*}."
28132796
cur_="${cur_#*.}"
@@ -2818,12 +2801,6 @@ __git_complete_config_variable_name ()
28182801
__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
28192802
return
28202803
;;
2821-
submodule.*.*)
2822-
local pfx="${cur_%.*}."
2823-
cur_="${cur_##*.}"
2824-
__gitcomp "url update branch fetchRecurseSubmodules ignore active" "$pfx" "$cur_" "$sfx"
2825-
return
2826-
;;
28272804
submodule.*)
28282805
local pfx="${cur_%.*}."
28292806
cur_="${cur_#*.}"
@@ -2834,12 +2811,6 @@ __git_complete_config_variable_name ()
28342811
__gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
28352812
return
28362813
;;
2837-
url.*.*)
2838-
local pfx="${cur_%.*}."
2839-
cur_="${cur_##*.}"
2840-
__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2841-
return
2842-
;;
28432814
*.*)
28442815
__git_compute_config_vars
28452816
__gitcomp "$__git_config_vars" "" "$cur_" "$sfx"

t/t9902-completion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ test_expect_success 'git config - variable name - submodule and __git_compute_fi
26012601
EOF
26022602
'
26032603

2604-
test_expect_success 'git config - variable name - submodule names' '
2604+
test_expect_success 'git config - variable name - __git_compute_second_level_config_vars_for_section' '
26052605
test_completion "git config submodule.sub." <<-\EOF
26062606
submodule.sub.url Z
26072607
submodule.sub.update Z

0 commit comments

Comments
 (0)