_git 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.sh
  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_nl ()
  63. {
  64. emulate -L zsh
  65. local IFS=$'\n'
  66. compset -P '*[=:]'
  67. compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
  68. }
  69. __gitcomp_nl_append ()
  70. {
  71. emulate -L zsh
  72. local IFS=$'\n'
  73. compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
  74. }
  75. __gitcomp_file ()
  76. {
  77. emulate -L zsh
  78. local IFS=$'\n'
  79. compset -P '*[=:]'
  80. compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
  81. }
  82. __git_zsh_bash_func ()
  83. {
  84. emulate -L ksh
  85. local command=$1
  86. local completion_func="_git_${command//-/_}"
  87. declare -f $completion_func >/dev/null && $completion_func && return
  88. local expansion=$(__git_aliased_command "$command")
  89. if [ -n "$expansion" ]; then
  90. words[1]=$expansion
  91. completion_func="_git_${expansion//-/_}"
  92. declare -f $completion_func >/dev/null && $completion_func
  93. fi
  94. }
  95. __git_zsh_cmd_common ()
  96. {
  97. local -a list
  98. list=(
  99. add:'add file contents to the index'
  100. bisect:'find by binary search the change that introduced a bug'
  101. branch:'list, create, or delete branches'
  102. checkout:'checkout a branch or paths to the working tree'
  103. clone:'clone a repository into a new directory'
  104. commit:'record changes to the repository'
  105. diff:'show changes between commits, commit and working tree, etc'
  106. fetch:'download objects and refs from another repository'
  107. grep:'print lines matching a pattern'
  108. init:'create an empty Git repository or reinitialize an existing one'
  109. log:'show commit logs'
  110. merge:'join two or more development histories together'
  111. mv:'move or rename a file, a directory, or a symlink'
  112. pull:'fetch from and merge with another repository or a local branch'
  113. push:'update remote refs along with associated objects'
  114. rebase:'forward-port local commits to the updated upstream head'
  115. reset:'reset current HEAD to the specified state'
  116. rm:'remove files from the working tree and from the index'
  117. show:'show various types of objects'
  118. status:'show the working tree status'
  119. tag:'create, list, delete or verify a tag object signed with GPG')
  120. _describe -t common-commands 'common commands' list && _ret=0
  121. }
  122. __git_zsh_cmd_alias ()
  123. {
  124. local -a list
  125. list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
  126. _describe -t alias-commands 'aliases' list $* && _ret=0
  127. }
  128. __git_zsh_cmd_all ()
  129. {
  130. local -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. __git_zsh_main ()
  136. {
  137. local curcontext="$curcontext" state state_descr line
  138. typeset -A opt_args
  139. local -a orig_words
  140. orig_words=( ${words[@]} )
  141. _arguments -C \
  142. '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
  143. '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
  144. '--git-dir=-[set the path to the repository]: :_directories' \
  145. '--bare[treat the repository as a bare repository]' \
  146. '(- :)--version[prints the git suite version]' \
  147. '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
  148. '--html-path[print the path where git''s HTML documentation is installed]' \
  149. '--info-path[print the path where the Info files are installed]' \
  150. '--man-path[print the manpath (see `man(1)`) for the man pages]' \
  151. '--work-tree=-[set the path to the working tree]: :_directories' \
  152. '--namespace=-[set the git namespace]' \
  153. '--no-replace-objects[do not use replacement refs to replace git objects]' \
  154. '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
  155. '(-): :->command' \
  156. '(-)*:: :->arg' && return
  157. case $state in
  158. (command)
  159. _alternative \
  160. 'alias-commands:alias:__git_zsh_cmd_alias' \
  161. 'common-commands:common:__git_zsh_cmd_common' \
  162. 'all-commands:all:__git_zsh_cmd_all' && _ret=0
  163. ;;
  164. (arg)
  165. local command="${words[1]}" __git_dir
  166. if (( $+opt_args[--bare] )); then
  167. __git_dir='.'
  168. else
  169. __git_dir=${opt_args[--git-dir]}
  170. fi
  171. (( $+opt_args[--help] )) && command='help'
  172. words=( ${orig_words[@]} )
  173. __git_zsh_bash_func $command
  174. ;;
  175. esac
  176. }
  177. _git ()
  178. {
  179. local _ret=1
  180. local cur cword prev
  181. cur=${words[CURRENT]}
  182. prev=${words[CURRENT-1]}
  183. let cword=CURRENT-1
  184. if (( $+functions[__${service}_zsh_main] )); then
  185. __${service}_zsh_main
  186. elif (( $+functions[__${service}_main] )); then
  187. emulate ksh -c __${service}_main
  188. elif (( $+functions[_${service}] )); then
  189. emulate ksh -c _${service}
  190. fi
  191. let _ret && _default && _ret=0
  192. return _ret
  193. }
  194. _git