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 <levraiphilippeblain@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Philippe Blain committed Feb 10, 2024 at 18:32 UTC 6e32f718ffa2e1358f3c34dafb9ed0dc8e479781
2 files changed +22 -51
contrib/completion/git-completion.bash
+21 -50
@@ -2596,6 +2596,13 @@ __git_compute_config_vars ()
2596 __git_config_vars="$(git help --config-for-completion)"
2597 }
2598
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 +
2606 __git_compute_first_level_config_vars_for_section ()
2607 {
2608 local section="$1"
@@ -2605,6 +2612,15 @@ __git_compute_first_level_config_vars_for_section ()
2612 printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
2613 }
2614
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 +
2624 __git_config_sections=
2625 __git_compute_config_sections ()
2626 {
@@ -2749,10 +2765,13 @@ __git_complete_config_variable_name ()
2765 done
2766
2767 case "$cur_" in
2752 - branch.*.*)
2768 + branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2769 local pfx="${cur_%.*}."
2770 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"
2775 return
2776 ;;
2777 branch.*)
@@ -2765,33 +2784,6 @@ __git_complete_config_variable_name ()
2784 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2785 return
2786 ;;
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 - ;;
2787 pager.*)
2788 local pfx="${cur_%.*}."
2789 cur_="${cur_#*.}"
@@ -2799,15 +2791,6 @@ __git_complete_config_variable_name ()
2791 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2792 return
2793 ;;
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 - ;;
2794 remote.*)
2795 local pfx="${cur_%.*}."
2796 cur_="${cur_#*.}"
@@ -2818,12 +2801,6 @@ __git_complete_config_variable_name ()
2801 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2802 return
2803 ;;
2821 - submodule.*.*)
2822 - local pfx="${cur_%.*}."
2823 - cur_="${cur_##*.}"
2824 - __gitcomp "url update branch fetchRecurseSubmodules ignore active" "$pfx" "$cur_" "$sfx"
2825 - return
2826 - ;;
2804 submodule.*)
2805 local pfx="${cur_%.*}."
2806 cur_="${cur_#*.}"
@@ -2834,12 +2811,6 @@ __git_complete_config_variable_name ()
2811 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2812 return
2813 ;;
2837 - url.*.*)
2838 - local pfx="${cur_%.*}."
2839 - cur_="${cur_##*.}"
2840 - __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2841 - return
2842 - ;;
2814 *.*)
2815 __git_compute_config_vars
2816 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
t/t9902-completion.sh
+1 -1
@@ -2601,7 +2601,7 @@ test_expect_success 'git config - variable name - submodule and __git_compute_fi
2601 EOF
2602 '
2603
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' '
2605 test_completion "git config submodule.sub." <<-\EOF
2606 submodule.sub.url Z
2607 submodule.sub.update Z