# [PATCH 1/3] completion: complete config variables for --get/getall/unset/unset-all

7 messages from 2009-05-07 to 2009-05-09. Participants: Stephen Boyd, Shawn O. Pearce.
Thread: https://gitlist.dev/t/19218

## Stephen Boyd, 2009-05-07 06:15

Subject: [PATCH 1/3] completion: complete config variables for --get/getall/unset/unset-all
Message-ID: <1241676906-29783-1-git-send-email-bebarino@gmail.com>
URL: https://gitlist.dev/e/1241676906-29783-1-git-send-email-bebarino%40gmail.com

```
This should make it easier for users to get and unset their
configuration variables without having to open documentation or dig
through their configuration file.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---
 contrib/completion/git-completion.bash |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 1683e6d..72a16a1 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1322,6 +1322,17 @@ _git_send_email ()
 	COMPREPLY=()
 }
 
+__git_config_get_set_variables ()
+{
+	for i in $(git --git-dir="$(__gitdir)" config --list); do
+		case "$i" in
+		*.*)
+			echo "${i/=*/}"
+			;;
+		esac
+	done
+}
+
 _git_config ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -1388,6 +1399,10 @@ _git_config ()
 		__gitcomp "$__git_send_email_suppresscc_options"
 		return
 		;;
+	--get|--get-all|--unset|--unset-all)
+		__gitcomp "$(__git_config_get_set_variables)"
+		return
+		;;
 	*.*)
 		COMPREPLY=()
 		return
-- 
1.6.3.rc4.29.g8146

```

## Stephen Boyd, 2009-05-07 06:15

Subject: [PATCH 2/3] completion: add find_argument_and_value() function
Message-ID: <1241676906-29783-2-git-send-email-bebarino@gmail.com>
URL: https://gitlist.dev/e/1241676906-29783-2-git-send-email-bebarino%40gmail.com
In-Reply-To: <1241676906-29783-1-git-send-email-bebarino@gmail.com>

```
Add a function to get arguments and their associated value. This is used
by the config completion to find config file arguments.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---

I'm not sure if this is correct. It works for the case I have, but
others I'm not so sure. Let's just say I'm no bash expert.

 contrib/completion/git-completion.bash |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 72a16a1..f179cc8 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -618,6 +618,31 @@ __git_find_subcommand ()
 	done
 }
 
+# __git_find_argument_and_value requires 1 argument
+__git_find_argument_and_value ()
+{
+	local word nextword argument c=1
+
+	while [ $c -lt $COMP_CWORD ]; do
+		word="${COMP_WORDS[c]}"
+		for argument in $1; do
+			if [ "${argument##--}" = "${word##--}" ]; then
+				nextword=${COMP_WORDS[$((c+1))]}
+				if [ "${nextword#-}" != "$nextword" ]; then
+					nextword=
+				fi
+				echo "$argument $nextword"
+				return
+			fi
+			if [ "${argument/=*/}" = "${word/=*/}" ]; then
+				echo "$word"
+				return
+			fi
+		done
+		c=$((++c))
+	done
+}
+
 __git_has_doubledash ()
 {
 	local c=1
-- 
1.6.3.rc4.29.g8146

```

## Stephen Boyd, 2009-05-07 06:18

Subject: [PATCH 2/3] completion: add find_argument_and_value() function
Message-ID: <1241677093-29937-1-git-send-email-bebarino@gmail.com>
URL: https://gitlist.dev/e/1241677093-29937-1-git-send-email-bebarino%40gmail.com
In-Reply-To: <1241676906-29783-1-git-send-email-bebarino@gmail.com>

```
Add a function to get arguments and their associated value. This is used
by the config completion to find config file arguments.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---

I'm not sure if this is correct. It works for the case I have, but
others I'm not so sure. Let's just say I'm no bash expert.

 contrib/completion/git-completion.bash |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 72a16a1..f179cc8 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -618,6 +618,31 @@ __git_find_subcommand ()
 	done
 }
 
+# __git_find_argument_and_value requires 1 argument
+__git_find_argument_and_value ()
+{
+	local word nextword argument c=1
+
+	while [ $c -lt $COMP_CWORD ]; do
+		word="${COMP_WORDS[c]}"
+		for argument in $1; do
+			if [ "${argument##--}" = "${word##--}" ]; then
+				nextword=${COMP_WORDS[$((c+1))]}
+				if [ "${nextword#-}" != "$nextword" ]; then
+					nextword=
+				fi
+				echo "$argument $nextword"
+				return
+			fi
+			if [ "${argument/=*/}" = "${word/=*/}" ]; then
+				echo "$word"
+				return
+			fi
+		done
+		c=$((++c))
+	done
+}
+
 __git_has_doubledash ()
 {
 	local c=1
-- 
1.6.3.rc4.29.g8146

```

## Stephen Boyd, 2009-05-07 06:18

Subject: [PATCH 3/3] completion: use specified file (if any) when completing config vars
Message-ID: <1241677093-29937-2-git-send-email-bebarino@gmail.com>
URL: https://gitlist.dev/e/1241677093-29937-2-git-send-email-bebarino%40gmail.com
In-Reply-To: <1241677093-29937-1-git-send-email-bebarino@gmail.com>

```
Only list the variables that can actually be affected by the command by
searching the arguments for a config file setting.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---
 contrib/completion/git-completion.bash |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f179cc8..8a9863b 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1349,7 +1349,10 @@ _git_send_email ()
 
 __git_config_get_set_variables ()
 {
-	for i in $(git --git-dir="$(__gitdir)" config --list); do
+	local args="--global --system --file= -f"
+	local config_file="$(__git_find_argument_and_value "$args")"
+	for i in $(git --git-dir="$(__gitdir)" config $config_file --list \
+			2>/dev/null); do
 		case "$i" in
 		*.*)
 			echo "${i/=*/}"
-- 
1.6.3.rc4.29.g8146

```

## Shawn O. Pearce, 2009-05-07 15:12

Subject: Re: [PATCH 3/3] completion: use specified file (if any) when completing config vars
Message-ID: <20090507151207.GQ30527@spearce.org>
URL: https://gitlist.dev/e/20090507151207.GQ30527%40spearce.org
In-Reply-To: <1241677093-29937-2-git-send-email-bebarino@gmail.com>

```
Stephen Boyd <bebarino@gmail.com> wrote:
> Only list the variables that can actually be affected by the command by
> searching the arguments for a config file setting.

Wow, this is a lot of magic.

Try:

  git config --global remote.gitster.url $(git config remote.o<TAB>

You'll complete against --global, not the local repository.

 
>  __git_config_get_set_variables ()
>  {
> -	for i in $(git --git-dir="$(__gitdir)" config --list); do
> +	local args="--global --system --file= -f"
> +	local config_file="$(__git_find_argument_and_value "$args")"
> +	for i in $(git --git-dir="$(__gitdir)" config $config_file --list \
> +			2>/dev/null); do

-- 
Shawn.

```

## Stephen Boyd, 2009-05-07 19:55

Subject: Re: [PATCH 3/3] completion: use specified file (if any) when completing config vars
Message-ID: <780e0a6b0905071255g4ade6316oace6a2be6a6d2@mail.gmail.com>
URL: https://gitlist.dev/e/780e0a6b0905071255g4ade6316oace6a2be6a6d2%40mail.gmail.com
In-Reply-To: <20090507151207.GQ30527@spearce.org>

```
On Thu, May 7, 2009 at 8:12 AM, Shawn O. Pearce <spearce@spearce.org> wrote:
> Stephen Boyd <bebarino@gmail.com> wrote:
>> Only list the variables that can actually be affected by the command by
>> searching the arguments for a config file setting.
>
> Wow, this is a lot of magic.
>
> Try:
>
>  git config --global remote.gitster.url $(git config remote.o<TAB>
>
> You'll complete against --global, not the local repository.
>

Yeah, I didn't think it would be very robust. Is it a good idea to
keep going down this path, or is it too magical? This example could be
fixed by searching the words backwards, but I imagine there is some
other case where that will fail. Also, --global and --system don't
have values, so right now it's all wrong (i.e. config_file is
"--global remote.gitster.url" for this example).

```

## Stephen Boyd, 2009-05-09 01:00

Subject: Re: [PATCH 3/3] completion: use specified file (if any) when completing config vars
Message-ID: <4A04D5A9.1090901@gmail.com>
URL: https://gitlist.dev/e/4A04D5A9.1090901%40gmail.com
In-Reply-To: <780e0a6b0905071255g4ade6316oace6a2be6a6d2@mail.gmail.com>

```
Stephen Boyd wrote:
> On Thu, May 7, 2009 at 8:12 AM, Shawn O. Pearce <spearce@spearce.org> wrote:
>> Wow, this is a lot of magic.
>>
>> Try:
>>
>> Â git config --global remote.gitster.url $(git config remote.o<TAB>
>>
>> You'll complete against --global, not the local repository
>
> Yeah, I didn't think it would be very robust. Is it a good idea to
> keep going down this path, or is it too magical? This example could be
> fixed by searching the words backwards, but I imagine there is some
> other case where that will fail. Also, --global and --system don't
> have values, so right now it's all wrong (i.e. config_file is
> "--global remote.gitster.url" for this example).

After further investigation, I can't get the tab completion you talk
about here to work at all. Should it work?

I've reworked this into two patches which I'll send as a follow up soon.

```
