Refactor git-completion to allow ZSH usage of PS1 functions
PATCH 1/1 Refactor git-completion to allow ZSH usage of PS1 functions
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.
---
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_upstreamfunction 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
contrib/completion/git-completion.bash +3 -114
@@ -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 ()
contrib/completion/git-prompt-functions new file +149 -0
149 lines not on this page · raw message
git.spec.in +1 -0
@@ -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
Signature · 1 line
1.7.5.rc2.4.g4d8b3
I'd be very negative on splitting this file into more than one files.
Junio C Hamano said the following on 26.04.2011 12:21:
Quoted 1 line
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.
Signature · 1 line
.marius
Quoted 4 lines · Marius Storm-Olsen
Junio C Hamano said the following on 26.04.2011 12:21:
Quoted 1 line
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.
PATCH 1/1 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.
---
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(-)
contrib/completion/git-completion.bash +11 -8
@@ -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 <[email protected]> # 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 @@ # [email protected] # +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:"
Signature · 1 line
1.7.5.rc2.4.g4d8b3
Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed
Quoted 4 lines · Marius Storm-Olsen
If bashcompinit has not already been autoloaded, do so automatically, as it is required to properly parse the git-completion file with ZSH.
Quoted 8 lines
Signed-off-by Marius Storm-Olsen <[email protected]>---
+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
Signature · 1 line
Felipe Contreras
Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed
Quoted 9 lines · Felipe Contreras
Quoted 6 lines · Marius Storm-Olsen
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 <[email protected]>---
Quoted 8 lines
Quoted 5 lines
+if [[ -n ${ZSH_VERSION-} ]]; then
+ if ! bashcompinit >/dev/null 2>&1; then
+ autoload -U bashcompinit
+ bashcompinit
+ fiHow about this instead?
autoload -UX bashcompinit && bashcompinit
Er, autoload -U +X.
Signature · 1 line
Felipe Contreras
PATCH v2 1/1 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.
---
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(-)
contrib/completion/git-completion.bash +8 -8
@@ -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 <[email protected]> # 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 @@ # [email protected] # +if [[ -n ${ZSH_VERSION-} ]]; then + autoload -U +X bashcompinit && bashcompinit +fi + case "$COMP_WORDBREAKS" in *:*) : great ;; *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
Signature · 1 line
1.7.5.rc2.4.g4d8b3
Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed
In reply to Marius Storm-Olsencontrib/completion/git-completion.bash line 21
Quoted 3 lines · Marius Storm-Olsen
contrib/completion/git-completion.bash +1
-1-# 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"?
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
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!
Signature · 2 lines
.marius @ N900 ----- Original message -----
Quoted 11 lines
contrib/completion/git-completion.bash line 21
Quoted 3 lines · Marius Storm-Olsen
contrib/completion/git-completion.bash +1
-1-# 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"?
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH] Automatically autoload bashcompinit for ZSH, when needed
Quoted 1 line · Marius Storm-Olsen
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.