completion: add --option completion for most builtin commands

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>

Nguyễn Thái Ngọc Duy committed Mar 24, 2018 at 21:35 UTC 9f642a71699dd10770dcf0e66d581b53ef244b14
2 files changed +34
contrib/completion/git-completion.bash
+28
@@ -3035,12 +3035,40 @@ _git_worktree ()
3035 fi
3036 }
3037
3038 +__git_complete_common () {
3039 + local command="$1"
3040 +
3041 + case "$cur" in
3042 + --*)
3043 + __gitcomp_builtin "$command"
3044 + ;;
3045 + esac
3046 +}
3047 +
3048 +__git_cmds_with_parseopt_helper=
3049 +__git_support_parseopt_helper () {
3050 + test -n "$__git_cmds_with_parseopt_helper" ||
3051 + __git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
3052 +
3053 + case " $__git_cmds_with_parseopt_helper " in
3054 + *" $1 "*)
3055 + return 0
3056 + ;;
3057 + *)
3058 + return 1
3059 + ;;
3060 + esac
3061 +}
3062 +
3063 __git_complete_command () {
3064 local command="$1"
3065 local completion_func="_git_${command//-/_}"
3066 if declare -f $completion_func >/dev/null 2>/dev/null; then
3067 $completion_func
3068 return 0
3069 + elif __git_support_parseopt_helper "$command"; then
3070 + __git_complete_common "$command"
3071 + return 0
3072 else
3073 return 1
3074 fi
t/t9902-completion.sh
+6
@@ -1454,6 +1454,12 @@ test_expect_success 'completion used <cmd> completion for alias: !f() { : git <c
1454 EOF
1455 '
1456
1457 +test_expect_success 'completion without explicit _git_xxx function' '
1458 + test_completion "git version --" <<-\EOF
1459 + --build-options Z
1460 + EOF
1461 +'
1462 +
1463 test_expect_failure 'complete with tilde expansion' '
1464 git init tmp && cd tmp &&
1465 test_when_finished "cd .. && rm -rf tmp" &&