Skip to content

Commit 9f642a7

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 48e1c69 commit 9f642a7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,12 +3035,40 @@ _git_worktree ()
30353035
fi
30363036
}
30373037

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+
30383063
__git_complete_command () {
30393064
local command="$1"
30403065
local completion_func="_git_${command//-/_}"
30413066
if declare -f $completion_func >/dev/null 2>/dev/null; then
30423067
$completion_func
30433068
return 0
3069+
elif __git_support_parseopt_helper "$command"; then
3070+
__git_complete_common "$command"
3071+
return 0
30443072
else
30453073
return 1
30463074
fi

t/t9902-completion.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,12 @@ test_expect_success 'completion used <cmd> completion for alias: !f() { : git <c
14541454
EOF
14551455
'
14561456

1457+
test_expect_success 'completion without explicit _git_xxx function' '
1458+
test_completion "git version --" <<-\EOF
1459+
--build-options Z
1460+
EOF
1461+
'
1462+
14571463
test_expect_failure 'complete with tilde expansion' '
14581464
git init tmp && cd tmp &&
14591465
test_when_finished "cd .. && rm -rf tmp" &&

0 commit comments

Comments
 (0)