_git 5.5 KB

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