[PATCH 1/4] completion: introduce __gitcomp_subcommand
- From
Rubén Justo <rjusto@gmail.com>
- Date
- Jan 26, 2024, 12:51 UTC
- Message-ID
- <8c902c53-815d-43c2-8ba9-8144d8884804@gmail.com>
- In-Reply-To
- <98daf977-dbad-4d3b-a293-6a769895088f@gmail.com>
Let's have a function to complete "subcommands" only in the correct position (right after the command), in commands that follow the syntax:
git <command> <subcommand>
Signed-off-by: Rubén Justo <rjusto@gmail.com> --- contrib/completion/git-completion.bash | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 8c40ade494..916e137021 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -554,6 +554,27 @@ __gitcomp_file () true } +# Completion for subcommands in commands that follow the syntax: +# +# git <command> <subcommand> +# +# 1: List of possible completion words. +# Returns false if the current word is not a possible subcommand +# (possitioned after the command), or if no option is found in +# the list provided. +__gitcomp_subcommand () +{ + local subcommands="$1" + + if [ $cword -eq $(($__git_cmd_idx + 1)) ]; then + __gitcomp "$subcommands" + + test -n "$COMPREPLY" + else + false + fi +} + # Execute 'git ls-files', unless the --committable option is specified, in # which case it runs 'git diff-index' to find out the files that can be # committed. It return paths relative to the directory specified in the first
-- 2.43.0