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