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

[PATCH] Automatically autoload bashcompinit for ZSH, when needed

From
Marius Storm-Olsen <marius@storm-olsen.com>
Date
Apr 26, 2011, 20:28 UTC
Message-ID
<1303849690-20894-1-git-send-email-mstormo@gmail.com>
In-Reply-To
<7vtydkddto.fsf@alter.siamese.dyndns.org>

If bashcompinit has not already been autoloaded, do so automatically, as it is required to properly parse the git-completion file with ZSH.

Signed-off-by: Marius Storm-Olsen <mstormo@gmail.com>
---
 Since I rebased the previous version, I didn't notice that some
 effort had already gone into making git-completion.bash parsable
 with ZSH.
 
 I've therefore dropped everything from the previous patch, and
 rather added some code to automatically do the required steps
 for ZSH, should it not have been done already; as well as
 simplified the 'documentation'.
 
 My appologies for not more closely inspecting the recent changes
 to the file.
 
 contrib/completion/git-completion.bash |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 840ae38..35d1c9b 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1,6 +1,6 @@
 #!bash
 #
-# bash completion support for core Git.
+# bash/zsh completion support for core Git.
 #
 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
@@ -18,16 +18,12 @@
 # To use these routines:
 #
 #    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
-#    2) Added the following line to your .bashrc:
-#        source ~/.git-completion.sh
-#
-#       Or, add the following lines to your .zshrc:
-#        autoload bashcompinit
-#        bashcompinit
+#    2) Added the following line to your .bashrc/.zshrc:
 #        source ~/.git-completion.sh
 #
 #    3) Consider changing your PS1 to also show the current branch:
-#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
+#         Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
+#         ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
 #
 #       The argument to __git_ps1 will be displayed only if you
 #       are currently in a git repository.  The %s token will be
@@ -77,6 +73,13 @@
 #       git@vger.kernel.org
 #
 
+if [[ -n ${ZSH_VERSION-} ]]; then
+	if ! bashcompinit >/dev/null 2>&1; then
+		autoload -U bashcompinit
+		bashcompinit
+	fi
+fi
+
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
-- 
1.7.5.rc2.4.g4d8b3
Previous: Junio C HamanoNext: Matthieu Moy
Message 5 of 11 in “Refactor git-completion to allow ZSH usage of PS1 functions”
  1. Refactor git-completion to allow ZSH usage of PS1 functionsMarius Storm-Olsen, Apr 26, 2011
  2. Junio C HamanoApr 26, 2011
  3. Marius Storm-OlsenApr 26, 2011
  4. Junio C HamanoApr 26, 2011
  5. Automatically autoload bashcompinit for ZSH, when neededMarius Storm-Olsen, Apr 26, 2011
  6. Matthieu MoyApr 26, 2011
  7. Marius Storm-OlsenApr 26, 2011
  8. Junio C HamanoApr 26, 2011
  9. Felipe ContrerasApr 27, 2011
  10. Felipe ContrerasApr 27, 2011
  11. Automatically autoload bashcompinit for ZSH, when neededMarius Storm-Olsen, Apr 27, 2011

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.