threads / patch / 43532

patchBash completion support for aliases

Subject: [PATCH] Bash completion support for aliases

## tl;dr

2 messages between Oct 28, 2006 and Oct 28, 2006. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Dennis Stosberg· Oct 28, 2006, 12:12 UTC · lore
 - Add aliases to the list of available git commands.
 - Make completion work for aliased commands.
Signed-off-by: Dennis Stosberg <dennis@stosberg.net>
---
 contrib/completion/git-completion.bash |   29 +++++++++++++++++++++++++++--
 1 files changed, 27 insertions(+), 2 deletions(-)
Show changes to contrib/completion/git-completion.bash +27 −2
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index d9cb17d..b074f4f 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -101,6 +101,23 @@ __git_complete_file ()
 	esac
 }
 
+__git_aliases ()
+{
+	git repo-config --list | grep '^alias\.' \
+		| sed -e 's/^alias\.//' -e 's/=.*$//'
+}
+
+__git_aliased_command ()
+{
+	local cmdline=$(git repo-config alias.$1)
+	for word in $cmdline; do
+		if [ "${word##-*}" ]; then
+			echo $word
+			return
+		fi
+	done
+}
+
 _git_branch ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -264,10 +281,18 @@ _git ()
 {
 	if [ $COMP_CWORD = 1 ]; then
 		COMPREPLY=($(compgen \
-			-W "--version $(git help -a|egrep '^ ')" \
+			-W "--version $(git help -a|egrep '^ ') \
+			    $(__git_aliases)" \
 			-- "${COMP_WORDS[COMP_CWORD]}"))
 	else
-		case "${COMP_WORDS[1]}" in
+		local command="${COMP_WORDS[1]}"
+		local expansion=$(__git_aliased_command "$command")
+
+		if [ "$expansion" ]; then
+			command="$expansion"
+		fi
+
+		case "$command" in
 		branch)      _git_branch ;;
 		cat-file)    _git_cat_file ;;
 		checkout)    _git_checkout ;;
Junio C Hamano· Oct 28, 2006, 20:37 UTC · re: Dennis Stosberg · lore

Re: [PATCH] Bash completion support for aliases

Dennis Stosberg <dennis@stosberg.net> writes:
>  - Add aliases to the list of available git commands.
>  - Make completion work for aliased commands.
You are my hero.

I started to get annoyed that "git co" does not complete my branch names and I needed to say "git checkout". This fixes it nicely.

← back to recent threads