_git 5.6 KB

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