2
0

git-extras.plugin.zsh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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_remote_names() {
  46. local expl
  47. declare -a remote_names
  48. remote_names=(${(f)"$(_call_program remotes git remote 2>/dev/null)"})
  49. __gitex_command_successful || return
  50. _wanted remote-names expl remote-name compadd $* - $remote_names
  51. }
  52. __gitex_tag_names() {
  53. local expl
  54. declare -a tag_names
  55. tag_names=(${${(f)"$(_call_program tags git for-each-ref --format='"%(refname)"' refs/tags 2>/dev/null)"}#refs/tags/})
  56. __gitex_command_successful || return
  57. _wanted tag-names expl tag-name compadd $* - $tag_names
  58. }
  59. __gitex_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 2>/dev/null)"}#refs/heads/})
  63. __gitex_command_successful || return
  64. _wanted branch-names expl branch-name compadd $* - $branch_names
  65. }
  66. __gitex_specific_branch_names() {
  67. local expl
  68. declare -a branch_names
  69. branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads/"$1" 2>/dev/null)"}#refs/heads/$1/})
  70. __gitex_command_successful || return
  71. _wanted branch-names expl branch-name compadd - $branch_names
  72. }
  73. __gitex_feature_branch_names() {
  74. __gitex_specific_branch_names 'feature'
  75. }
  76. __gitex_submodule_names() {
  77. local expl
  78. declare -a submodule_names
  79. submodule_names=(${(f)"$(_call_program branchrefs git submodule status | awk '{print $2}')"}) # '
  80. __gitex_command_successful || return
  81. _wanted submodule-names expl submodule-name compadd $* - $submodule_names
  82. }
  83. __gitex_author_names() {
  84. local expl
  85. declare -a author_names
  86. author_names=(${(f)"$(_call_program branchrefs git log --format='%aN' | sort -u)"})
  87. __gitex_command_successful || return
  88. _wanted author-names expl author-name compadd $* - $author_names
  89. }
  90. # subcommands
  91. # new subcommand should be added in alphabetical order
  92. _git-authors() {
  93. _arguments -C \
  94. '(--list -l)'{--list,-l}'[show authors]' \
  95. '--no-email[without email]' \
  96. }
  97. _git-changelog() {
  98. _arguments \
  99. '(-l --list)'{-l,--list}'[list commits]' \
  100. }
  101. _git-clear() {
  102. _arguments \
  103. '(-f --force)'{-f,--force}'[force clear]' \
  104. '(-h --help)'{-h,--help}'[help message]' \
  105. }
  106. _git-coauthor() {
  107. _arguments \
  108. ':co-author[co-author to add]' \
  109. ':co-author-email[email address of co-author to add]'
  110. }
  111. _git-contrib() {
  112. _arguments \
  113. ':author:__gitex_author_names'
  114. }
  115. _git-count() {
  116. _arguments \
  117. '--all[detailed commit count]'
  118. }
  119. _git-create-branch() {
  120. local curcontext=$curcontext state line
  121. _arguments -C \
  122. ': :->command' \
  123. '*:: :->option-or-argument'
  124. case "$state" in
  125. (command)
  126. _arguments \
  127. '(--remote -r)'{--remote,-r}'[setup remote tracking branch]'
  128. ;;
  129. (option-or-argument)
  130. curcontext=${curcontext%:*}-$line[1]:
  131. case $line[1] in
  132. -r|--remote )
  133. _arguments -C \
  134. ':remote-name:__gitex_remote_names'
  135. ;;
  136. esac
  137. esac
  138. }
  139. _git-delete-branch() {
  140. _arguments \
  141. ':branch-name:__gitex_branch_names'
  142. }
  143. _git-delete-squashed-branches() {
  144. _arguments \
  145. ':branch-name:__gitex_branch_names'
  146. }
  147. _git-delete-submodule() {
  148. _arguments \
  149. ':submodule-name:__gitex_submodule_names'
  150. }
  151. _git-delete-tag() {
  152. _arguments \
  153. ':tag-name:__gitex_tag_names'
  154. }
  155. _git-effort() {
  156. _arguments \
  157. '--above[ignore file with less than x commits]'
  158. }
  159. _git-extras() {
  160. local curcontext=$curcontext state line ret=1
  161. declare -A opt_args
  162. _arguments -C \
  163. ': :->command' \
  164. '*:: :->option-or-argument' && ret=0
  165. case $state in
  166. (command)
  167. declare -a commands
  168. commands=(
  169. 'update:update git-extras'
  170. )
  171. _describe -t commands command commands && ret=0
  172. ;;
  173. esac
  174. _arguments \
  175. '(-v --version)'{-v,--version}'[show current version]'
  176. }
  177. _git-feature() {
  178. local curcontext=$curcontext state line ret=1
  179. declare -A opt_args
  180. _arguments -C \
  181. ': :->command' \
  182. '*:: :->option-or-argument' && ret=0
  183. case $state in
  184. (command)
  185. declare -a commands
  186. commands=(
  187. 'finish:merge feature into the current branch'
  188. )
  189. _describe -t commands command commands && ret=0
  190. ;;
  191. (option-or-argument)
  192. curcontext=${curcontext%:*}-$line[1]:
  193. case $line[1] in
  194. (finish)
  195. _arguments -C \
  196. '--squash[Use squash merge]' \
  197. ':branch-name:__gitex_feature_branch_names'
  198. ;;
  199. -r|--remote )
  200. _arguments -C \
  201. ':remote-name:__gitex_remote_names'
  202. ;;
  203. esac
  204. return 0
  205. esac
  206. _arguments \
  207. '(--remote -r)'{--remote,-r}'[setup remote tracking branch]'
  208. }
  209. _git-graft() {
  210. _arguments \
  211. ':src-branch-name:__gitex_branch_names' \
  212. ':dest-branch-name:__gitex_branch_names'
  213. }
  214. _git-guilt() {
  215. _arguments -C \
  216. '(--email -e)'{--email,-e}'[display author emails instead of names]' \
  217. '(--ignore-whitespace -w)'{--ignore-whitespace,-w}'[ignore whitespace only changes]' \
  218. '(--debug -d)'{--debug,-d}'[output debug information]' \
  219. '-h[output usage information]'
  220. }
  221. _git-ignore() {
  222. _arguments -C \
  223. '(--local -l)'{--local,-l}'[show local gitignore]' \
  224. '(--global -g)'{--global,-g}'[show global gitignore]' \
  225. '(--private -p)'{--private,-p}'[show repo gitignore]'
  226. }
  227. _git-info() {
  228. _arguments -C \
  229. '(--color -c)'{--color,-c}'[use color for information titles]' \
  230. '--no-config[do not show list all variables set in config file, along with their values]'
  231. }
  232. _git-merge-into() {
  233. _arguments '--ff-only[merge only fast-forward]'
  234. _arguments \
  235. ':src:__gitex_branch_names' \
  236. ':dest:__gitex_branch_names'
  237. }
  238. _git-missing() {
  239. _arguments \
  240. ':first-branch-name:__gitex_branch_names' \
  241. ':second-branch-name:__gitex_branch_names'
  242. }
  243. _git-release() {
  244. _arguments -C \
  245. '-c[Generates/populates the changelog with all commit message since the last tag.]' \
  246. '-r[The "remote" repository that is destination of a push operation.]' \
  247. '-m[use the custom commit information instead of the default message.]' \
  248. '-s[Create a signed and annotated tag.]' \
  249. '-u[Create a tag, annotated and signed with the given key.]' \
  250. '--semver[If the latest tag in your repo matches the semver format requirement, you could increase part of it as the new release tag.]' \
  251. '--prefix[Add a prefix string to semver to allow more complex tags.]' \
  252. '--no-empty-commit[Avoid creating empty commit if nothing could be committed.]' \
  253. '--[The arguments listed after "--" separator will be passed to pre/post-release hook.]'
  254. }
  255. _git-squash() {
  256. _arguments '--squash-msg[commit with the squashed commit messages]'
  257. _arguments \
  258. ':branch-name:__gitex_branch_names'
  259. }
  260. _git-stamp() {
  261. _arguments -C \
  262. '(--replace -r)'{--replace,-r}'[replace stamps with same id]'
  263. }
  264. _git-standup() {
  265. _arguments -C \
  266. '-a[Specify the author of commits. Use "all" to specify all authors.]' \
  267. '-d[Show history since N days ago]' \
  268. '-D[Specify the date format displayed in commit history]' \
  269. '-f[Fetch commits before showing history]' \
  270. '-g[Display GPG signed info]' \
  271. '-h[Display help message]' \
  272. '-L[Enable the inclusion of symbolic links]' \
  273. '-m[The depth of recursive directory search]' \
  274. '-B[Display the commits in branch groups]'
  275. }
  276. _git-summary() {
  277. _arguments '--line[summarize with lines rather than commits]'
  278. _arguments '--dedup-by-email[remove duplicate users by the email address]'
  279. _arguments '--no-merges[exclude merge commits]'
  280. __gitex_commits
  281. }
  282. _git-undo(){
  283. _arguments -C \
  284. '(--soft -s)'{--soft,-s}'[only rolls back the commit but changes remain un-staged]' \
  285. '(--hard -h)'{--hard,-h}'[wipes your commit(s)]'
  286. }
  287. zstyle -g existing_user_commands ':completion:*:*:git:*' user-commands
  288. zstyle ':completion:*:*:git:*' user-commands $existing_user_commands \
  289. alias:'define, search and show aliases' \
  290. abort:'abort current revert, merge, rebase, or cherry-pick process' \
  291. archive-file:'export the current head of the git repository to an archive' \
  292. authors:'generate authors report' \
  293. browse:'open repo website in browser' \
  294. browse-ci:'open repo CI page in browser' \
  295. bug:'create bug branch' \
  296. bulk:'run bulk commands' \
  297. brv:'list branches sorted by their last commit date'\
  298. changelog:'generate a changelog report' \
  299. chore:'create chore branch' \
  300. clear-soft:'soft clean up a repository' \
  301. clear:'rigorously clean up a repository' \
  302. coauthor:'add a co-author to the last commit' \
  303. commits-since:'show commit logs since some date' \
  304. contrib:'show user contributions' \
  305. count:'show commit count' \
  306. create-branch:'create branches' \
  307. delete-branch:'delete branches' \
  308. delete-merged-branches:'delete merged branches' \
  309. delete-squashed-branches:'delete squashed branches' \
  310. delete-submodule:'delete submodules' \
  311. delete-tag:'delete tags' \
  312. delta:'lists changed files' \
  313. effort:'show effort statistics on file(s)' \
  314. extras:'awesome git utilities' \
  315. feature:'create/merge feature branch' \
  316. force-clone:'overwrite local repositories with clone' \
  317. fork:'fork a repo on GitHub' \
  318. fresh-branch:'create fresh branches' \
  319. gh-pages:'create the GitHub pages branch' \
  320. graft:'merge and destroy a given branch' \
  321. guilt:'calculate change between two revisions' \
  322. ignore-io:'get sample gitignore file' \
  323. ignore:'add .gitignore patterns' \
  324. info:'returns information on current repository' \
  325. local-commits:'list local commits' \
  326. lock:'lock a file excluded from version control' \
  327. locked:'ls files that have been locked' \
  328. magic:'commits everything with a generated message' \
  329. merge-into:'merge one branch into another' \
  330. merge-repo:'merge two repo histories' \
  331. missing:'show commits missing from another branch' \
  332. mr:'checks out a merge request locally' \
  333. obliterate:'rewrite past commits to remove some files' \
  334. paste:'send patches to pastebin sites' \
  335. pr:'checks out a pull request locally' \
  336. psykorebase:'rebase a branch with a merge commit' \
  337. pull-request:'create pull request to GitHub project' \
  338. reauthor:'replace the author and/or committer identities in commits and tags' \
  339. rebase-patch:'rebases a patch' \
  340. refactor:'create refactor branch' \
  341. release:'commit, tag and push changes to the repository' \
  342. rename-branch:'rename a branch' \
  343. rename-tag:'rename a tag' \
  344. rename-remote:'rename a remote' \
  345. repl:'git read-eval-print-loop' \
  346. reset-file:'reset one file' \
  347. root:'show path of root' \
  348. scp:'copy files to ssh compatible `git-remote`' \
  349. sed:'replace patterns in git-controlled files' \
  350. setup:'set up a git repository' \
  351. show-merged-branches:'show merged branches' \
  352. show-tree:'show branch tree of commit history' \
  353. show-unmerged-branches:'show unmerged branches' \
  354. squash:'import changes from a branch' \
  355. stamp:'stamp the last commit message' \
  356. standup:'recall the commit history' \
  357. summary:'show repository summary' \
  358. sync:'sync local branch with remote branch' \
  359. touch:'touch and add file to the index' \
  360. undo:'remove latest commits' \
  361. unlock:'unlock a file excluded from version control' \
  362. utimes:'change files modification time to their last commit date'