completion: add proper public __git_complete

When __git_complete was introduced, it was meant to be temporarily, while a proper guideline for public shell functions was established (tentatively _GIT_complete), but since that never happened, people in the wild started to use __git_complete, even though it was marked as not public. Eight years is more than enough wait, let's mark this function as public, and make it a bit more user-friendly. So that instead of doing: __git_complete gk __gitk_main The user can do: __git_complete gk gitk And instead of: __git_complete gf _git_fetch Do: __git_complete gf git_fetch Backwards compatibility is maintained. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Dec 30, 2020 at 17:29 UTC 5a067ba9d04eebc92ad77f101b785219238f4f1e
2 files changed +46 -8
contrib/completion/git-completion.bash
+33 -7
@@ -29,6 +29,15 @@
29 # tell the completion to use commit completion. This also works with aliases
30 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
31 #
32 +# If you have a command that is not part of git, but you would still
33 +# like completion, you can use __git_complete:
34 +#
35 +# __git_complete gl git_log
36 +#
37 +# Or if it's a main command (i.e. git or gitk):
38 +#
39 +# __git_complete gk gitk
40 +#
41 # Compatible with bash 3.2.57.
42 #
43 # You can set the following environment variables to influence the behavior of
@@ -3497,10 +3506,7 @@ __git_func_wrap ()
3506 $1
3507 }
3508
3500 -# Setup completion for certain functions defined above by setting common
3501 -# variables and workarounds.
3502 -# This is NOT a public function; use at your own risk.
3503 -__git_complete ()
3509 +___git_complete ()
3510 {
3511 local wrapper="__git_wrap${2}"
3512 eval "$wrapper () { __git_func_wrap $2 ; }"
@@ -3508,13 +3514,33 @@ __git_complete ()
3514 || complete -o default -o nospace -F $wrapper $1
3515 }
3516
3511 -__git_complete git __git_main
3512 -__git_complete gitk __gitk_main
3517 +# Setup the completion for git commands
3518 +# 1: command or alias
3519 +# 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3520 +__git_complete ()
3521 +{
3522 + local func
3523 +
3524 + if __git_have_func $2; then
3525 + func=$2
3526 + elif __git_have_func __$2_main; then
3527 + func=__$2_main
3528 + elif __git_have_func _$2; then
3529 + func=_$2
3530 + else
3531 + echo "ERROR: could not find function '$2'" 1>&2
3532 + return 1
3533 + fi
3534 + ___git_complete $1 $func
3535 +}
3536 +
3537 +___git_complete git __git_main
3538 +___git_complete gitk __gitk_main
3539
3540 # The following are necessary only for Cygwin, and only are needed
3541 # when the user has tab-completed the executable name and consequently
3542 # included the '.exe' suffix.
3543 #
3544 if [ "$OSTYPE" = cygwin ]; then
3519 - __git_complete git.exe __git_main
3545 + ___git_complete git.exe __git_main
3546 fi
t/t9902-completion.sh
+13 -1
@@ -2382,10 +2382,22 @@ test_expect_success 'sourcing the completion script clears cached --options' '
2382
2383 test_expect_success '__git_complete' '
2384 unset -f __git_wrap__git_main &&
2385 +
2386 __git_complete foo __git_main &&
2387 __git_have_func __git_wrap__git_main &&
2388 + unset -f __git_wrap__git_main &&
2389 +
2390 __git_complete gf _git_fetch &&
2388 - __git_have_func __git_wrap_git_fetch
2391 + __git_have_func __git_wrap_git_fetch &&
2392 +
2393 + __git_complete foo git &&
2394 + __git_have_func __git_wrap__git_main &&
2395 + unset -f __git_wrap__git_main &&
2396 +
2397 + __git_complete gd git_diff &&
2398 + __git_have_func __git_wrap_git_diff &&
2399 +
2400 + test_must_fail __git_complete ga missing
2401 '
2402
2403 test_done