completion: complete config variables and values for 'git clone --config='

Completing configuration sections and variable names for the stuck argument of 'git clone --config=<TAB>' requires a bit of extra care compared to doing the same for the unstuck argument of 'git clone --config <TAB>', because we have to deal with that '--config=' being part of the current word to be completed. Add an option to the __git_complete_config_variable_name_and_value() and in turn to the __git_complete_config_variable_name() helper functions to specify the current section/variable name to be completed, so they can be used even when completing the stuck argument of '--config='. __git_complete_config_variable_value() already has such an option, and thus no further changes were necessary to complete possible values after 'git clone --config=section.name=<TAB>'. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Aug 13, 2019 at 14:26 UTC 5af9d5f6c8530f873ab6a1fce6071c703be2e8d6
2 files changed +70 -17
contrib/completion/git-completion.bash
+49 -17
@@ -1406,6 +1406,11 @@ _git_clone ()
1406 ;;
1407 esac
1408 case "$cur" in
1409 + --config=*)
1410 + __git_complete_config_variable_name_and_value \
1411 + --cur="${cur##--config=}"
1412 + return
1413 + ;;
1414 --*)
1415 __gitcomp_builtin clone
1416 return
@@ -2352,35 +2357,41 @@ __git_complete_config_variable_value ()
2357 # Completes configuration sections, subsections, variable names.
2358 #
2359 # Usage: __git_complete_config_variable_name [<option>]...
2360 +# --cur=<word>: The current configuration section/variable name to be
2361 +# completed. Defaults to the current word to be completed.
2362 # --sfx=<suffix>: A suffix to be appended to each fully completed
2363 # configuration variable name (but not to sections or
2364 # subsections) instead of the default space.
2365 __git_complete_config_variable_name ()
2366 {
2360 - local sfx
2367 + local cur_="$cur" sfx
2368
2369 while test $# != 0; do
2370 case "$1" in
2371 + --cur=*) cur_="${1##--cur=}" ;;
2372 --sfx=*) sfx="${1##--sfx=}" ;;
2373 *) return 1 ;;
2374 esac
2375 shift
2376 done
2377
2370 - case "$cur" in
2378 + case "$cur_" in
2379 branch.*.*)
2372 - local pfx="${cur%.*}." cur_="${cur##*.}"
2380 + local pfx="${cur_%.*}."
2381 + cur_="${cur_##*.}"
2382 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2383 return
2384 ;;
2385 branch.*)
2377 - local pfx="${cur%.*}." cur_="${cur#*.}"
2386 + local pfx="${cur%.*}."
2387 + cur_="${cur#*.}"
2388 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2389 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
2390 return
2391 ;;
2392 guitool.*.*)
2383 - local pfx="${cur%.*}." cur_="${cur##*.}"
2393 + local pfx="${cur_%.*}."
2394 + cur_="${cur_##*.}"
2395 __gitcomp "
2396 argPrompt cmd confirm needsFile noConsole noRescan
2397 prompt revPrompt revUnmerged title
@@ -2388,28 +2399,33 @@ __git_complete_config_variable_name ()
2399 return
2400 ;;
2401 difftool.*.*)
2391 - local pfx="${cur%.*}." cur_="${cur##*.}"
2402 + local pfx="${cur_%.*}."
2403 + cur_="${cur_##*.}"
2404 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2405 return
2406 ;;
2407 man.*.*)
2396 - local pfx="${cur%.*}." cur_="${cur##*.}"
2408 + local pfx="${cur_%.*}."
2409 + cur_="${cur_##*.}"
2410 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2411 return
2412 ;;
2413 mergetool.*.*)
2401 - local pfx="${cur%.*}." cur_="${cur##*.}"
2414 + local pfx="${cur_%.*}."
2415 + cur_="${cur_##*.}"
2416 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2417 return
2418 ;;
2419 pager.*)
2406 - local pfx="${cur%.*}." cur_="${cur#*.}"
2420 + local pfx="${cur_%.*}."
2421 + cur_="${cur_#*.}"
2422 __git_compute_all_commands
2423 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
2424 return
2425 ;;
2426 remote.*.*)
2412 - local pfx="${cur%.*}." cur_="${cur##*.}"
2427 + local pfx="${cur_%.*}."
2428 + cur_="${cur_##*.}"
2429 __gitcomp "
2430 url proxy fetch push mirror skipDefaultUpdate
2431 receivepack uploadpack tagOpt pushurl
@@ -2417,19 +2433,21 @@ __git_complete_config_variable_name ()
2433 return
2434 ;;
2435 remote.*)
2420 - local pfx="${cur%.*}." cur_="${cur#*.}"
2436 + local pfx="${cur_%.*}."
2437 + cur_="${cur_#*.}"
2438 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2439 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
2440 return
2441 ;;
2442 url.*.*)
2426 - local pfx="${cur%.*}." cur_="${cur##*.}"
2443 + local pfx="${cur_%.*}."
2444 + cur_="${cur_##*.}"
2445 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2446 return
2447 ;;
2448 *.*)
2449 __git_compute_config_vars
2432 - __gitcomp "$__git_config_vars" "" "$cur" "$sfx"
2450 + __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2451 ;;
2452 *)
2453 __git_compute_config_vars
@@ -2441,22 +2459,36 @@ __git_complete_config_variable_name ()
2459 for (s in sections)
2460 print s "."
2461 }
2444 - ')"
2462 + ')" "" "$cur_"
2463 ;;
2464 esac
2465 }
2466
2467 # Completes '='-separated configuration sections/variable names and values
2468 # for 'git -c section.name=value'.
2469 +#
2470 +# Usage: __git_complete_config_variable_name_and_value [<option>]...
2471 +# --cur=<word>: The current configuration section/variable name/value to be
2472 +# completed. Defaults to the current word to be completed.
2473 __git_complete_config_variable_name_and_value ()
2474 {
2453 - case "$cur" in
2475 + local cur_="$cur"
2476 +
2477 + while test $# != 0; do
2478 + case "$1" in
2479 + --cur=*) cur_="${1##--cur=}" ;;
2480 + *) return 1 ;;
2481 + esac
2482 + shift
2483 + done
2484 +
2485 + case "$cur_" in
2486 *=*)
2487 __git_complete_config_variable_value \
2456 - --varname="${cur%%=*}" --cur="${cur#*=}"
2488 + --varname="${cur_%%=*}" --cur="${cur_#*=}"
2489 ;;
2490 *)
2459 - __git_complete_config_variable_name --sfx='='
2491 + __git_complete_config_variable_name --cur="$cur_" --sfx='='
2492 ;;
2493 esac
2494 }
t/t9902-completion.sh
+21
@@ -1740,6 +1740,27 @@ test_expect_success 'git -c - value' '
1740 EOF
1741 '
1742
1743 +test_expect_success 'git clone --config= - section' '
1744 + test_completion "git clone --config=br" <<-\EOF
1745 + branch.Z
1746 + browser.Z
1747 + EOF
1748 +'
1749 +
1750 +test_expect_success 'git clone --config= - variable name' '
1751 + test_completion "git clone --config=log.d" <<-\EOF
1752 + log.date=Z
1753 + log.decorate=Z
1754 + EOF
1755 +'
1756 +
1757 +test_expect_success 'git clone --config= - value' '
1758 + test_completion "git clone --config=color.pager=" <<-\EOF
1759 + false Z
1760 + true Z
1761 + EOF
1762 +'
1763 +
1764 test_expect_success 'sourcing the completion script clears cached commands' '
1765 __git_compute_all_commands &&
1766 verbose test -n "$__git_all_commands" &&