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

[PATCH 09/12] completion: calculate argument position properly

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 8, 2012, 03:07 UTC
Message-ID
<1333854479-23260-10-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1333854479-23260-1-git-send-email-felipe.contreras@gmail.com>

Positions are currently hard-coded, which means completions in the form of 'git --foo bar' fail, because positions have been shifted.

This fixes the issue that was spotted in the mailing list regarding certain aliases[1].

[1] http://thread.gmane.org/gmane.comp.version-control.git/185184
Cc: SZEDER Gábor <szeder@ira.uka.de>
Cc: Scott Bronson <bronson@rinspin.com>
Cc: Nathan Broadbent <nathan.f77@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash |   35 +++++++++++++++++++++++---------
 t/t9902-completion.sh                  |   21 +++++++++++++++++++
 2 files changed, 46 insertions(+), 10 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c26f96c..60ea224 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -719,10 +719,18 @@ __git_complete_revlist ()
 	__git_complete_revlist_file
 }
 
+__git_get_pos ()
+{
+	echo $(( t = cmd_pos - 2 + $1 ))
+}
+
 __git_complete_remote_or_refspec ()
 {
 	local cur_="$cur"
-	local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
+	local i c remote="" pfx="" lhs=1 no_complete_refspec=0
+
+	c=$(__git_get_pos 2)
+
 	if [ "$cmd" = "remote" ]; then
 		((c++))
 	fi
@@ -974,7 +982,8 @@ __git_aliased_command ()
 # __git_find_on_cmdline requires 1 argument
 __git_find_on_cmdline ()
 {
-	local word subcommand c=1
+	local word subcommand c
+	c=$(__git_get_pos 1)
 	while [ $c -lt $cword ]; do
 		word="${words[c]}"
 		for subcommand in $1; do
@@ -989,7 +998,8 @@ __git_find_on_cmdline ()
 
 __git_has_doubledash ()
 {
-	local c=1
+	local c
+	c=$(__git_get_pos 1)
 	while [ $c -lt $cword ]; do
 		if [ "--" = "${words[c]}" ]; then
 			return 0
@@ -1109,8 +1119,8 @@ _git_bisect ()
 
 _git_branch ()
 {
-	local i c=1 only_local_ref="n" has_r="n"
-
+	local i c only_local_ref="n" has_r="n"
+	c=$(__git_get_pos 1)
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
 		case "$i" in
@@ -1142,8 +1152,9 @@ _git_bundle ()
 {
 	local subcommands='create list-heads verify unbundle'
 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
-
-	case "$cword" in
+	local r
+	(( r = cword - cmd_pos + 2 ))
+	case "$r" in
 	2)
 		__gitcomp "$subcommands"
 		;;
@@ -1427,6 +1438,7 @@ __git_match_ctag() {
 _git_grep ()
 {
 	__git_has_doubledash && return
+	local p=$(__git_get_pos 2)
 
 	case "$cur" in
 	--*)
@@ -1447,7 +1459,7 @@ _git_grep ()
 	esac
 
 	case "$cword,$prev" in
-	2,*|*,-*)
+	$p,*|*,-*)
 		if test -r tags; then
 			__gitcomp_nl "$(__git_match_ctag "$cur" tags)"
 			return
@@ -2562,7 +2574,8 @@ _git_svn ()
 
 _git_tag ()
 {
-	local i c=1 f=0
+	local i c f=0
+	c=$(__git_get_pos 1)
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
 		case "$i" in
@@ -2601,7 +2614,7 @@ _git_whatchanged ()
 
 _git ()
 {
-	local i c=1 cmd __git_dir
+	local i c=1 cmd cmd_pos __git_dir
 
 	if [[ -n ${ZSH_VERSION-} ]]; then
 		emulate -L bash
@@ -2656,6 +2669,8 @@ _git ()
 		return
 	fi
 
+	(( cmd_pos = c + 1 ))
+
 	local completion_func="_git_${cmd//-/_}"
 	declare -f $completion_func >/dev/null && $completion_func && return
 
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index f1b660f..b99fb88 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -284,4 +284,25 @@ test_expect_success 'other' '
 	test_completion "git tag -d v" "v0.1 "
 '
 
+test_expect_success 'global options extra checks' '
+	test_completion "git --no-pager fetch o" "origin " &&
+	test_completion "git --no-pager fetch origin m" "master:master " &&
+	test_completion "git --no-pager pull o" "origin " &&
+	test_completion "git --no-pager pull origin m" "master " &&
+	test_completion "git --no-pager push o" "origin " &&
+	test_completion "git --no-pager push origin m" "master " &&
+	test_completion "git --no-pager bisect st" "start " &&
+	test_completion "git --no-pager add -- foo" "" &&
+	test_completion "git --no-pager config --file=foo --get c" "color.ui " &&
+	cat >expected <<-\EOF &&
+	origin/HEAD 
+	origin/master 
+	EOF
+	test_completion "git --no-pager branch -r o" &&
+	test_completion "git --no-pager bundle cr" "create " &&
+	test_completion "git --no-pager grep f" "foobar " &&
+	test_completion "git --no-pager notes --ref m" "master " &&
+	test_completion "git --no-pager tag -d v" "v0.1 "
+'
+
 test_done
-- 
1.7.10.3.g5a738d
Previous: Felipe ContrerasNext: Felipe Contreras
Message 42 of 48 in “Bash completion rework”
  1. 00/12 Bash completion reworkFelipe Contreras, Apr 8, 2012
  2. 01/12 tests: add initial bash completion testsFelipe Contreras, Apr 8, 2012
  3. Junio C HamanoApr 8, 2012
  4. Jeff KingApr 8, 2012
  5. Junio C HamanoApr 8, 2012
  6. Jeff KingApr 8, 2012
  7. Jeff KingApr 8, 2012
  8. Andreas SchwabApr 8, 2012
  9. Jeff KingApr 8, 2012
  10. Felipe ContrerasApr 11, 2012
  11. Junio C HamanoApr 11, 2012
  12. Jeff KingApr 8, 2012
  13. Jonathan NiederApr 8, 2012
  14. Jeff KingApr 8, 2012
  15. Junio C HamanoApr 9, 2012
  16. John KeepingApr 8, 2012
  17. 02/12 completion: simplify __gitcompFelipe Contreras, Apr 8, 2012
  18. SZEDER GáborApr 8, 2012
  19. Felipe ContrerasApr 8, 2012
  20. Jonathan NiederApr 8, 2012
  21. Felipe ContrerasApr 8, 2012
  22. Jonathan NiederApr 8, 2012
  23. Felipe ContrerasApr 8, 2012
  24. Johannes SixtApr 9, 2012
  25. Felipe ContrerasApr 9, 2012
  26. Felipe ContrerasApr 8, 2012
  27. Jonathan NiederApr 8, 2012
  28. Junio C HamanoApr 9, 2012
  29. 03/12 completion: simplify __gitcomp_1Felipe Contreras, Apr 8, 2012
  30. 04/12 completion: trivial simplificationFelipe Contreras, Apr 8, 2012
  31. 05/12 completion: add missing global optionsFelipe Contreras, Apr 8, 2012
  32. John KeepingApr 8, 2012
  33. SZEDER GáborApr 8, 2012
  34. 06/12 tests: add more bash completion testsFelipe Contreras, Apr 8, 2012
  35. 07/12 completion: simplify command stuffFelipe Contreras, Apr 8, 2012
  36. SZEDER GáborApr 11, 2012
  37. Felipe ContrerasApr 11, 2012
  38. SZEDER GáborApr 11, 2012
  39. Felipe ContrerasApr 11, 2012
  40. Felipe ContrerasApr 12, 2012
  41. 08/12 completion: simplify _git_bundleFelipe Contreras, Apr 8, 2012
  42. 09/12 completion: calculate argument position properlyFelipe Contreras, Apr 8, 2012
  43. 10/12 completion: add new git_complete helperFelipe Contreras, Apr 8, 2012
  44. SZEDER GáborApr 11, 2012
  45. Felipe ContrerasApr 11, 2012
  46. 11/12 test: add tests for aliases in bash completionFelipe Contreras, Apr 8, 2012
  47. Felipe ContrerasApr 8, 2012
  48. 12/12 completion: rename _git and _gitkFelipe Contreras, Apr 8, 2012

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.