git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Oct 14, 2010, 08:58 UTC
Message-ID
<1287046727-22509-1-git-send-email-szeder@ira.uka.de>
In-Reply-To
<20101014085538.GB838@neumann>

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 <szeder@ira.uka.de>
---
 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
Previous: SZEDER GáborNext: Jonathan Nieder
Message 5 of 8 in “bash: support pretty format aliases”
  1. bash: support pretty format aliasesSZEDER Gábor, Oct 10, 2010
  2. Jonathan NiederOct 10, 2010
  3. bash: support pretty format aliasesSZEDER Gábor, Oct 10, 2010
  4. SZEDER GáborOct 14, 2010
  5. 1/2 bash: add helper function to get config variables for completionSZEDER Gábor, Oct 14, 2010
  6. Jonathan NiederOct 14, 2010
  7. SZEDER GáborOct 20, 2010
  8. 2/2 bash: support pretty format aliasesSZEDER Gábor, Oct 14, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.