_git 7.3 KB

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