.git-completion.zsh 7.0 KB

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