Thread (8 messages) flat view 8 messages, 2 authors, 2016-06-15
STALE3732d

[PATCH 1/2] bash: add helper function to get config variables for completion

From: SZEDER Gábor <hidden>
Date: 2016-06-15 22:49:47
Subsystem: the rest · Maintainer: Linus Torvalds

Currently there are three completion functions that perform similar
queries to 'git config' to get config variable names.  These are the
completion of aliases, remotes, and remote groups for 'git remote
update'.  Since the following patch is about to add yet another
similar 'git config'-querying completion function to support pretty
aliases, it's time to introduce a unified helper function first to
avoid redundant code.

We took care that the resulting helper function still copes well with
newlines in config variable values and that it works with 'set -u'
(see commits e0d7805 (completion: fix alias listings with newlines,
2009-10-08) and 25a31f8 (bash-completion: Support running when set -u
is enabled, 2009-01-15) for details).

Signed-off-by: SZEDER Gábor <redacted>
---
 contrib/completion/git-completion.bash |   28 +++++++++++-----------------
 1 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f83f019..97a3d8c 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -451,10 +451,7 @@ __git_remotes ()
 		echo ${i#$d/remotes/}
 	done
 	[ "$ngoff" ] && shopt -u nullglob
-	for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
-		i="${i#remote.}"
-		echo "${i/.url*/}"
-	done
+	__git_get_config_variables "remote" "url"
 }
 
 __git_list_merge_strategies ()
@@ -750,14 +747,16 @@ __git_compute_porcelain_commands ()
 	: ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
 }
 
-__git_aliases ()
+# returns all config variables within a given section with an optional
+# suffix, with both the section name and the suffix removed
+__git_get_config_variables ()
 {
-	local i IFS=$'\n'
-	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
+	local section="$1" suffix="${2-}" i IFS=$'\n'
+	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "$section\..*${suffix:+\.$suffix}" 2>/dev/null); do
 		case "$i" in
-		alias.*)
-			i="${i#alias.}"
-			echo "${i/ */}"
+		$section.*)
+			i="${i#$section.}"
+			echo "${i/${suffix:+.$suffix} */}"
 			;;
 		esac
 	done
@@ -2017,12 +2016,7 @@ _git_remote ()
 		__gitcomp "$(__git_remotes)"
 		;;
 	update)
-		local i c='' IFS=$'\n'
-		for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
-			i="${i#remotes.}"
-			c="$c ${i/ */}"
-		done
-		__gitcomp "$c"
+		__gitcomp "$(__git_get_config_variables "remotes")"
 		;;
 	*)
 		COMPREPLY=()
@@ -2366,7 +2360,7 @@ _git ()
 			"
 			;;
 		*)     __git_compute_porcelain_commands
-		       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
+		       __gitcomp "$__git_porcelain_commands $(__git_get_config_variables "alias")" ;;
 		esac
 		return
 	fi
-- 
1.7.3.1.151.g3779c
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help