{"thread":{"id":"25002","subject":"[PATCHv3] completion: make compatible with zsh","startedAt":"2010-09-06T12:33:19Z","lastAt":"2010-09-06T12:33:19Z","messageCount":1,"participants":["Mark Lodato"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"150079","messageId":"1283776399-4632-1-git-send-email-lodatom@gmail.com","threadId":"25002","inReplyTo":null,"subject":"[PATCHv3] completion: make compatible with zsh","fromName":"Mark Lodato","fromEmail":"lodatom@gmail.com","sentAt":"2010-09-06T12:33:19Z","receivedAt":"2010-09-06T12:33:19Z","isPatch":false,"sender":{"key":"lodatom@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58860?v=4"},"body":"Modify git-completion.bash so that it also works with zsh when using\nbashcompinit.  In particular:\n\ndeclare -F\n    Zsh doesn't have the same 'declare -F' as bash, but 'declare -f'\n    is the same, and it works just as well for our purposes.\n\n${var:2}\n    Zsh does not implement ${var:2} to skip the first 2 characters, but\n    ${var#??} works in both shells to replace the first 2 characters\n    with nothing.  Thanks to Jonathan Nieder for the suggestion.\n\nfor (( n=1; \"$n\" ... ))\n    Zsh does not allow \"$var\" in arithmetic loops.  Instead, pre-compute\n    the endpoint and use the variables without $'s or quotes.\n\nshopt\n    Zsh uses 'setopt', which has a different syntax than 'shopt'.  Since\n    'shopt' is used infrequently in git-completion, we provide\n    a bare-bones emulation.\n\nemulate -L bash\nKSH_TYPESET\n    Zsh offers bash emulation, which turns on a set of features to\n    closely resemble bash. In particular, this enables SH_WORDSPLIT,\n    which splits scalar variables on word boundaries in 'for' loops.\n    We also need to set KSH_TYPESET, to fix \"local var=$(echo foo bar)\"\n    issues.\n\nThe last set of options are turned on only in _git and _gitk.  Some of\nthe sub-functions may not work correctly if called directly.\n\nSigned-off-by: Mark Lodato <lodatom@gmail.com>\n---\n\nI think this is now ready for inclusion.  The only change from PATCHv2\nis that \"-z\" was changed to \"-n\" in the final test for ZSH_VERSION.\n\nNot sure if I should put this in the actual commit message myself, but\nPATCHv2 was:\n\nReviewed-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n\n\n contrib/completion/git-completion.bash |   50 +++++++++++++++++++++++++++++--\n 1 files changed, 46 insertions(+), 4 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 6756990..feab651 100755\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -21,6 +21,11 @@\n #    2) Added the following line to your .bashrc:\n #        source ~/.git-completion.sh\n #\n+#       Or, add the following lines to your .zshrc:\n+#        autoload bashcompinit\n+#        bashcompinit\n+#        source ~/.git-completion.sh\n+#\n #    3) Consider changing your PS1 to also show the current branch:\n #        PS1='[\\u@\\h \\W$(__git_ps1 \" (%s)\")]\\$ '\n #\n@@ -138,11 +143,12 @@ __git_ps1_show_upstream ()\n \t\t# get the upstream from the \"git-svn-id: ...\" in a commit message\n \t\t# (git-svn uses essentially the same procedure internally)\n \t\tlocal svn_upstream=($(git log --first-parent -1 \\\n-\t\t\t\t\t--grep=\"^git-svn-id: \\(${svn_url_pattern:2}\\)\" 2>/dev/null))\n+\t\t\t\t\t--grep=\"^git-svn-id: \\(${svn_url_pattern#??}\\)\" 2>/dev/null))\n \t\tif [[ 0 -ne ${#svn_upstream[@]} ]]; then\n \t\t\tsvn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}\n \t\t\tsvn_upstream=${svn_upstream%@*}\n-\t\t\tfor ((n=1; \"$n\" <= \"${#svn_remote[@]}\"; ++n)); do\n+\t\t\tlocal n_stop=\"${#svn_remote[@]}\"\n+\t\t\tfor ((n=1; n <= n_stop; ++n)); do\n \t\t\t\tsvn_upstream=${svn_upstream#${svn_remote[$n]}}\n \t\t\tdone\n \n@@ -2339,6 +2345,11 @@ _git ()\n {\n \tlocal i c=1 command __git_dir\n \n+\tif [[ -n $ZSH_VERSION ]]; then\n+\t\temulate -L bash\n+\t\tsetopt KSH_TYPESET\n+\tfi\n+\n \twhile [ $c -lt $COMP_CWORD ]; do\n \t\ti=\"${COMP_WORDS[c]}\"\n \t\tcase \"$i\" in\n@@ -2372,17 +2383,22 @@ _git ()\n \tfi\n \n \tlocal completion_func=\"_git_${command//-/_}\"\n-\tdeclare -F $completion_func >/dev/null && $completion_func && return\n+\tdeclare -f $completion_func >/dev/null && $completion_func && return\n \n \tlocal expansion=$(__git_aliased_command \"$command\")\n \tif [ -n \"$expansion\" ]; then\n \t\tcompletion_func=\"_git_${expansion//-/_}\"\n-\t\tdeclare -F $completion_func >/dev/null && $completion_func\n+\t\tdeclare -f $completion_func >/dev/null && $completion_func\n \tfi\n }\n \n _gitk ()\n {\n+\tif [[ -n $ZSH_VERSION ]]; then\n+\t\temulate -L bash\n+\t\tsetopt KSH_TYPESET\n+\tfi\n+\n \t__git_has_doubledash && return\n \n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n@@ -2417,3 +2433,29 @@ if [ Cygwin = \"$(uname -o 2>/dev/null)\" ]; then\n complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \\\n \t|| complete -o default -o nospace -F _git git.exe\n fi\n+\n+if [[ -n $ZSH_VERSION ]]; then\n+\tshopt () {\n+\t\tlocal option\n+\t\tif [ $# -ne 2 ]; then\n+\t\t\techo \"USAGE: $0 (-q|-s|-u) <option>\" >&2\n+\t\t\treturn 1\n+\t\tfi\n+\t\tcase \"$2\" in\n+\t\tnullglob)\n+\t\t\toption=\"$2\"\n+\t\t\t;;\n+\t\t*)\n+\t\t\techo \"$0: invalid option: $2\" >&2\n+\t\t\treturn 1\n+\t\tesac\n+\t\tcase \"$1\" in\n+\t\t-q)\tsetopt | grep -q \"$option\" ;;\n+\t\t-u)\tunsetopt \"$option\" ;;\n+\t\t-s)\tsetopt \"$option\" ;;\n+\t\t*)\n+\t\t\techo \"$0: invalid flag: $1\" >&2\n+\t\t\treturn 1\n+\t\tesac\n+\t}\n+fi\n-- \n1.7.2.3\n"}]}