git-extras.plugin.zsh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #compdef git
  2. # ------------------------------------------------------------------------------
  3. # Description
  4. # -----------
  5. #
  6. # Completion script for git-extras (http://github.com/tj/git-extras).
  7. #
  8. # ------------------------------------------------------------------------------
  9. # Authors
  10. # -------
  11. #
  12. # * Alexis GRIMALDI (https://github.com/agrimaldi)
  13. # * spacewander (https://github.com/spacewander)
  14. #
  15. # ------------------------------------------------------------------------------
  16. # Inspirations
  17. # -----------
  18. #
  19. # * git-extras (http://github.com/tj/git-extras)
  20. # * git-flow-completion (http://github.com/bobthecow/git-flow-completion)
  21. #
  22. # ------------------------------------------------------------------------------
  23. __git_command_successful () {
  24. if (( ${#pipestatus:#0} > 0 )); then
  25. _message 'not a git repository'
  26. return 1
  27. fi
  28. return 0
  29. }
  30. __git_commits() {
  31. declare -A commits
  32. git log --oneline -15 | sed 's/\([[:alnum:]]\{7\}\) /\1:/' | while read commit
  33. do
  34. hash=$(echo $commit | cut -d':' -f1)
  35. commits[$hash]="$commit"
  36. done
  37. local ret=1
  38. _describe -t commits commit commits && ret=0
  39. }
  40. __git_tag_names() {
  41. local expl
  42. declare -a tag_names
  43. tag_names=(${${(f)"$(_call_program tags git for-each-ref --format='"%(refname)"' refs/tags 2>/dev/null)"}#refs/tags/})
  44. __git_command_successful || return
  45. _wanted tag-names expl tag-name compadd $* - $tag_names
  46. }
  47. __git_branch_names() {
  48. local expl
  49. declare -a branch_names
  50. branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/})
  51. __git_command_successful || return
  52. _wanted branch-names expl branch-name compadd $* - $branch_names
  53. }
  54. __git_specific_branch_names() {
  55. local expl
  56. declare -a branch_names
  57. branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads/"$1" 2>/dev/null)"}#refs/heads/$1/})
  58. __git_command_successful || return
  59. _wanted branch-names expl branch-name compadd $* - $branch_names
  60. }
  61. __git_feature_branch_names() {
  62. __git_specific_branch_names 'feature'
  63. }
  64. __git_refactor_branch_names() {
  65. __git_specific_branch_names 'refactor'
  66. }
  67. __git_bug_branch_names() {
  68. __git_specific_branch_names 'bug'
  69. }
  70. __git_submodule_names() {
  71. local expl
  72. declare -a submodule_names
  73. submodule_names=(${(f)"$(_call_program branchrefs git submodule status | awk '{print $2}')"})
  74. __git_command_successful || return
  75. _wanted submodule-names expl submodule-name compadd $* - $submodule_names
  76. }
  77. __git_author_names() {
  78. local expl
  79. declare -a author_names
  80. author_names=(${(f)"$(_call_program branchrefs git log --format='%aN' | sort -u)"})
  81. __git_command_successful || return
  82. _wanted author-names expl author-name compadd $* - $author_names
  83. }
  84. # subcommands
  85. _git-bug() {
  86. local curcontext=$curcontext state line ret=1
  87. declare -A opt_args
  88. _arguments -C \
  89. ': :->command' \
  90. '*:: :->option-or-argument' && ret=0
  91. case $state in
  92. (command)
  93. declare -a commands
  94. commands=(
  95. 'finish:merge bug into the current branch'
  96. )
  97. _describe -t commands command commands && ret=0
  98. ;;
  99. (option-or-argument)
  100. curcontext=${curcontext%:*}-$line[1]:
  101. case $line[1] in
  102. (finish)
  103. _arguments -C \
  104. ':branch-name:__git_bug_branch_names'
  105. ;;
  106. esac
  107. esac
  108. }
  109. _git-changelog() {
  110. _arguments \
  111. '(-l --list)'{-l,--list}'[list commits]' \
  112. }
  113. _git-contrib() {
  114. _arguments \
  115. ':author:__git_author_names'
  116. }
  117. _git-count() {
  118. _arguments \
  119. '--all[detailed commit count]'
  120. }
  121. _git-delete-branch() {
  122. _arguments \
  123. ':branch-name:__git_branch_names'
  124. }
  125. _git-delete-submodule() {
  126. _arguments \
  127. ':submodule-name:__git_submodule_names'
  128. }
  129. _git-delete-tag() {
  130. _arguments \
  131. ':tag-name:__git_tag_names'
  132. }
  133. _git-effort() {
  134. _arguments \
  135. '--above[ignore file with less than x commits]'
  136. }
  137. _git-extras() {
  138. local curcontext=$curcontext state line ret=1
  139. declare -A opt_args
  140. _arguments -C \
  141. ': :->command' \
  142. '*:: :->option-or-argument' && ret=0
  143. case $state in
  144. (command)
  145. declare -a commands
  146. commands=(
  147. 'update:update git-extras'
  148. )
  149. _describe -t commands command commands && ret=0
  150. ;;
  151. esac
  152. _arguments \
  153. '(-v --version)'{-v,--version}'[show current version]'
  154. }
  155. _git-feature() {
  156. local curcontext=$curcontext state line ret=1
  157. declare -A opt_args
  158. _arguments -C \
  159. ': :->command' \
  160. '*:: :->option-or-argument' && ret=0
  161. case $state in
  162. (command)
  163. declare -a commands
  164. commands=(
  165. 'finish:merge feature into the current branch'
  166. )
  167. _describe -t commands command commands && ret=0
  168. ;;
  169. (option-or-argument)
  170. curcontext=${curcontext%:*}-$line[1]:
  171. case $line[1] in
  172. (finish)
  173. _arguments -C \
  174. ':branch-name:__git_feature_branch_names'
  175. ;;
  176. esac
  177. esac
  178. }
  179. _git-graft() {
  180. _arguments \
  181. ':src-branch-name:__git_branch_names' \
  182. ':dest-branch-name:__git_branch_names'
  183. }
  184. _git-ignore() {
  185. _arguments -C \
  186. '(--local -l)'{--local,-l}'[show local gitignore]' \
  187. '(--global -g)'{--global,-g}'[show global gitignore]'
  188. }
  189. _git-missing() {
  190. _arguments \
  191. ':first-branch-name:__git_branch_names' \
  192. ':second-branch-name:__git_branch_names'
  193. }
  194. _git-refactor() {
  195. local curcontext=$curcontext state line ret=1
  196. declare -A opt_args
  197. _arguments -C \
  198. ': :->command' \
  199. '*:: :->option-or-argument' && ret=0
  200. case $state in
  201. (command)
  202. declare -a commands
  203. commands=(
  204. 'finish:merge refactor into the current branch'
  205. )
  206. _describe -t commands command commands && ret=0
  207. ;;
  208. (option-or-argument)
  209. curcontext=${curcontext%:*}-$line[1]:
  210. case $line[1] in
  211. (finish)
  212. _arguments -C \
  213. ':branch-name:__git_refactor_branch_names'
  214. ;;
  215. esac
  216. esac
  217. }
  218. _git-squash() {
  219. _arguments \
  220. ':branch-name:__git_branch_names'
  221. }
  222. _git-summary() {
  223. _arguments '--line[summarize with lines other than commits]'
  224. __git_commits
  225. }
  226. _git-undo(){
  227. _arguments -C \
  228. '(--soft -s)'{--soft,-s}'[only rolls back the commit but changes remain un-staged]' \
  229. '(--hard -h)'{--hard,-h}'[wipes your commit(s)]'
  230. }
  231. zstyle ':completion:*:*:git:*' user-commands \
  232. alias:'define, search and show aliases' \
  233. archive-file:'export the current HEAD of the git repository to a archive' \
  234. back:'undo and stage latest commits' \
  235. bug:'create a bug branch' \
  236. changelog:'populate changelog file with commits since the previous tag' \
  237. commits-since:'list commits since a given date' \
  238. contrib:'display author contributions' \
  239. count:'count commits' \
  240. create-branch:'create local and remote branch' \
  241. delete-branch:'delete local and remote branch' \
  242. delete-merged-brancees:'delete merged branches'\
  243. delete-submodule:'delete submodule' \
  244. delete-tag:'delete local and remote tag' \
  245. effort:'display effort statistics' \
  246. extras:'git-extras' \
  247. feature:'create a feature branch' \
  248. fork:'fork a repo on github' \
  249. fresh-branch:'create empty local branch' \
  250. gh-pages:'create the GitHub Pages branch' \
  251. graft:'merge commits from source branch to destination branch' \
  252. ignore:'add patterns to .gitignore' \
  253. info:'show info about the repository' \
  254. local-commits:'list unpushed commits on the local branch' \
  255. lock:'lock a file excluded from version control' \
  256. locked:'ls files that have been locked' \
  257. missing:'show commits missing from another branch' \
  258. pr:'checks out a pull request locally' \
  259. rebase-patch:'rebases a patch' \
  260. refactor:'create a refactor branch' \
  261. release:'commit, tag and push changes to the repository' \
  262. rename-tag:'rename a tag' \
  263. repl:'read-eval-print-loop' \
  264. reset-file:'reset one file' \
  265. root:'show path of root' \
  266. setup:'setup a git repository' \
  267. show-tree:'show branch tree of commit history' \
  268. squash:'merge commits from source branch into the current one as a single commit' \
  269. summary:'repository summary' \
  270. touch:'one step creation of new files' \
  271. undo:'remove the latest commit' \
  272. unlock:'unlock a file excluded from version control'