# [PATCH v2 1/4] completion: bash: fix prefix detection in branch.*

5 messages from 2020-12-23 to 2020-12-23. Participants: Felipe Contreras.
Thread: https://gitlist.dev/t/54874

## Felipe Contreras, 2020-12-23 14:48

Subject: [PATCH v2 1/4] completion: bash: fix prefix detection in branch.*
Message-ID: <20201223144845.143039-2-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20201223144845.143039-2-felipe.contreras%40gmail.com
In-Reply-To: <20201223144845.143039-1-felipe.contreras@gmail.com>

```
Otherwise we are completely ignoring the --cur argument.

The issue can be tested with:

  git clone --config=branch.<tab>

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 463a3124da..10d225cc9c 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2633,8 +2633,8 @@ __git_complete_config_variable_name ()
 		return
 		;;
 	branch.*)
-		local pfx="${cur%.*}."
-		cur_="${cur#*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_#*.}"
 		__gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
 		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
 		return
-- 
2.30.0.rc1


```

## Felipe Contreras, 2020-12-23 14:48

Subject: [PATCH v2 0/4] completion: bash: a bunch of fixes
Message-ID: <20201223144845.143039-1-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20201223144845.143039-1-felipe.contreras%40gmail.com

```
These should be pretty obvious and straightforward.

Changes since v1:

 * Removed test for suffix in variable names

Felipe Contreras (4):
  completion: bash: fix prefix detection in branch.*
  completion: bash: fix for suboptions with value
  completion: bash: fix for multiple dash commands
  completion: bash: add correct suffix in variables

 contrib/completion/git-completion.bash | 14 +++++++-------
 t/t9902-completion.sh                  | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 7 deletions(-)

Range-diff:
1:  26bbb19677 = 1:  dd49b0e680 completion: bash: fix prefix detection in branch.*
3:  aad0bc6a0f = 2:  6ac184b9a4 completion: bash: fix for suboptions with value
4:  77a4b0b0d0 = 3:  960a692fda completion: bash: fix for multiple dash commands
2:  4bcc8a5fca ! 4:  a95c3edaf9 completion: bash: add correct suffix in variables
    @@ Commit message
     
           git config branch.autoSetupMe<tab>
     
    +    This fix only works for versions of bash greater than 4.0, before that
    +    "local sfx" creates an empty string, therefore the unset expansion
    +    doesn't work. The same happens in zsh.
    +
    +    Therefore we don't add the test for that for now.
    +
    +    The correct fix for all shells requires semantic changes in __gitcomp,
    +    but that can be done later.
    +
    +    Cc: SZEDER Gábor <szeder.dev@gmail.com>
         Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
     
      ## contrib/completion/git-completion.bash ##
    @@ contrib/completion/git-completion.bash: __git_complete_config_variable_name ()
      		return
      		;;
      	url.*.*)
    -
    - ## t/t9902-completion.sh ##
    -@@ t/t9902-completion.sh: test_expect_success 'git config - value' '
    - 	EOF
    - '
    - 
    -+test_expect_success 'git config - direct completions' '
    -+	test_completion "git config branch.autoSetup" <<-\EOF
    -+	branch.autoSetupMerge Z
    -+	branch.autoSetupRebase Z
    -+	EOF
    -+'
    -+
    - test_expect_success 'git -c - section' '
    - 	test_completion "git -c br" <<-\EOF
    - 	branch.Z
-- 
2.30.0.rc1


```

## Felipe Contreras, 2020-12-23 14:48

Subject: [PATCH v2 3/4] completion: bash: fix for multiple dash commands
Message-ID: <20201223144845.143039-4-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20201223144845.143039-4-felipe.contreras%40gmail.com
In-Reply-To: <20201223144845.143039-1-felipe.contreras@gmail.com>

```
Otherwise commands like 'for-each-ref' are not completed.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 10e69af9ed..64cf35ba04 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -412,7 +412,7 @@ __gitcomp_builtin ()
 	local incl="${2-}"
 	local excl="${3-}"
 
-	local var=__gitcomp_builtin_"${cmd/-/_}"
+	local var=__gitcomp_builtin_"${cmd//-/_}"
 	local options
 	eval "options=\${$var-}"
 
-- 
2.30.0.rc1


```

## Felipe Contreras, 2020-12-23 14:48

Subject: [PATCH v2 4/4] completion: bash: add correct suffix in variables
Message-ID: <20201223144845.143039-5-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20201223144845.143039-5-felipe.contreras%40gmail.com
In-Reply-To: <20201223144845.143039-1-felipe.contreras@gmail.com>

```
__gitcomp automatically adds a suffix, but __gitcomp_nl and others
don't, we need to specify a space by default.

Can be tested with:

  git config branch.autoSetupMe<tab>

This fix only works for versions of bash greater than 4.0, before that
"local sfx" creates an empty string, therefore the unset expansion
doesn't work. The same happens in zsh.

Therefore we don't add the test for that for now.

The correct fix for all shells requires semantic changes in __gitcomp,
but that can be done later.

Cc: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 64cf35ba04..926e9ceb56 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2636,7 +2636,7 @@ __git_complete_config_variable_name ()
 		local pfx="${cur_%.*}."
 		cur_="${cur_#*.}"
 		__gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
-		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
+		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx- }"
 		return
 		;;
 	guitool.*.*)
@@ -2670,7 +2670,7 @@ __git_complete_config_variable_name ()
 		local pfx="${cur_%.*}."
 		cur_="${cur_#*.}"
 		__git_compute_all_commands
-		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
+		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx- }"
 		return
 		;;
 	remote.*.*)
@@ -2686,7 +2686,7 @@ __git_complete_config_variable_name ()
 		local pfx="${cur_%.*}."
 		cur_="${cur_#*.}"
 		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
-		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
+		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx- }"
 		return
 		;;
 	url.*.*)
-- 
2.30.0.rc1


```

## Felipe Contreras, 2020-12-23 14:48

Subject: [PATCH v2 2/4] completion: bash: fix for suboptions with value
Message-ID: <20201223144845.143039-3-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20201223144845.143039-3-felipe.contreras%40gmail.com
In-Reply-To: <20201223144845.143039-1-felipe.contreras@gmail.com>

```
We need to ignore options that don't start with -- as well.

Depending on the value of COMP_WORDBREAKS the last word could be
duplicated otherwise.

Can be tested with:

  git merge -X diff-algorithm=<tab>

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash |  2 +-
 t/t9902-completion.sh                  | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 10d225cc9c..10e69af9ed 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -347,7 +347,7 @@ __gitcomp ()
 	local cur_="${3-$cur}"
 
 	case "$cur_" in
-	--*=)
+	*=)
 		;;
 	--no-*)
 		local c i=0 IFS=$' \t\n'
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 81a728c743..c41030b6cc 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -537,6 +537,15 @@ test_expect_success '__gitcomp - expand/narrow all negative options' '
 	EOF
 '
 
+test_expect_success '__gitcomp - equal skip' '
+	test_gitcomp "--option=" "--option=" <<-\EOF &&
+
+	EOF
+	test_gitcomp "option=" "option=" <<-\EOF
+
+	EOF
+'
+
 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
 	__gitcomp "$invalid_variable_name"
 '
@@ -2354,6 +2363,12 @@ test_expect_success 'git clone --config= - value' '
 	EOF
 '
 
+test_expect_success 'options with value' '
+	test_completion "git merge -X diff-algorithm=" <<-\EOF
+
+	EOF
+'
+
 test_expect_success 'sourcing the completion script clears cached commands' '
 	__git_compute_all_commands &&
 	verbose test -n "$__git_all_commands" &&
-- 
2.30.0.rc1


```
