_git 5.9 KB

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