Skip to content

Commit 3fec482

Browse files
rjustogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c689c38 commit 3fec482

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,26 @@ __gitcomp_file ()
554554
true
555555
}
556556

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+
557577
# Execute 'git ls-files', unless the --committable option is specified, in
558578
# which case it runs 'git diff-index' to find out the files that can be
559579
# committed. It return paths relative to the directory specified in the first

0 commit comments

Comments
 (0)