git-extras.plugin.zsh 9.0 KB

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