Skip to contentSkip to conversation

completion: make compatible with zsh

PATCH Superseded Mark Lodato 2 participants 5 messages Jun 15, 2016 2 messages not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 4 lines · Mark Lodato

Modify git-completion.bash to work with both bash and zsh.  Most code works fine, but a few things need tweaking.  Where possible, code is written in such a way that both shells interpret fine, but some areas need special-casing.

Is there any interest in this patch, or anything I could do that would make it more likely to be accepted? Perhaps at least some of these changes could be implemented.

Thanks,
Mark

#2 lore raw
Quoted 9 lines · Mark Lodato
Quoted 4 lines · Mark Lodato

Modify git-completion.bash to work with both bash and zsh.  Most code works fine, but a few things need tweaking.  Where possible, code is written in such a way that both shells interpret fine, but some areas need special-casing.

Is there any interest in this patch, or anything I could do that would make it more likely to be accepted?  Perhaps at least some of these changes could be implemented.

What's the Message-ID for that patch? I can't find it in my mailbox or in a public archive (via Google).

#3 lore raw

PATCH 1/1 completion: make compatible with zsh

Modify git-completion.bash to work with both bash and zsh. Most code works fine, but a few things need tweaking. Where possible, code is written in such a way that both shells interpret fine, but some areas need special-casing.

declare -F
Zsh doesn't have the same 'declare -F' as bash, but 'declare -f' is the same, and it works just as well for our purposes.

${var:2}
Zsh uses $var[3,-1] to trim the first two characters. There is no way to write cross-shell code to accomplish this, so we must wrap this in a check for zsh.

for (( n=1; "$n" ... ))
Zsh does not allow "$var" in arithmetic loops. Instead, pre-compute the endpoint and use the variables without $'s or quotes.

shopt
Zsh uses 'setopt', which has a different syntax than 'shopt. Since 'shopt' is used infrequently in git-completion, we provide a bare-bones emulation.

emulate -L bash
KSH_TYPESET
Zsh offers bash emulation, which turns on a set of features to closely resemble bash; in particular, this turns on SH_WORDSPLIT. We also need to set KSH_TYPESET, to fix "local var=$(echo foo bar)" issues.

The last set of options are turned on only in _git and _gitk. Some of the sub-functions may not work correctly if called directly.

---

Quoted 7 lines · Ævar Arnfjörð Bjarmason
Quoted 6 lines · Mark Lodato
Quoted 4 lines · Mark Lodato

Modify git-completion.bash to work with both bash and zsh. Most code works fine, but a few things need tweaking. Where possible, code is written in such a way that both shells interpret fine, but some areas need special-casing.

Quoted 6 lines
Quoted 3 lines

Is there any interest in this patch, or anything I could do that would make it more likely to be accepted? Perhaps at least some of these changes could be implemented.

What's the Message-ID for that patch? I can't find it in my mailbox or in a public archive (via Google).

Thanks for letting me know.

It appears that my initial message didn't go through. I used the --to argument with 'git format-patch', but 'git send-email' didn't seem to parse it properly. It stuck an empty "To:" line at the normal position, and then put an additional "To: [email protected]" at the bottom of the headers. Gmail must not have liked this.

So, here's another shot. I am not too familiar with how the completion works, so the changed parts ought to be tested on both zsh and bash. Any suggestions on how to do so - or results from doing so - would be greatly appreciated.

I tried to make as few changes to the main part of the script as possible, so that zsh compatibility does not get in the way of normal development. If anyone has any suggestions for improvement, please let me know.

If this should be split into separate patches, I can do that. Each change is so small, I thought it would make more sense to roll them all into one.

 contrib/completion/git-completion.bash |   53 +++++++++++++++++++++++++++++---
 1 files changed, 48 insertions(+), 5 deletions(-)
contrib/completion/git-completion.bash +48 -5
index 6756990..dde796e 100755
@@ -137,12 +137,19 @@ __git_ps1_show_upstream ()
 	svn*)
 		# get the upstream from the "git-svn-id: ..." in a commit message
 		# (git-svn uses essentially the same procedure internally)
-		local svn_upstream=($(git log --first-parent -1 \
-					--grep="^git-svn-id: \(${svn_url_pattern:2}\)" 2>/dev/null))
+		local svn_upstream pattern n_stop
+		if [[ -n $ZSH_VERSION ]]; then
+			pattern="$svn_url_pattern[3,-1]"
+		else
+			pattern="${svn_url_pattern:2}"
+		fi
+		svn_upstream=($(git log --first-parent -1 \
+				--grep="^git-svn-id: \($pattern\)" 2>/dev/null))
 		if [[ 0 -ne ${#svn_upstream[@]} ]]; then
 			svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
 			svn_upstream=${svn_upstream%@*}
-			for ((n=1; "$n" <= "${#svn_remote[@]}"; ++n)); do
+			n_stop="${#svn_remote[@]}"
+			for ((n=1; n <= n_stop; ++n)); do
 				svn_upstream=${svn_upstream#${svn_remote[$n]}}
 			done
 
@@ -2339,6 +2346,11 @@ _git ()
 {
 	local i c=1 command __git_dir
 
+	if [[ -n $ZSH_VERSION ]]; then
+		emulate -L bash
+		setopt KSH_TYPESET
+	fi
+
 	while [ $c -lt $COMP_CWORD ]; do
 		i="${COMP_WORDS[c]}"
 		case "$i" in
@@ -2372,17 +2384,22 @@ _git ()
 	fi
 
 	local completion_func="_git_${command//-/_}"
-	declare -F $completion_func >/dev/null && $completion_func && return
+	declare -f $completion_func >/dev/null && $completion_func && return
 
 	local expansion=$(__git_aliased_command "$command")
 	if [ -n "$expansion" ]; then
 		completion_func="_git_${expansion//-/_}"
-		declare -F $completion_func >/dev/null && $completion_func
+		declare -f $completion_func >/dev/null && $completion_func
 	fi
 }
 
 _gitk ()
 {
+	if [[ -n $ZSH_VERSION ]]; then
+		emulate -L bash
+		setopt KSH_TYPESET
+	fi
+
 	__git_has_doubledash && return
 
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -2417,3 +2434,29 @@ if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
 complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
 	|| complete -o default -o nospace -F _git git.exe
 fi
+
+if [[ -z $ZSH_VERSION ]]; then
+	shopt () {
+		local option
+		if [ $# -ne 2 ]; then
+			echo "USAGE: $0 (-q|-s|-u) <option>" >&2
+			return 1
+		fi
+		case "$2" in
+		nullglob)
+			option="$2"
+			;;
+		*)
+			echo "$0: invalid option: $2" >&2
+			return 1
+		esac
+		case "$1" in
+		-q)	setopt | grep -q "$option" ;;
+		-u)	unsetopt "$option" ;;
+		-s)	setopt "$option" ;;
+		*)
+			echo "$0: invalid flag: $1" >&2
+			return 1
+		esac
+	}
+fi
Signature · 1 line
1.7.2.1

A message that is not in the archive (sent off-list or not yet received): 20100814212507.GD2372@burratino

#5 lore raw

[original email was sent only to me]

Quoted 10 lines · Jonathan Nieder
Quoted 4 lines · Mark Lodato

${var:2}
    Zsh uses $var[3,-1] to trim the first two characters.  There is no way to write cross-shell code to accomplish this, so we must wrap this in a check for zsh.

Maybe

       ${var#??}

Good idea. I'll include this in the next version of the patch.

#6 lore raw
In reply to Mark Lodato
Quoted 28 lines · Mark Lodato
Quoted 7 lines · Ævar Arnfjörð Bjarmason
Quoted 6 lines · Mark Lodato
Quoted 4 lines · Mark Lodato

Modify git-completion.bash to work with both bash and zsh.  Most code works fine, but a few things need tweaking.  Where possible, code is written in such a way that both shells interpret fine, but some areas need special-casing.

Quoted 6 lines
Quoted 3 lines

Is there any interest in this patch, or anything I could do that would make it more likely to be accepted?  Perhaps at least some of these changes could be implemented.

What's the Message-ID for that patch? I can't find it in my mailbox or in a public archive (via Google).

Thanks for letting me know.

It appears that my initial message didn't go through.  I used the --to argument with 'git format-patch', but 'git send-email' didn't seem to parse it properly.  It stuck an empty "To:" line at the normal position, and then put an additional "To: [email protected]" at the bottom of the headers.  Gmail must not have liked this.

So, here's another shot.  I am not too familiar with how the completion works, so the changed parts ought to be tested on both zsh and bash. Any suggestions on how to do so - or results from doing so - would be greatly appreciated.

Quoted 8 lines

I tried to make as few changes to the main part of the script as possible, so that zsh compatibility does not get in the way of normal development.  If anyone has any suggestions for improvement, please let me know.

If this should be split into separate patches, I can do that.  Each change is so small, I thought it would make more sense to roll them all into one.

It makes more sense as one patch.

It could use something in the comments at the top mentioning that this is supposed to work on zsh too, otherwise it looks good (but I'm not familiar with this code), it's a pretty minimal change for a big win.