]> git.ipfire.org Git - thirdparty/git.git/commitdiff
completion: add --option completion for most builtin commands
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sat, 24 Mar 2018 20:35:22 +0000 (21:35 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sun, 25 Mar 2018 16:34:34 +0000 (09:34 -0700)
Many builtin commands use parseopt which can expose the option list
via --git-completion-helper but do not have explicit support in
git-completion.bash. This patch detects those commands and uses
__gitcomp_builtin for option completion.

This does not pollute the command name completion though. "git <tab>"
will show you the same set as before. This only kicks in when you type
the correct command name.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/completion/git-completion.bash
t/t9902-completion.sh

index 4aaec3cd40d767608ee170069b9fb6aecfd82f0c..f65cb5d1f949d8dfa538af5810120051404b5891 100644 (file)
@@ -3035,12 +3035,40 @@ _git_worktree ()
        fi
 }
 
+__git_complete_common () {
+       local command="$1"
+
+       case "$cur" in
+       --*)
+               __gitcomp_builtin "$command"
+               ;;
+       esac
+}
+
+__git_cmds_with_parseopt_helper=
+__git_support_parseopt_helper () {
+       test -n "$__git_cmds_with_parseopt_helper" ||
+               __git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
+
+       case " $__git_cmds_with_parseopt_helper " in
+       *" $1 "*)
+               return 0
+               ;;
+       *)
+               return 1
+               ;;
+       esac
+}
+
 __git_complete_command () {
        local command="$1"
        local completion_func="_git_${command//-/_}"
        if declare -f $completion_func >/dev/null 2>/dev/null; then
                $completion_func
                return 0
+       elif __git_support_parseopt_helper "$command"; then
+               __git_complete_common "$command"
+               return 0
        else
                return 1
        fi
index b7f5b1e632fb27a0448239361d6b4207be4b9908..1b34caa1e1a5e86cd2edccbb1559009b35022e10 100755 (executable)
@@ -1454,6 +1454,12 @@ test_expect_success 'completion used <cmd> completion for alias: !f() { : git <c
        EOF
 '
 
+test_expect_success 'completion without explicit _git_xxx function' '
+       test_completion "git version --" <<-\EOF
+       --build-options Z
+       EOF
+'
+
 test_expect_failure 'complete with tilde expansion' '
        git init tmp && cd tmp &&
        test_when_finished "cd .. && rm -rf tmp" &&