completion: introduce __git_find_subcommand

Let's have a function to get the current subcommand when completing commands that follow the syntax: git <command> <subcommand> As a convenience, let's allow an optional "default subcommand" to be returned if none is found. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Mar 2, 2024 at 16:51 UTC 3fec482b5f7f2f13c7465cf79062440a84233d14
1 file changed +20
contrib/completion/git-completion.bash
+20
@@ -554,6 +554,26 @@ __gitcomp_file ()
554 true
555 }
556
557 +# Find the current subcommand for commands that follow the syntax:
558 +#
559 +# git <command> <subcommand>
560 +#
561 +# 1: List of possible subcommands.
562 +# 2: Optional subcommand to return when none is found.
563 +__git_find_subcommand ()
564 +{
565 + local subcommand subcommands="$1" default_subcommand="$2"
566 +
567 + for subcommand in $subcommands; do
568 + if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
569 + echo $subcommand
570 + return
571 + fi
572 + done
573 +
574 + echo $default_subcommand
575 +}
576 +
577 # Execute 'git ls-files', unless the --committable option is specified, in
578 # which case it runs 'git diff-index' to find out the files that can be
579 # committed. It return paths relative to the directory specified in the first