From d447fe2bfe68840cb127fbbc9fc3a53faab2124b Mon Sep 17 00:00:00 2001 From: SZEDER Gábor Date: Thu, 19 Dec 2019 16:09:17 +0100 Subject: completion: clean up the __git_find_on_cmdline() helper function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The __git_find_on_cmdline() helper function started its life as __git_find_subcommand() [1], but it served a more general purpose than looking for subcommands, so later it was renamed accordingly [2]. However, that rename didn't touch the body of the function, and left the $subcommand local variable behind, still reminiscent of the function's original purpose. Let's clean up the names of __git_find_on_cmdline()'s local variables and get rid of that $subcommand variable name. While at it, add a short comment describing the function's purpose. [1] 3ff1320d4b (bash: refactor searching for subcommands on the command line, 2008-03-10), [2] 918c03c2a7 (bash: rename __git_find_subcommand() to __git_find_on_cmdline(), 2009-09-15) Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'contrib/completion/git-completion.bash') diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 67705da641..84ce84d65c 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -1070,14 +1070,17 @@ __git_aliased_command () } # __git_find_on_cmdline requires 1 argument +# Check whether one of the given words is present on the command line, +# and print the first word found. __git_find_on_cmdline () { - local word subcommand c=1 + local word c=1 + local wordlist="$1" + while [ $c -lt $cword ]; do - word="${words[c]}" - for subcommand in $1; do - if [ "$subcommand" = "$word" ]; then - echo "$subcommand" + for word in $wordlist; do + if [ "$word" = "${words[c]}" ]; then + echo "$word" return fi done -- cgit v1.3 From 367efd54b34883889068255c370a4b9f079cec2d Mon Sep 17 00:00:00 2001 From: SZEDER Gábor Date: Thu, 19 Dec 2019 16:09:18 +0100 Subject: completion: return the index of found word from __git_find_on_cmdline() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using the __git_find_on_cmdline() helper function so far we've only been interested in which one of a set of words appear on the command line. To complete options for some of 'git worktree's subcommands in the following patches we'll need not only that, but the index of that word on the command line as well. Extend __git_find_on_cmdline() to optionally show the index of the found word on the command line (IOW in the $words array) when the '--show-idx' option is given. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 20 +++++++++++++++++--- t/t9902-completion.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) (limited to 'contrib/completion/git-completion.bash') diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 84ce84d65c..340d8defce 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -1069,18 +1069,32 @@ __git_aliased_command () done } -# __git_find_on_cmdline requires 1 argument # Check whether one of the given words is present on the command line, # and print the first word found. +# +# Usage: __git_find_on_cmdline [