Skip to contentSkip to conversation

completion: complete config variables for --get/getall/unset/unset-all

PATCH In review Stephen Boyd 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

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.

---
 contrib/completion/git-completion.bash |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
contrib/completion/git-completion.bash +15 -0
index 1683e6d..72a16a1 100755
@@ -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
Signature · 1 line
1.6.3.rc4.29.g8146
#1 lore raw

PATCH 2/3 completion: add find_argument_and_value() function

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

---

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(-)
contrib/completion/git-completion.bash +25 -0
index 72a16a1..f179cc8 100755
@@ -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
Signature · 1 line
1.6.3.rc4.29.g8146
#2 lore raw

PATCH 2/3 completion: add find_argument_and_value() function

In reply to Stephen Boyd

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

---

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(-)
contrib/completion/git-completion.bash +25 -0
index 72a16a1..f179cc8 100755
@@ -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
Signature · 1 line
1.6.3.rc4.29.g8146
#3 lore raw

PATCH 3/3 completion: use specified file (if any) when completing config vars

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

---
 contrib/completion/git-completion.bash |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
contrib/completion/git-completion.bash +4 -1
index f179cc8..8a9863b 100755
@@ -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/=*/}"
Signature · 1 line
1.6.3.rc4.29.g8146
#4 lore raw

Re: [PATCH 3/3] completion: use specified file (if any) when completing config vars

Quoted 2 lines · Stephen Boyd

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.

Quoted 7 lines

__git_config_get_set_variables ()
{

contrib/completion/git-completion.bash +4 -1
-	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
Signature · 1 line
Shawn.
#5 lore raw

Re: [PATCH 3/3] completion: use specified file (if any) when completing config vars

Quoted 4 lines · Shawn O. Pearce
Quoted 2 lines · Stephen Boyd

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

Quoted 8 lines

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

#6 lore raw

Re: [PATCH 3/3] completion: use specified file (if any) when completing config vars

Quoted 7 lines · Stephen Boyd
Quoted 6 lines · Shawn O. Pearce

Wow, this is a lot of magic.

Try:

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

Quoted 8 lines
Quoted 1 line

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.