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

[PATCH 1/2] bash: Enable completion for external subcommands

From
Teemu Matilainen <teemu.matilainen@iki.fi>
Date
Feb 23, 2010, 14:43 UTC
Message-ID
<1266936193-10644-1-git-send-email-teemu.matilainen@iki.fi>

Now the parameter completion is only available for subcommands delivered with git. Providers of external subcommands do not have a way to supply bash completion for their commands (other than instructing users to hack their git-completion.bash file).

This makes it possible to have completion also for external git subcommands. It can be provided by specifying a function (or a command in PATH) '_git_<subcommand>' that sets the environment variable COMPREPLY.

All dashes (-) in the subcommand name are replaced with underscores (_). E.g. completion for command 'git foo-bar' can be provided by '_git_foo_bar'.

Signed-off-by: Teemu Matilainen <teemu.matilainen@iki.fi>
---
 contrib/completion/git-completion.bash |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index fe93747..c7ac727 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -14,6 +14,9 @@
 #    *) git 'subcommands'
 #    *) tree paths within 'ref:path/to/file' expressions
 #    *) common --long-options
+#    *) completion for external 'git <sub-command>' can be provided by
+#       specifying function (or command in PATH) '_git_<sub_command>'
+#       that sets COMPREPLY
 #
 # To use these routines:
 #
@@ -2257,7 +2260,10 @@ _git ()
 	svn)         _git_svn ;;
 	tag)         _git_tag ;;
 	whatchanged) _git_log ;;
-	*)           COMPREPLY=() ;;
+	*)
+		local f="_git_${command//-/_}"
+		type -t "$f" >/dev/null && "$f" || COMPREPLY=()
+		;;
 	esac
 }
 
-- 
1.7.0.83.g241b9
Next: Teemu Matilainen
Message 1 of 4 in “bash: Enable completion for external subcommands”
  1. 1/2 bash: Enable completion for external subcommandsTeemu Matilainen, Feb 23, 2010
  2. 2/2 bash: Use dynamic binding for subcommand completion functionsTeemu Matilainen, Feb 23, 2010
  3. SZEDER GáborFeb 23, 2010
  4. Teemu MatilainenFeb 23, 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.