Skip to content

Commit 5a067ba

Browse files
felipecgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e02bdc commit 5a067ba

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

contrib/completion/git-completion.bash

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
# tell the completion to use commit completion. This also works with aliases
3030
# of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
3131
#
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+
#
3241
# Compatible with bash 3.2.57.
3342
#
3443
# You can set the following environment variables to influence the behavior of
@@ -3497,24 +3506,41 @@ __git_func_wrap ()
34973506
$1
34983507
}
34993508

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 ()
35043510
{
35053511
local wrapper="__git_wrap${2}"
35063512
eval "$wrapper () { __git_func_wrap $2 ; }"
35073513
complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
35083514
|| complete -o default -o nospace -F $wrapper $1
35093515
}
35103516

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
35133539

35143540
# The following are necessary only for Cygwin, and only are needed
35153541
# when the user has tab-completed the executable name and consequently
35163542
# included the '.exe' suffix.
35173543
#
35183544
if [ "$OSTYPE" = cygwin ]; then
3519-
__git_complete git.exe __git_main
3545+
___git_complete git.exe __git_main
35203546
fi

t/t9902-completion.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2382,10 +2382,22 @@ test_expect_success 'sourcing the completion script clears cached --options' '
23822382

23832383
test_expect_success '__git_complete' '
23842384
unset -f __git_wrap__git_main &&
2385+
23852386
__git_complete foo __git_main &&
23862387
__git_have_func __git_wrap__git_main &&
2388+
unset -f __git_wrap__git_main &&
2389+
23872390
__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
23892401
'
23902402

23912403
test_done

0 commit comments

Comments
 (0)