You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
6.9 KiB

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