threads / patch / 27188

patchRefactor git-completion to allow ZSH usage of PS1 functions

Subject: [PATCH] Refactor git-completion to allow ZSH usage of PS1 functions

## tl;dr

11 messages between Apr 26, 2011 and Apr 27, 2011. Diffs are folded; open one to read it.

replies: 10people: 5as markdown or json

Marius Storm-Olsen· Apr 26, 2011, 13:24 UTC · lore

The PS1 functions in git-completion are simple functions which work just as well for ZSH as for Bash. So, refactoring them out allows ZSH users to also use them, 'standardizing' the prompt\ for Git.

The only thing not supported by ZSH is the
    __git_ps1_show_upstream
function, so this functionality is simply forced disabled there.
Signed-off-by: Marius Storm-Olsen <mstormo@gmail.com>
---
 This patch is mainly just moving the two functions
     __gitdir
     __git_ps1
 out into a new file. However, the most "nasty" is the way this
 file is then included from the original git-completion.bash
 file, with
     GIT_COMPLETION_BASH_ONLY=1
     source $(dirname ${BASH_SOURCE[0]})/git-prompt-functions
 where the variable ensures that only Bash will include the
     __git_ps1_show_upstream
 function call, and then the PS1 functions are loaded via a
 dirname'd BASH_SOURCE[0]. I am by no means a Bash guru, so
 others will have to evaluate the compatability of using this
 technique outside of Bash on Linux. Relying on $0 at least,
 does not work.
 Someone will also have to verify the RPM part at the bottom.
 contrib/completion/git-completion.bash  |  117 +-----------------------
 contrib/completion/git-prompt-functions |  149 +++++++++++++++++++++++++++++++
 git.spec.in                             |    1 +
 3 files changed, 153 insertions(+), 114 deletions(-)
 create mode 100755 contrib/completion/git-prompt-functions
Show changes to 3 files +153 −114

contrib/completion/git-completion.bash, contrib/completion/git-prompt-functions, git.spec.in

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 840ae38..417cb0a 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -82,25 +82,6 @@ case "$COMP_WORDBREAKS" in
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
 esac
 
-# __gitdir accepts 0 or 1 arguments (i.e., location)
-# returns location of .git repo
-__gitdir ()
-{
-	if [ -z "${1-}" ]; then
-		if [ -n "${__git_dir-}" ]; then
-			echo "$__git_dir"
-		elif [ -d .git ]; then
-			echo .git
-		else
-			git rev-parse --git-dir 2>/dev/null
-		fi
-	elif [ -d "$1/.git" ]; then
-		echo "$1/.git"
-	else
-		echo "$1"
-	fi
-}
-
 # stores the divergence from upstream in $p
 # used by GIT_PS1_SHOWUPSTREAM
 __git_ps1_show_upstream ()
@@ -220,101 +201,9 @@ __git_ps1_show_upstream ()
 
 }
 
-
-# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
-# returns text to add to bash PS1 prompt (includes branch name)
-__git_ps1 ()
-{
-	local g="$(__gitdir)"
-	if [ -n "$g" ]; then
-		local r=""
-		local b=""
-		if [ -f "$g/rebase-merge/interactive" ]; then
-			r="|REBASE-i"
-			b="$(cat "$g/rebase-merge/head-name")"
-		elif [ -d "$g/rebase-merge" ]; then
-			r="|REBASE-m"
-			b="$(cat "$g/rebase-merge/head-name")"
-		else
-			if [ -d "$g/rebase-apply" ]; then
-				if [ -f "$g/rebase-apply/rebasing" ]; then
-					r="|REBASE"
-				elif [ -f "$g/rebase-apply/applying" ]; then
-					r="|AM"
-				else
-					r="|AM/REBASE"
-				fi
-			elif [ -f "$g/MERGE_HEAD" ]; then
-				r="|MERGING"
-			elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
-				r="|CHERRY-PICKING"
-			elif [ -f "$g/BISECT_LOG" ]; then
-				r="|BISECTING"
-			fi
-
-			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
-
-				b="$(
-				case "${GIT_PS1_DESCRIBE_STYLE-}" in
-				(contains)
-					git describe --contains HEAD ;;
-				(branch)
-					git describe --contains --all HEAD ;;
-				(describe)
-					git describe HEAD ;;
-				(* | default)
-					git describe --tags --exact-match HEAD ;;
-				esac 2>/dev/null)" ||
-
-				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
-				b="unknown"
-				b="($b)"
-			}
-		fi
-
-		local w=""
-		local i=""
-		local s=""
-		local u=""
-		local c=""
-		local p=""
-
-		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
-			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
-				c="BARE:"
-			else
-				b="GIT_DIR!"
-			fi
-		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
-			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
-				if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
-					git diff --no-ext-diff --quiet --exit-code || w="*"
-					if git rev-parse --quiet --verify HEAD >/dev/null; then
-						git diff-index --cached --quiet HEAD -- || i="+"
-					else
-						i="#"
-					fi
-				fi
-			fi
-			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
-			        git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
-			fi
-
-			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
-			   if [ -n "$(git ls-files --others --exclude-standard)" ]; then
-			      u="%"
-			   fi
-			fi
-
-			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
-				__git_ps1_show_upstream
-			fi
-		fi
-
-		local f="$w$i$s$u"
-		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
-	fi
-}
+# load the __git_ps1 functionality
+GIT_COMPLETION_BASH_ONLY=1
+source $(dirname ${BASH_SOURCE[0]})/git-prompt-functions
 
 # __gitcomp_1 requires 2 arguments
 __gitcomp_1 ()
diff --git a/contrib/completion/git-prompt-functions b/contrib/completion/git-prompt-functions
new file mode 100755
index 0000000..89d9449
--- /dev/null
+++ b/contrib/completion/git-prompt-functions
@@ -0,0 +1,149 @@
+#!bash
+#
+# bash/zsh prompt functions for core Git.
+#
+# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
+#
+# This code was originially in git-completion.bash. However,
+# since this code works just fine also for ZSH, it was refactored
+# out, to allow ZSH users to source only the prompt functionality,
+# given that ZSH already comes with git completion of its own.
+#
+# Bash users might consider setting
+#     PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
+# while the ZSH equivalent would be
+#     PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
+#
+# You may set the following values to tweak the output of the
+# __git_ps1() function:
+#
+#  GIT_PS1_SHOWDIRTYSTATE=true
+#     Reports if in a dirty state
+#      '*' means modified file(s)
+#      '+' means added file(s)
+#
+#  GIT_PS1_SHOWSTASHSTATE=true
+#     Reports the current stash state
+#
+#  GIT_PS1_SHOWUNTRACKEDFILES=true
+#     Reports if there are untracked files in the repo
+#
+#  GIT_PS1_SHOWUPSTREAM=auto
+#     Places the divergence of repo against upstream, in variable 'p'
+#     (Use $p in Bash prompts. Not available in ZSH prompts)
+#
+# See git-completion.bash for other details
+
+# __gitdir accepts 0 or 1 arguments (i.e., location)
+# returns location of .git repo
+__gitdir ()
+{
+	if [ -z "${1-}" ]; then
+		if [ -n "${__git_dir-}" ]; then
+			echo "$__git_dir"
+		elif [ -d .git ]; then
+			echo .git
+		else
+			git rev-parse --git-dir 2>/dev/null
+		fi
+	elif [ -d "$1/.git" ]; then
+		echo "$1/.git"
+	else
+		echo "$1"
+	fi
+}
+
+# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
+# returns text to add to bash PS1 prompt (includes branch name)
+__git_ps1 ()
+{
+	local g="$(__gitdir)"
+	if [ -n "$g" ]; then
+		local r=""
+		local b=""
+		if [ -f "$g/rebase-merge/interactive" ]; then
+			r="|REBASE-i"
+			b="$(cat "$g/rebase-merge/head-name")"
+		elif [ -d "$g/rebase-merge" ]; then
+			r="|REBASE-m"
+			b="$(cat "$g/rebase-merge/head-name")"
+		else
+			if [ -d "$g/rebase-apply" ]; then
+				if [ -f "$g/rebase-apply/rebasing" ]; then
+					r="|REBASE"
+				elif [ -f "$g/rebase-apply/applying" ]; then
+					r="|AM"
+				else
+					r="|AM/REBASE"
+				fi
+			elif [ -f "$g/MERGE_HEAD" ]; then
+				r="|MERGING"
+			elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+				r="|CHERRY-PICKING"
+			elif [ -f "$g/BISECT_LOG" ]; then
+				r="|BISECTING"
+			fi
+
+			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
+
+				b="$(
+				case "${GIT_PS1_DESCRIBE_STYLE-}" in
+				(contains)
+					git describe --contains HEAD ;;
+				(branch)
+					git describe --contains --all HEAD ;;
+				(describe)
+					git describe HEAD ;;
+				(* | default)
+					git describe --tags --exact-match HEAD ;;
+				esac 2>/dev/null)" ||
+
+				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
+				b="unknown"
+				b="($b)"
+			}
+		fi
+
+		local w=""
+		local i=""
+		local s=""
+		local u=""
+		local c=""
+		local p=""
+
+		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
+			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
+				c="BARE:"
+			else
+				b="GIT_DIR!"
+			fi
+		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+				if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
+					git diff --no-ext-diff --quiet --exit-code || w="*"
+					if git rev-parse --quiet --verify HEAD >/dev/null; then
+						git diff-index --cached --quiet HEAD -- || i="+"
+					else
+						i="#"
+					fi
+				fi
+			fi
+			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
+				git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
+			fi
+
+			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
+			   if [ -n "$(git ls-files --others --exclude-standard)" ]; then
+			      u="%"
+			   fi
+			fi
+
+			if [ -n "${GIT_COMPLETION_BASH_ONLY-}" ] && [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+				__git_ps1_show_upstream
+			fi
+		fi
+
+		local f="$w$i$s$u"
+		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
+	fi
+}
diff --git a/git.spec.in b/git.spec.in
index 91c8462..c81385e 100644
--- a/git.spec.in
+++ b/git.spec.in
@@ -137,6 +137,7 @@ rm -rf $RPM_BUILD_ROOT%{_mandir}
 
 mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/bash_completion.d
 install -m 644 -T contrib/completion/git-completion.bash $RPM_BUILD_ROOT%{_sysconfdir}/bash_completion.d/git
+install -m 644 -T contrib/completion/git-prompt-functions $RPM_BUILD_ROOT%{_sysconfdir}/bash_completion.d/git-prompt-functions
 
 %clean
 rm -rf $RPM_BUILD_ROOT
-- 
1.7.5.rc2.4.g4d8b3
Marius Storm-Olsen· Apr 26, 2011, 17:37 UTC · re: Junio C Hamano · lore

Re: [PATCH] Refactor git-completion to allow ZSH usage of PS1 functions

Junio C Hamano said the following on 26.04.2011 12:21:
> I'd be very negative on splitting this file into more than one files.
The only other option would be duplication. Is that preferred?

I was hoping to more easily keep the implementations in sync, given that the code is trivial enough for both shells to interpret. However, having two separate implementations would allow for more advanced options in the ZSH PSx handling, I guess.

-- 
.marius
Junio C Hamano· Apr 26, 2011, 19:15 UTC · re: Marius Storm-Olsen · lore

Re: [PATCH] Refactor git-completion to allow ZSH usage of PS1 functions

Marius Storm-Olsen <mstormo@gmail.com> writes:
> Junio C Hamano said the following on 26.04.2011 12:21:
>> I'd be very negative on splitting this file into more than one files.
>
> The only other option would be duplication. Is that preferred?

That is not the answer I was hoping to hear. Can't we think of a way to share without duplication the common parts and have tweaks per two shell implementations?

A conditional that says "Do this part only if I am zsh" in a single file is fine. Makefile that concatenates the common one and specific one among two would also be acceptable, even though it makes things more cumbersome to the user. But no matter what you do, I would really prefer for the user to have to copy just _one_ file, not two.

Marius Storm-Olsen· Apr 26, 2011, 20:28 UTC · re: Junio C Hamano · lore

[PATCH] Automatically autoload bashcompinit for ZSH, when needed

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(-)
Show changes to contrib/completion/git-completion.bash +11 −8
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
Matthieu Moy· Apr 26, 2011, 20:47 UTC · re: Marius Storm-Olsen · lore

Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed

Marius Storm-Olsen <marius@storm-olsen.com> writes:
> -#    2) Added the following line to your .bashrc:
> +#    2) Added the following line to your .bashrc/.zshrc:
While you're there: shouldn't this be "Add", not "Added"?
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Marius Storm-Olsen· Apr 26, 2011, 20:52 UTC · re: Matthieu Moy · lore

Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed

Agreed.
I'm away from my machines right now, so if anyone feels like rerolling the patch with that update, that'd be great.
Or Junio could just fix it up when/if picking it.
If not, I'll reroll later tonight.
Thanks!
-- 
.marius @ N900
----- Original message -----
> Marius Storm-Olsen <marius@storm-olsen.com> writes:
> 
> > -#       2) Added the following line to your .bashrc:
> 
> > +#       2) Added the following line to your .bashrc/.zshrc:
> 
> While you're there: shouldn't this be "Add", not "Added"?
> 
> -- 
> Matthieu Moy
> http://www-verimag.imag.fr/~moy/
Junio C Hamano· Apr 26, 2011, 21:55 UTC · re: Marius Storm-Olsen · lore

Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed

Marius Storm-Olsen <marius@storm-olsen.com> writes:
> Or Junio could just fix it up when/if picking it.
Surely, no problem.

I do not use zsh myself, so unless a real zsh user complains, I'll queue it directly on 'master' as part of the batch for the first week.

Thanks.
Felipe Contreras· Apr 27, 2011, 01:11 UTC · re: Marius Storm-Olsen · lore

Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed

On Tue, Apr 26, 2011 at 11:28 PM, Marius Storm-Olsen <marius@storm-olsen.com> wrote:

Show 6 quoted lines
> 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>
> ---
Show 5 quoted lines
> +if [[ -n ${ZSH_VERSION-} ]]; then
> +       if ! bashcompinit >/dev/null 2>&1; then
> +               autoload -U bashcompinit
> +               bashcompinit
> +       fi

How about this instead? autoload -UX bashcompinit && bashcompinit

-- 
Felipe Contreras
Felipe Contreras· Apr 27, 2011, 01:22 UTC · re: Felipe Contreras · lore

Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed

On Wed, Apr 27, 2011 at 4:11 AM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 17 quoted lines
> On Tue, Apr 26, 2011 at 11:28 PM, Marius Storm-Olsen
> <marius@storm-olsen.com> wrote:
>> 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>
>> ---
>
>> +if [[ -n ${ZSH_VERSION-} ]]; then
>> +       if ! bashcompinit >/dev/null 2>&1; then
>> +               autoload -U bashcompinit
>> +               bashcompinit
>> +       fi
>
> How about this instead?
> autoload -UX bashcompinit && bashcompinit
Er, autoload -U +X.
-- 
Felipe Contreras
Marius Storm-Olsen· Apr 27, 2011, 03:23 UTC · re: Felipe Contreras · lore

[PATCH v2] Automatically autoload bashcompinit for ZSH, when needed

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>
---
 Changes since v2:
 -----------------
 'Added' -> 'Add'
 Simplified test for bashcompinit, as per advice from Felipe Contreras
 contrib/completion/git-completion.bash |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
Show changes to contrib/completion/git-completion.bash +8 −8
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 840ae38..9150ea6 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) Add 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,10 @@
 #       git@vger.kernel.org
 #
 
+if [[ -n ${ZSH_VERSION-} ]]; then
+	autoload -U +X bashcompinit && bashcompinit
+fi
+
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
-- 
1.7.5.rc2.4.g4d8b3

← back to recent threads