1#compdef git gitk 2 3# zsh completion wrapper for git 4# 5# Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com> 6# 7# You need git's bash completion script installed somewhere, by default it 8# would be the location bash-completion uses. 9# 10# If your script is somewhere else, you can configure it on your ~/.zshrc: 11# 12# zstyle ':completion:*:*:git:*' script ~/.git-completion.sh 13# 14# The recommended way to install this script is to copy to '~/.zsh/_git', and 15# then add the following to your ~/.zshrc file: 16# 17# fpath=(~/.zsh $fpath) 18 19complete () 20{ 21# do nothing 22return0 23} 24 25zstyle -T':completion:*:*:git:*' tag-order&& \ 26 zstyle ':completion:*:*:git:*' tag-order'common-commands' 27 28zstyle -s":completion:*:*:git:*"script script 29if[-z"$script"];then 30local -a locations 31local e 32 locations=( 33$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash 34'/etc/bash_completion.d/git'# fedora, old debian 35'/usr/share/bash-completion/completions/git'# arch, ubuntu, new debian 36'/usr/share/bash-completion/git'# gentoo 37) 38for e in$locations;do 39test -f$e&&script="$e"&&break 40done 41fi 42ZSH_VERSION='' . "$script" 43 44__gitcomp () 45{ 46 emulate -L zsh 47 48local cur_="${3-$cur}" 49 50case"$cur_"in 51--*=) 52;; 53*) 54local c IFS=$'\t\n' 55local -a array 56for c in${=1};do 57 c="$c${4-}" 58case$cin 59--*=*|*.) ;; 60*) c="$c";; 61esac 62 array+=("$c") 63done 64 compset -P'*[=:]' 65 compadd -Q -S''-p"${2-}"-a -- array && _ret=0 66;; 67esac 68} 69 70__gitcomp_nl () 71{ 72 emulate -L zsh 73 74local IFS=$'\n' 75 compset -P'*[=:]' 76 compadd -Q -S"${4- }"-p"${2-}"--${=1}&& _ret=0 77} 78 79__gitcomp_nl_append () 80{ 81 emulate -L zsh 82 83local IFS=$'\n' 84 compadd -Q -S"${4- }"-p"${2-}"--${=1}&& _ret=0 85} 86 87__gitcomp_file () 88{ 89 emulate -L zsh 90 91local IFS=$'\n' 92 compset -P'*[=:]' 93 compadd -Q -p"${2-}"-f --${=1}&& _ret=0 94} 95 96__git_zsh_bash_func () 97{ 98 emulate -L ksh 99 100local command=$1 101 102local completion_func="_git_${command//-/_}" 103declare -f$completion_func>/dev/null &&$completion_func&&return 104 105local expansion=$(__git_aliased_command "$command") 106if[-n"$expansion"];then 107 completion_func="_git_${expansion//-/_}" 108declare -f$completion_func>/dev/null &&$completion_func 109fi 110} 111 112__git_zsh_cmd_common () 113{ 114local -a list 115 list=( 116 add:'add file contents to the index' 117 bisect:'find by binary search the change that introduced a bug' 118 branch:'list, create, or delete branches' 119 checkout:'checkout a branch or paths to the working tree' 120 clone:'clone a repository into a new directory' 121 commit:'record changes to the repository' 122diff:'show changes between commits, commit and working tree, etc' 123 fetch:'download objects and refs from another repository' 124grep:'print lines matching a pattern' 125 init:'create an empty Git repository or reinitialize an existing one' 126 log:'show commit logs' 127 merge:'join two or more development histories together' 128mv:'move or rename a file, a directory, or a symlink' 129 pull:'fetch from and merge with another repository or a local branch' 130 push:'update remote refs along with associated objects' 131 rebase:'forward-port local commits to the updated upstream head' 132reset:'reset current HEAD to the specified state' 133rm:'remove files from the working tree and from the index' 134 show:'show various types of objects' 135 status:'show the working tree status' 136 tag:'create, list, delete or verify a tag object signed with GPG') 137 _describe -t common-commands'common commands' list && _ret=0 138} 139 140__git_zsh_cmd_alias () 141{ 142local -a list 143 list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*}) 144 _describe -t alias-commands'aliases' list $* && _ret=0 145} 146 147__git_zsh_cmd_all () 148{ 149local -a list 150 emulate ksh -c __git_compute_all_commands 151 list=(${=__git_all_commands}) 152 _describe -t all-commands'all commands' list && _ret=0 153} 154 155__git_zsh_main () 156{ 157local curcontext="$curcontext" state state_descr line 158 typeset -A opt_args 159local -a orig_words 160 161 orig_words=(${words[@]}) 162 163 _arguments -C \ 164'(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \ 165'(-p --paginate)--no-pager[do not pipe git output into a pager]' \ 166'--git-dir=-[set the path to the repository]: :_directories' \ 167'--bare[treat the repository as a bare repository]' \ 168'(- :)--version[prints the git suite version]' \ 169'--exec-path=-[path to where your core git programs are installed]:: :_directories' \ 170'--html-path[print the path where git''s HTML documentation is installed]' \ 171'--info-path[print the path where the Info files are installed]' \ 172'--man-path[print the manpath (see `man(1)`) for the man pages]' \ 173'--work-tree=-[set the path to the working tree]: :_directories' \ 174'--namespace=-[set the git namespace]' \ 175'--no-replace-objects[do not use replacement refs to replace git objects]' \ 176'(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \ 177'(-): :->command' \ 178'(-)*:: :->arg'&&return 179 180case$statein 181(command) 182 _alternative \ 183'alias-commands:alias:__git_zsh_cmd_alias' \ 184'common-commands:common:__git_zsh_cmd_common' \ 185'all-commands:all:__git_zsh_cmd_all'&& _ret=0 186;; 187(arg) 188local command="${words[1]}" __git_dir 189 190if(( $+opt_args[--bare] ));then 191 __git_dir='.' 192else 193 __git_dir=${opt_args[--git-dir]} 194fi 195 196(( $+opt_args[--help] )) &&command='help' 197 198 words=(${orig_words[@]}) 199 200 __git_zsh_bash_func $command 201;; 202esac 203} 204 205_git () 206{ 207local _ret=1 208local cur cword prev 209 210 cur=${words[CURRENT]} 211 prev=${words[CURRENT-1]} 212let cword=CURRENT-1 213 214if(( $+functions[__${service}_zsh_main] ));then 215 __${service}_zsh_main 216else 217 emulate ksh -c __${service}_main 218fi 219 220let _ret && _default && _ret=0 221return _ret 222} 223 224_git