_git 6.8 KB

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