git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>'
@ 2019-08-13 12:26 SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 01/11] completion: fix a typo in a comment SZEDER Gábor
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

Recently I've been writing 'git -c foo.bar ...' often enough that
typing our long configuration variables got annoying...

So this patch series teaches our Bash completion script to complete
supported configuration variables and their values after 'git -c
<TAB>', and, while at it, after 'git clone -c <TAB>' as well.


SZEDER Gábor (11):
  completion: fix a typo in a comment
  completion: complete more values of more 'color.*' configuration
    variables
  completion: add tests for 'git config' completion
  completion: deduplicate configuration sections
  completion: use 'sort -u' to deduplicate config variable names
  completion: simplify inner 'case' pattern in __gitcomp()
  completion: split _git_config()
  completion: complete configuration sections and variable names for
    'git -c'
  completion: complete values of configuration variables after 'git -c
    var='
  completion: complete config variables names and values for 'git clone
    -c'
  completion: complete config variables and values for 'git clone
    --config='

 contrib/completion/git-completion.bash | 244 ++++++++++++++++++-------
 t/t9902-completion.sh                  |  63 +++++++
 2 files changed, 243 insertions(+), 64 deletions(-)

-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 01/11] completion: fix a typo in a comment
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 02/11] completion: complete more values of more 'color.*' configuration variables SZEDER Gábor
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

Signed-off-by: SZEDER Gábor <szeder.dev@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 e087c4bf00..cd9d8e1940 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -524,7 +524,7 @@ __git_index_files ()
 			# Even when a directory name itself does not contain
 			# any special characters, it will still be quoted if
 			# any of its (stripped) trailing path components do.
-			# Because of this we may have seen the same direcory
+			# Because of this we may have seen the same directory
 			# both quoted and unquoted.
 			if (p in paths)
 				# We have seen the same directory unquoted,
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 02/11] completion: complete more values of more 'color.*' configuration variables
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 01/11] completion: fix a typo in a comment SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 03/11] completion: add tests for 'git config' completion SZEDER Gábor
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

Most 'color.*' configuration variables, with the sole exception of
'color.pager', accept the same set of values, but our completion
script recognizes only about half of them.  We could explicitly add
all those missing variables, but let's try to reduce future
maintenance burden, and use the catch-all 'color.*' pattern instead,
so this list won't get out of sync when a similar new configuration
variable accepting the same values is introduced [1].

Furthermore, their documentation explicitly mentions that they all
accept the standard boolean values 'false' and 'true' as well, so list
these, too, among the possible values.

[1] OTOH, there will be a maintenance burden if ever a new
    'color.something' is introduced which doesn't accept the same set
    of values.  We'll see which one happens first...

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---

Notes:
    I find the documentation of these configuration variables confusing.
    A so-called "boolean" (quoting the docs), that besides 'false' and
    'true' (and their standard 0,no,1,yes and non-standard 'never'/'auto'
    synonyms) can have a third possible value 'always', too, is, well, not
    a boolean.

 contrib/completion/git-completion.bash | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index cd9d8e1940..c59347daee 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2277,11 +2277,6 @@ _git_config ()
 		__gitcomp "$__git_merge_strategies"
 		return
 		;;
-	color.branch|color.diff|color.interactive|\
-	color.showbranch|color.status|color.ui)
-		__gitcomp "always never auto"
-		return
-		;;
 	color.pager)
 		__gitcomp "false true"
 		return
@@ -2293,6 +2288,10 @@ _git_config ()
 			"
 		return
 		;;
+	color.*)
+		__gitcomp "false true always never auto"
+		return
+		;;
 	diff.submodule)
 		__gitcomp "$__git_diff_submodule_formats"
 		return
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 03/11] completion: add tests for 'git config' completion
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 01/11] completion: fix a typo in a comment SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 02/11] completion: complete more values of more 'color.*' configuration variables SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 04/11] completion: deduplicate configuration sections SZEDER Gábor
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

The next patches will change/refactor the way we complete
configuration variable names and values, so add a few tests to cover
the basics, namely the completion of matching configuration sections,
full variable names, and their values.

Note that the test checking the completion of configuration sections
is currently failing, though it's not a sign of an actual bug.  If a
section contains multiple variables, then that section is currently
repeated as many times as the number of variables in there.  This is
not a correctness issue in practice, because Bash's completion
facilities remove all repetitions anyway.  Consequently, we could list
all those repeated sections in the expected output of this test as
well, but then it would have to be updated whenever a new
configuration variable is added to those sections.  Instead, list each
matching configuration section only once, mark the test as failing for
now, and the next patch will update the completion script to avoid
those repetitions.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 t/t9902-completion.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 75512c3403..e15be1164d 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1698,6 +1698,27 @@ do
 	'
 done
 
+test_expect_failure 'git config - section' '
+	test_completion "git config br" <<-\EOF
+	branch.Z
+	browser.Z
+	EOF
+'
+
+test_expect_success 'git config - variable name' '
+	test_completion "git config log.d" <<-\EOF
+	log.date Z
+	log.decorate Z
+	EOF
+'
+
+test_expect_success 'git config - value' '
+	test_completion "git config color.pager " <<-\EOF
+	false Z
+	true Z
+	EOF
+'
+
 test_expect_success 'sourcing the completion script clears cached commands' '
 	__git_compute_all_commands &&
 	verbose test -n "$__git_all_commands" &&
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 04/11] completion: deduplicate configuration sections
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (2 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 03/11] completion: add tests for 'git config' completion SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 05/11] completion: use 'sort -u' to deduplicate config variable names SZEDER Gábor
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

The number of configuration variables listed by the completion script
grew quite when we started to auto-generate it from the documentation
[1], so we now complete them in two steps: first we list only the
section names, then the rest [2].  To get the section names we simply
strip everything following the first dot in each variable name,
resulting in a lot of repeated section names, because most sections
contain more than one configuration variable.  This is not a
correctness issue in practice, because Bash's completion facilities
remove all repetitions anyway, but these repetitions make testing a
bit harder.

Replace the small 'sed' script removing subsections and variable names
with an 'awk' script that does the same, and in addition removes any
repeated configuration sections as well (by first creating and filling
an associative array indexed by all encountered configuration
sections, and then iterating over this array and printing the indices,
i.e. the unique section names).  This change makes the failing 'git
config - section' test in 't9902-completion.sh' pass.

Note that this changes the order of section names in the output, and
makes it downright undeterministic, but this is not an issue, because
Bash sorts them before presenting them to the user, and our completion
tests sort them as well before comparing with the expected output.

Yeah, it would be simpler and shorter to just append '| sort -u' to
that command, but that would incur the overhead of one more external
process and pipeline stage every time a user completes configuration
sections.

[1] e17ca92637 (completion: drop the hard coded list of config vars,
    2018-05-26)
[2] f22f682695 (completion: complete general config vars in two steps,
    2018-05-27)

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 10 +++++++++-
 t/t9902-completion.sh                  |  2 +-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c59347daee..f89324d84f 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2398,7 +2398,15 @@ _git_config ()
 		;;
 	*)
 		__git_compute_config_vars
-		__gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
+		__gitcomp "$(echo "$__git_config_vars" |
+				awk -F . '{
+					sections[$1] = 1
+				}
+				END {
+					for (s in sections)
+						print s "."
+				}
+				')"
 	esac
 }
 
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index e15be1164d..008fba7c89 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1698,7 +1698,7 @@ do
 	'
 done
 
-test_expect_failure 'git config - section' '
+test_expect_success 'git config - section' '
 	test_completion "git config br" <<-\EOF
 	branch.Z
 	browser.Z
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 05/11] completion: use 'sort -u' to deduplicate config variable names
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (3 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 04/11] completion: deduplicate configuration sections SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 06/11] completion: simplify inner 'case' pattern in __gitcomp() SZEDER Gábor
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

The completion script runs the classic '| sort | uniq' pipeline to
deduplicate the output of 'git help --config-for-completion'.  'sort
-u' does the same, but uses one less external process and pipeline
stage.  Not a bit win, as it's only run once as the list of supported
configuration variables is initialized, but at least it sets a better
example for others to follow.

Signed-off-by: SZEDER Gábor <szeder.dev@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 f89324d84f..b51cb31ea1 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2225,7 +2225,7 @@ __git_config_vars=
 __git_compute_config_vars ()
 {
 	test -n "$__git_config_vars" ||
-	__git_config_vars="$(git help --config-for-completion | sort | uniq)"
+	__git_config_vars="$(git help --config-for-completion | sort -u)"
 }
 
 _git_config ()
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 06/11] completion: simplify inner 'case' pattern in __gitcomp()
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (4 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 05/11] completion: use 'sort -u' to deduplicate config variable names SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 07/11] completion: split _git_config() SZEDER Gábor
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

The second '*' in the '--*=*' pattern of the inner 'case' statement of
the __gitcomp() helper function never matches anything, so let's use
'--*=' instead.

The purpose of that inner case statement is to decide when to append a
trailing space to the listed options and when not.  When an option
requires a stuck argument, i.e. '--option=', then the trailing space
should not be added, so the user can continue typing the required
argument right away.  That '--*=*' pattern is supposed to match these
options, but for this purpose that second '*' is unnecessary, a '--*='
pattern works just as well.  That second '*' would only make a
difference in case of a possible completion word like
'--option=value', but our completion script never passes such a word
to __gitcomp(), because the '--option=' and its 'value' must be
completed separately.

Signed-off-by: SZEDER Gábor <szeder.dev@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 b51cb31ea1..fc437bf3eb 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -340,7 +340,7 @@ __gitcomp ()
 			c="$c${4-}"
 			if [[ $c == "$cur_"* ]]; then
 				case $c in
-				--*=*|*.) ;;
+				--*=|*.) ;;
 				*) c="$c " ;;
 				esac
 				COMPREPLY[i++]="${2-}$c"
@@ -360,7 +360,7 @@ __gitcomp ()
 			c="$c${4-}"
 			if [[ $c == "$cur_"* ]]; then
 				case $c in
-				--*=*|*.) ;;
+				--*=|*.) ;;
 				*) c="$c " ;;
 				esac
 				COMPREPLY[i++]="${2-}$c"
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 07/11] completion: split _git_config()
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (5 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 06/11] completion: simplify inner 'case' pattern in __gitcomp() SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 08/11] completion: complete configuration sections and variable names for 'git -c' SZEDER Gábor
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

_git_config() contains two enormous case statements, one to complete
configuration sections and variable names, and the other to complete
their values.

Split these out into two separate helper functions, so in the next
patches we can use them to implement completion for 'git -c <TAB>'.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 39 ++++++++++++++++++++------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index fc437bf3eb..3e9c5b6b71 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2228,7 +2228,8 @@ __git_compute_config_vars ()
 	__git_config_vars="$(git help --config-for-completion | sort -u)"
 }
 
-_git_config ()
+# Completes possible values of various configuration variables.
+__git_complete_config_variable_value ()
 {
 	local varname
 
@@ -2320,19 +2321,16 @@ _git_config ()
 		__gitcomp "7bit 8bit quoted-printable base64"
 		return
 		;;
-	--get|--get-all|--unset|--unset-all)
-		__gitcomp_nl "$(__git_config_get_set_variables)"
-		return
-		;;
 	*.*)
 		return
 		;;
 	esac
+}
+
+# Completes configuration sections, subsections, variable names.
+__git_complete_config_variable_name ()
+{
 	case "$cur" in
-	--*)
-		__gitcomp_builtin config
-		return
-		;;
 	branch.*.*)
 		local pfx="${cur%.*}." cur_="${cur##*.}"
 		__gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
@@ -2407,6 +2405,29 @@ _git_config ()
 						print s "."
 				}
 				')"
+		;;
+	esac
+}
+
+_git_config ()
+{
+	case "$prev" in
+	--get|--get-all|--unset|--unset-all)
+		__gitcomp_nl "$(__git_config_get_set_variables)"
+		return
+		;;
+	*.*)
+		__git_complete_config_variable_value
+		return
+		;;
+	esac
+	case "$cur" in
+	--*)
+		__gitcomp_builtin config
+		;;
+	*)
+		__git_complete_config_variable_name
+		;;
 	esac
 }
 
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 08/11] completion: complete configuration sections and variable names for 'git -c'
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (6 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 07/11] completion: split _git_config() SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 09/11] completion: complete values of configuration variables after 'git -c var=' SZEDER Gábor
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

'git config' expects a configuration variable's name and value in
separate arguments, so we let the __gitcomp() helper append a space
character to each variable name by default, like we do for most other
things (--options, refs, paths, etc.).  'git -c', however, expects
them in a single option joined by a '=' character, i.e.
'section.name=value', so we should append a '=' character to each
fully completed variable name, but no space, so the user can continue
typing the value right away.

Add an option to the __git_complete_config_variable_name() function to
allow callers to specify an alternate suffix to add, and use it to
append that '=' character to configuration variables.  Update the
__gitcomp() helper function to not append a trailing space to any
completion words ending with a '=', not just to those option with a
stuck argument.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 59 ++++++++++++++++++++------
 t/t9902-completion.sh                  | 14 ++++++
 2 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3e9c5b6b71..367b1c50f4 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -360,7 +360,7 @@ __gitcomp ()
 			c="$c${4-}"
 			if [[ $c == "$cur_"* ]]; then
 				case $c in
-				--*=|*.) ;;
+				*=|*.) ;;
 				*) c="$c " ;;
 				esac
 				COMPREPLY[i++]="${2-}$c"
@@ -2328,18 +2328,33 @@ __git_complete_config_variable_value ()
 }
 
 # Completes configuration sections, subsections, variable names.
+#
+# Usage: __git_complete_config_variable_name [<option>]...
+# --sfx=<suffix>: A suffix to be appended to each fully completed
+#                 configuration variable name (but not to sections or
+#                 subsections) instead of the default space.
 __git_complete_config_variable_name ()
 {
+	local sfx
+
+	while test $# != 0; do
+		case "$1" in
+		--sfx=*)	sfx="${1##--sfx=}" ;;
+		*)		return 1 ;;
+		esac
+		shift
+	done
+
 	case "$cur" in
 	branch.*.*)
 		local pfx="${cur%.*}." cur_="${cur##*.}"
-		__gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
+		__gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	branch.*)
 		local pfx="${cur%.*}." cur_="${cur#*.}"
 		__gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
-		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
+		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	guitool.*.*)
@@ -2347,28 +2362,28 @@ __git_complete_config_variable_name ()
 		__gitcomp "
 			argPrompt cmd confirm needsFile noConsole noRescan
 			prompt revPrompt revUnmerged title
-			" "$pfx" "$cur_"
+			" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	difftool.*.*)
 		local pfx="${cur%.*}." cur_="${cur##*.}"
-		__gitcomp "cmd path" "$pfx" "$cur_"
+		__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	man.*.*)
 		local pfx="${cur%.*}." cur_="${cur##*.}"
-		__gitcomp "cmd path" "$pfx" "$cur_"
+		__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	mergetool.*.*)
 		local pfx="${cur%.*}." cur_="${cur##*.}"
-		__gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
+		__gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	pager.*)
 		local pfx="${cur%.*}." cur_="${cur#*.}"
 		__git_compute_all_commands
-		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
+		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	remote.*.*)
@@ -2376,23 +2391,23 @@ __git_complete_config_variable_name ()
 		__gitcomp "
 			url proxy fetch push mirror skipDefaultUpdate
 			receivepack uploadpack tagOpt pushurl
-			" "$pfx" "$cur_"
+			" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	remote.*)
 		local pfx="${cur%.*}." cur_="${cur#*.}"
 		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
-		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
+		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	url.*.*)
 		local pfx="${cur%.*}." cur_="${cur##*.}"
-		__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
+		__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	*.*)
 		__git_compute_config_vars
-		__gitcomp "$__git_config_vars"
+		__gitcomp "$__git_config_vars" "" "$cur" "$sfx"
 		;;
 	*)
 		__git_compute_config_vars
@@ -2409,6 +2424,20 @@ __git_complete_config_variable_name ()
 	esac
 }
 
+# Completes '='-separated configuration sections/variable names and values
+# for 'git -c section.name=value'.
+__git_complete_config_variable_name_and_value ()
+{
+	case "$cur" in
+	*=*)
+		# in the next patch...
+		;;
+	*)
+		__git_complete_config_variable_name --sfx='='
+		;;
+	esac
+}
+
 _git_config ()
 {
 	case "$prev" in
@@ -2984,7 +3013,11 @@ __git_main ()
 			# Bash filename completion
 			return
 			;;
-		-c|--namespace)
+		-c)
+			__git_complete_config_variable_name_and_value
+			return
+			;;
+		--namespace)
 			# we don't support completing these options' arguments
 			return
 			;;
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 008fba7c89..bf60a11fa8 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1719,6 +1719,20 @@ test_expect_success 'git config - value' '
 	EOF
 '
 
+test_expect_success 'git -c - section' '
+	test_completion "git -c br" <<-\EOF
+	branch.Z
+	browser.Z
+	EOF
+'
+
+test_expect_success 'git -c - variable name' '
+	test_completion "git -c log.d" <<-\EOF
+	log.date=Z
+	log.decorate=Z
+	EOF
+'
+
 test_expect_success 'sourcing the completion script clears cached commands' '
 	__git_compute_all_commands &&
 	verbose test -n "$__git_all_commands" &&
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 09/11] completion: complete values of configuration variables after 'git -c var='
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (7 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 08/11] completion: complete configuration sections and variable names for 'git -c' SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 10/11] completion: complete config variables names and values for 'git clone -c' SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 11/11] completion: complete config variables and values for 'git clone --config=' SZEDER Gábor
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

'git config' expects a configuration variable's name and value in
separate options, so we complete values as they stand on their own on
the command line.  'git -c', however, expects them in a single option
joined by a '=' character, so we should be able to complete values
when they are following 'section.name=' in the same word.

Add new options to the __git_complete_config_variable_value() function
to allow callers to specify the current word to be completed and the
configuration variable whose value is to be completed, and use these
to complete possible values after 'git -c 'section.name=<TAB>'.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 65 ++++++++++++++++----------
 t/t9902-completion.sh                  |  7 +++
 2 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 367b1c50f4..6f2bc60707 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2229,96 +2229,112 @@ __git_compute_config_vars ()
 }
 
 # Completes possible values of various configuration variables.
+#
+# Usage: __git_complete_config_variable_value [<option>]...
+# --varname=<word>: The name of the configuration variable whose value is
+#                   to be completed.  Defaults to the previous word on the
+#                   command line.
+# --cur=<word>: The current value to be completed.  Defaults to the current
+#               word to be completed.
 __git_complete_config_variable_value ()
 {
-	local varname
+	local varname="$prev" cur_="$cur"
+
+	while test $# != 0; do
+		case "$1" in
+		--varname=*)	varname="${1##--varname=}" ;;
+		--cur=*)	cur_="${1##--cur=}" ;;
+		*)		return 1 ;;
+		esac
+		shift
+	done
 
 	if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
-		varname="${prev,,}"
+		varname="${varname,,}"
 	else
-		varname="$(echo "$prev" |tr A-Z a-z)"
+		varname="$(echo "$varname" |tr A-Z a-z)"
 	fi
 
 	case "$varname" in
 	branch.*.remote|branch.*.pushremote)
-		__gitcomp_nl "$(__git_remotes)"
+		__gitcomp_nl "$(__git_remotes)" "" "$cur_"
 		return
 		;;
 	branch.*.merge)
-		__git_complete_refs
+		__git_complete_refs --cur="$cur_"
 		return
 		;;
 	branch.*.rebase)
-		__gitcomp "false true merges preserve interactive"
+		__gitcomp "false true merges preserve interactive" "" "$cur_"
 		return
 		;;
 	remote.pushdefault)
-		__gitcomp_nl "$(__git_remotes)"
+		__gitcomp_nl "$(__git_remotes)" "" "$cur_"
 		return
 		;;
 	remote.*.fetch)
-		local remote="${prev#remote.}"
+		local remote="${varname#remote.}"
 		remote="${remote%.fetch}"
-		if [ -z "$cur" ]; then
+		if [ -z "$cur_" ]; then
 			__gitcomp_nl "refs/heads/" "" "" ""
 			return
 		fi
-		__gitcomp_nl "$(__git_refs_remotes "$remote")"
+		__gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
 		return
 		;;
 	remote.*.push)
-		local remote="${prev#remote.}"
+		local remote="${varname#remote.}"
 		remote="${remote%.push}"
 		__gitcomp_nl "$(__git for-each-ref \
-			--format='%(refname):%(refname)' refs/heads)"
+			--format='%(refname):%(refname)' refs/heads)" "" "$cur_"
 		return
 		;;
 	pull.twohead|pull.octopus)
 		__git_compute_merge_strategies
-		__gitcomp "$__git_merge_strategies"
+		__gitcomp "$__git_merge_strategies" "" "$cur_"
 		return
 		;;
 	color.pager)
-		__gitcomp "false true"
+		__gitcomp "false true" "" "$cur_"
 		return
 		;;
 	color.*.*)
 		__gitcomp "
 			normal black red green yellow blue magenta cyan white
 			bold dim ul blink reverse
-			"
+			" "" "$cur_"
 		return
 		;;
 	color.*)
-		__gitcomp "false true always never auto"
+		__gitcomp "false true always never auto" "" "$cur_"
 		return
 		;;
 	diff.submodule)
-		__gitcomp "$__git_diff_submodule_formats"
+		__gitcomp "$__git_diff_submodule_formats" "" "$cur_"
 		return
 		;;
 	help.format)
-		__gitcomp "man info web html"
+		__gitcomp "man info web html" "" "$cur_"
 		return
 		;;
 	log.date)
-		__gitcomp "$__git_log_date_formats"
+		__gitcomp "$__git_log_date_formats" "" "$cur_"
 		return
 		;;
 	sendemail.aliasfiletype)
-		__gitcomp "mutt mailrc pine elm gnus"
+		__gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
 		return
 		;;
 	sendemail.confirm)
-		__gitcomp "$__git_send_email_confirm_options"
+		__gitcomp "$__git_send_email_confirm_options" "" "$cur_"
 		return
 		;;
 	sendemail.suppresscc)
-		__gitcomp "$__git_send_email_suppresscc_options"
+		__gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
 		return
 		;;
 	sendemail.transferencoding)
-		__gitcomp "7bit 8bit quoted-printable base64"
+		__gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
 		return
 		;;
 	*.*)
@@ -2430,7 +2446,8 @@ __git_complete_config_variable_name_and_value ()
 {
 	case "$cur" in
 	*=*)
-		# in the next patch...
+		__git_complete_config_variable_value \
+			--varname="${cur%%=*}" --cur="${cur#*=}"
 		;;
 	*)
 		__git_complete_config_variable_name --sfx='='
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index bf60a11fa8..9e90a64830 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1733,6 +1733,13 @@ test_expect_success 'git -c - variable name' '
 	EOF
 '
 
+test_expect_success 'git -c - value' '
+	test_completion "git -c color.pager=" <<-\EOF
+	false Z
+	true Z
+	EOF
+'
+
 test_expect_success 'sourcing the completion script clears cached commands' '
 	__git_compute_all_commands &&
 	verbose test -n "$__git_all_commands" &&
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 10/11] completion: complete config variables names and values for 'git clone -c'
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (8 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 09/11] completion: complete values of configuration variables after 'git -c var=' SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  2019-08-13 12:26 ` [PATCH 11/11] completion: complete config variables and values for 'git clone --config=' SZEDER Gábor
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

The previous commits taught the completion script how to complete
configuration section, variable names, and their valus after 'git -c
<TAB>', and with a bit of foresight encapsulated all that in a
dedicated helper function.  Use that function to complete the unstuck
argument of 'git config -c|--config <TAB>', which expect configuration
variables and values in the same 'section.name=value' form.

Note that handling the struck argument for 'git clone --config=<TAB>'
requires some extra care, so it will be done a separate patch.

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

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6f2bc60707..279f04df87 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1399,6 +1399,12 @@ _git_clean ()
 
 _git_clone ()
 {
+	case "$prev" in
+	-c|--config)
+		__git_complete_config_variable_name_and_value
+		return
+		;;
+	esac
 	case "$cur" in
 	--*)
 		__gitcomp_builtin clone
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 11/11] completion: complete config variables and values for 'git clone --config='
  2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
                   ` (9 preceding siblings ...)
  2019-08-13 12:26 ` [PATCH 10/11] completion: complete config variables names and values for 'git clone -c' SZEDER Gábor
@ 2019-08-13 12:26 ` SZEDER Gábor
  10 siblings, 0 replies; 12+ messages in thread
From: SZEDER Gábor @ 2019-08-13 12:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

Completing configuration sections and variable names for the stuck
argument of 'git clone --config=<TAB>' requires a bit of extra care
compared to doing the same for the unstuck argument of 'git clone
--config <TAB>', because we have to deal with that '--config=' being
part of the current word to be completed.

Add an option to the __git_complete_config_variable_name_and_value()
and in turn to the __git_complete_config_variable_name() helper
functions to specify the current section/variable name to be
completed, so they can be used even when completing the stuck argument
of '--config='.

__git_complete_config_variable_value() already has such an option, and
thus no further changes were necessary to complete possible values
after 'git clone --config=section.name=<TAB>'.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 66 +++++++++++++++++++-------
 t/t9902-completion.sh                  | 21 ++++++++
 2 files changed, 70 insertions(+), 17 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 279f04df87..ce7ff0a96d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1406,6 +1406,11 @@ _git_clone ()
 		;;
 	esac
 	case "$cur" in
+	--config=*)
+		__git_complete_config_variable_name_and_value \
+			--cur="${cur##--config=}"
+		return
+		;;
 	--*)
 		__gitcomp_builtin clone
 		return
@@ -2352,35 +2357,41 @@ __git_complete_config_variable_value ()
 # Completes configuration sections, subsections, variable names.
 #
 # Usage: __git_complete_config_variable_name [<option>]...
+# --cur=<word>: The current configuration section/variable name to be
+#               completed.  Defaults to the current word to be completed.
 # --sfx=<suffix>: A suffix to be appended to each fully completed
 #                 configuration variable name (but not to sections or
 #                 subsections) instead of the default space.
 __git_complete_config_variable_name ()
 {
-	local sfx
+	local cur_="$cur" sfx
 
 	while test $# != 0; do
 		case "$1" in
+		--cur=*)	cur_="${1##--cur=}" ;;
 		--sfx=*)	sfx="${1##--sfx=}" ;;
 		*)		return 1 ;;
 		esac
 		shift
 	done
 
-	case "$cur" in
+	case "$cur_" in
 	branch.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
 		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
 		;;
 	guitool.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "
 			argPrompt cmd confirm needsFile noConsole noRescan
 			prompt revPrompt revUnmerged title
@@ -2388,28 +2399,33 @@ __git_complete_config_variable_name ()
 		return
 		;;
 	difftool.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	man.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	mergetool.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	pager.*)
-		local pfx="${cur%.*}." cur_="${cur#*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_#*.}"
 		__git_compute_all_commands
 		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	remote.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "
 			url proxy fetch push mirror skipDefaultUpdate
 			receivepack uploadpack tagOpt pushurl
@@ -2417,19 +2433,21 @@ __git_complete_config_variable_name ()
 		return
 		;;
 	remote.*)
-		local pfx="${cur%.*}." cur_="${cur#*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_#*.}"
 		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
 		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	url.*.*)
-		local pfx="${cur%.*}." cur_="${cur##*.}"
+		local pfx="${cur_%.*}."
+		cur_="${cur_##*.}"
 		__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
 		return
 		;;
 	*.*)
 		__git_compute_config_vars
-		__gitcomp "$__git_config_vars" "" "$cur" "$sfx"
+		__gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
 		;;
 	*)
 		__git_compute_config_vars
@@ -2441,22 +2459,36 @@ __git_complete_config_variable_name ()
 					for (s in sections)
 						print s "."
 				}
-				')"
+				')" "" "$cur_"
 		;;
 	esac
 }
 
 # Completes '='-separated configuration sections/variable names and values
 # for 'git -c section.name=value'.
+#
+# Usage: __git_complete_config_variable_name_and_value [<option>]...
+# --cur=<word>: The current configuration section/variable name/value to be
+#               completed. Defaults to the current word to be completed.
 __git_complete_config_variable_name_and_value ()
 {
-	case "$cur" in
+	local cur_="$cur"
+
+	while test $# != 0; do
+		case "$1" in
+		--cur=*)	cur_="${1##--cur=}" ;;
+		*)		return 1 ;;
+		esac
+		shift
+	done
+
+	case "$cur_" in
 	*=*)
 		__git_complete_config_variable_value \
-			--varname="${cur%%=*}" --cur="${cur#*=}"
+			--varname="${cur_%%=*}" --cur="${cur_#*=}"
 		;;
 	*)
-		__git_complete_config_variable_name --sfx='='
+		__git_complete_config_variable_name --cur="$cur_" --sfx='='
 		;;
 	esac
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 9e90a64830..5d98d66dbd 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1740,6 +1740,27 @@ test_expect_success 'git -c - value' '
 	EOF
 '
 
+test_expect_success 'git clone --config= - section' '
+	test_completion "git clone --config=br" <<-\EOF
+	branch.Z
+	browser.Z
+	EOF
+'
+
+test_expect_success 'git clone --config= - variable name' '
+	test_completion "git clone --config=log.d" <<-\EOF
+	log.date=Z
+	log.decorate=Z
+	EOF
+'
+
+test_expect_success 'git clone --config= - value' '
+	test_completion "git clone --config=color.pager=" <<-\EOF
+	false Z
+	true Z
+	EOF
+'
+
 test_expect_success 'sourcing the completion script clears cached commands' '
 	__git_compute_all_commands &&
 	verbose test -n "$__git_all_commands" &&
-- 
2.23.0.rc2.350.gf4fdc32db7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2019-08-13 12:27 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-13 12:26 [PATCH 00/11] completion: complete configuration variables and values after 'git -c <TAB>' SZEDER Gábor
2019-08-13 12:26 ` [PATCH 01/11] completion: fix a typo in a comment SZEDER Gábor
2019-08-13 12:26 ` [PATCH 02/11] completion: complete more values of more 'color.*' configuration variables SZEDER Gábor
2019-08-13 12:26 ` [PATCH 03/11] completion: add tests for 'git config' completion SZEDER Gábor
2019-08-13 12:26 ` [PATCH 04/11] completion: deduplicate configuration sections SZEDER Gábor
2019-08-13 12:26 ` [PATCH 05/11] completion: use 'sort -u' to deduplicate config variable names SZEDER Gábor
2019-08-13 12:26 ` [PATCH 06/11] completion: simplify inner 'case' pattern in __gitcomp() SZEDER Gábor
2019-08-13 12:26 ` [PATCH 07/11] completion: split _git_config() SZEDER Gábor
2019-08-13 12:26 ` [PATCH 08/11] completion: complete configuration sections and variable names for 'git -c' SZEDER Gábor
2019-08-13 12:26 ` [PATCH 09/11] completion: complete values of configuration variables after 'git -c var=' SZEDER Gábor
2019-08-13 12:26 ` [PATCH 10/11] completion: complete config variables names and values for 'git clone -c' SZEDER Gábor
2019-08-13 12:26 ` [PATCH 11/11] completion: complete config variables and values for 'git clone --config=' SZEDER Gábor

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).