_git 5.9 KB

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