git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] completion: ignore git options for subcommand completion

From
Peter Wu <peter@lekensteyn.nl>
Date
Oct 28, 2015, 17:21 UTC
Message-ID
<1446052885-11173-2-git-send-email-peter@lekensteyn.nl>
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
Previous: Peter WuNext: Peter Wu
Message 2 of 4 in “bash-completion fixes for global git options handling”
  1. 0/3 bash-completion fixes for global git options handlingPeter Wu, Oct 28, 2015
  2. 1/3 completion: ignore git options for subcommand completionPeter Wu, Oct 28, 2015
  3. 2/3 completion: pass --git-dir to more commandsPeter Wu, Oct 28, 2015
  4. 3/3 completion: handle git -C optionPeter Wu, Oct 28, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.