_git 6.9 KB

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