_git 7.4 KB

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