git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Wu <peter@lekensteyn.nl>
To: git@vger.kernel.org
Subject: [PATCH 1/3] completion: ignore git options for subcommand completion
Date: Wed, 28 Oct 2015 18:21:23 +0100	[thread overview]
Message-ID: <1446052885-11173-2-git-send-email-peter@lekensteyn.nl> (raw)
In-Reply-To: <1446052885-11173-1-git-send-email-peter@lekensteyn.nl>

Do not assume that the first option after "git" is a subcommand. This
fixes completion such as:

    # do not detect "push" as remote name
    git --git-dir=git/.git push origin <TAB>
    # do not overwrite "--git-dir=..." with the alias expansion
    git --git-dir=git/.git gerrit-diff <TAB>

Signed-off-by: Peter Wu <peter@lekensteyn.nl>
---
 contrib/completion/git-completion.bash | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 482ca84..bd9ef4c 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -531,8 +531,8 @@ __git_complete_revlist ()
 
 __git_complete_remote_or_refspec ()
 {
-	local cur_="$cur" cmd="${words[1]}"
-	local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
+	local cur_="$cur" cmd="${words[command_word_index]}"
+	local i c=$((command_word_index+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
 	if [ "$cmd" = "remote" ]; then
 		((c++))
 	fi
@@ -788,7 +788,7 @@ __git_aliased_command ()
 # __git_find_on_cmdline requires 1 argument
 __git_find_on_cmdline ()
 {
-	local word subcommand c=1
+	local word subcommand c=$command_word_index
 	while [ $c -lt $cword ]; do
 		word="${words[c]}"
 		for subcommand in $1; do
@@ -803,7 +803,7 @@ __git_find_on_cmdline ()
 
 __git_has_doubledash ()
 {
-	local c=1
+	local c=$command_word_index
 	while [ $c -lt $cword ]; do
 		if [ "--" = "${words[c]}" ]; then
 			return 0
@@ -826,8 +826,8 @@ __git_count_arguments ()
 {
 	local word i c=0
 
-	# Skip "git" (first argument)
-	for ((i=1; i < ${#words[@]}; i++)); do
+	# Skip "git" (first argument) and any options such as --git-dir.
+	for ((i=command_word_index; i < ${#words[@]}; i++)); do
 		word="${words[i]}"
 
 		case "$word" in
@@ -957,7 +957,7 @@ _git_bisect ()
 
 _git_branch ()
 {
-	local i c=1 only_local_ref="n" has_r="n"
+	local i c=$command_word_index only_local_ref="n" has_r="n"
 
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
@@ -992,7 +992,7 @@ _git_branch ()
 
 _git_bundle ()
 {
-	local cmd="${words[2]}"
+	local cmd="${words[command_word_index+1]}"
 	case "$cword" in
 	2)
 		__gitcomp "create list-heads verify unbundle"
@@ -1760,7 +1760,7 @@ _git_stage ()
 __git_config_get_set_variables ()
 {
 	local prevword word config_file= c=$cword
-	while [ $c -gt 1 ]; do
+	while [ $c -gt $command_word_index ]; do
 		word="${words[c]}"
 		case "$word" in
 		--system|--global|--local|--file=*)
@@ -2516,7 +2516,7 @@ _git_svn ()
 
 _git_tag ()
 {
-	local i c=1 f=0
+	local i c=$command_word_index f=0
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
 		case "$i" in
@@ -2562,7 +2562,7 @@ _git_whatchanged ()
 
 __git_main ()
 {
-	local i c=1 command __git_dir
+	local i c=1 command command_word_index __git_dir
 
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
@@ -2573,7 +2573,7 @@ __git_main ()
 		--help) command="help"; break ;;
 		-c|--work-tree|--namespace) ((c++)) ;;
 		-*) ;;
-		*) command="$i"; break ;;
+		*) command="$i"; command_word_index=$c; break ;;
 		esac
 		((c++))
 	done
@@ -2608,7 +2608,7 @@ __git_main ()
 
 	local expansion=$(__git_aliased_command "$command")
 	if [ -n "$expansion" ]; then
-		words[1]=$expansion
+		words[command_word_index]=$expansion
 		completion_func="_git_${expansion//-/_}"
 		declare -f $completion_func >/dev/null && $completion_func
 	fi
-- 
2.6.1

  reply	other threads:[~2015-10-28 17:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-28 17:21 [PATCH 0/3] bash-completion fixes for global git options handling Peter Wu
2015-10-28 17:21 ` Peter Wu [this message]
2015-10-28 17:21 ` [PATCH 2/3] completion: pass --git-dir to more commands Peter Wu
2015-10-28 17:21 ` [PATCH 3/3] completion: handle git -C option Peter Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1446052885-11173-2-git-send-email-peter@lekensteyn.nl \
    --to=peter@lekensteyn.nl \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).