git-flow.plugin.zsh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #!zsh
  2. #
  3. # Installation
  4. # ------------
  5. #
  6. # To achieve git-flow completion nirvana:
  7. #
  8. # 0. Update your zsh's git-completion module to the newest version.
  9. # From here. https://raw.githubusercontent.com/zsh-users/zsh/master/Completion/Unix/Command/_git
  10. #
  11. # 1. Install this file. Either:
  12. #
  13. # a. Place it in your .zshrc:
  14. #
  15. # b. Or, copy it somewhere (e.g. ~/.git-flow-completion.zsh) and put the following line in
  16. # your .zshrc:
  17. #
  18. # source ~/.git-flow-completion.zsh
  19. #
  20. # c. Or, use this file as an oh-my-zsh plugin.
  21. #
  22. #Alias
  23. alias gfl='git flow'
  24. alias gcd='git checkout develop'
  25. alias gch='git checkout hotfix'
  26. alias gcr='git checkout release'
  27. _git-flow ()
  28. {
  29. local curcontext="$curcontext" state line
  30. typeset -A opt_args
  31. _arguments -C \
  32. ':command:->command' \
  33. '*::options:->options'
  34. case $state in
  35. (command)
  36. local -a subcommands
  37. subcommands=(
  38. 'init:Initialize a new git repo with support for the branching model.'
  39. 'feature:Manage your feature branches.'
  40. 'release:Manage your release branches.'
  41. 'hotfix:Manage your hotfix branches.'
  42. 'support:Manage your support branches.'
  43. 'version:Shows version information.'
  44. )
  45. _describe -t commands 'git flow' subcommands
  46. ;;
  47. (options)
  48. case $line[1] in
  49. (init)
  50. _arguments \
  51. -f'[Force setting of gitflow branches, even if already configured]'
  52. ;;
  53. (version)
  54. ;;
  55. (hotfix)
  56. __git-flow-hotfix
  57. ;;
  58. (release)
  59. __git-flow-release
  60. ;;
  61. (feature)
  62. __git-flow-feature
  63. ;;
  64. esac
  65. ;;
  66. esac
  67. }
  68. __git-flow-release ()
  69. {
  70. local curcontext="$curcontext" state line
  71. typeset -A opt_args
  72. _arguments -C \
  73. ':command:->command' \
  74. '*::options:->options'
  75. case $state in
  76. (command)
  77. local -a subcommands
  78. subcommands=(
  79. 'start:Start a new release branch.'
  80. 'finish:Finish a release branch.'
  81. 'list:List all your release branches. (Alias to `git flow release`)'
  82. 'publish: public'
  83. 'track: track'
  84. )
  85. _describe -t commands 'git flow release' subcommands
  86. _arguments \
  87. -v'[Verbose (more) output]'
  88. ;;
  89. (options)
  90. case $line[1] in
  91. (start)
  92. _arguments \
  93. -F'[Fetch from origin before performing finish]'\
  94. ':version:__git_flow_version_list'
  95. ;;
  96. (finish)
  97. _arguments \
  98. -F'[Fetch from origin before performing finish]' \
  99. -s'[Sign the release tag cryptographically]'\
  100. -u'[Use the given GPG-key for the digital signature (implies -s)]'\
  101. -m'[Use the given tag message]'\
  102. -p'[Push to $ORIGIN after performing finish]'\
  103. -k'[Keep branch after performing finish]'\
  104. -n"[Don't tag this release]"\
  105. ':version:__git_flow_version_list'
  106. ;;
  107. (publish)
  108. _arguments \
  109. ':version:__git_flow_version_list'\
  110. ;;
  111. (track)
  112. _arguments \
  113. ':version:__git_flow_version_list'\
  114. ;;
  115. *)
  116. _arguments \
  117. -v'[Verbose (more) output]'
  118. ;;
  119. esac
  120. ;;
  121. esac
  122. }
  123. __git-flow-hotfix ()
  124. {
  125. local curcontext="$curcontext" state line
  126. typeset -A opt_args
  127. _arguments -C \
  128. ':command:->command' \
  129. '*::options:->options'
  130. case $state in
  131. (command)
  132. local -a subcommands
  133. subcommands=(
  134. 'start:Start a new hotfix branch.'
  135. 'finish:Finish a hotfix branch.'
  136. 'list:List all your hotfix branches. (Alias to `git flow hotfix`)'
  137. )
  138. _describe -t commands 'git flow hotfix' subcommands
  139. _arguments \
  140. -v'[Verbose (more) output]'
  141. ;;
  142. (options)
  143. case $line[1] in
  144. (start)
  145. _arguments \
  146. -F'[Fetch from origin before performing finish]'\
  147. ':hotfix:__git_flow_version_list'\
  148. ':branch-name:__git_branch_names'
  149. ;;
  150. (finish)
  151. _arguments \
  152. -F'[Fetch from origin before performing finish]' \
  153. -s'[Sign the release tag cryptographically]'\
  154. -u'[Use the given GPG-key for the digital signature (implies -s)]'\
  155. -m'[Use the given tag message]'\
  156. -p'[Push to $ORIGIN after performing finish]'\
  157. -k'[Keep branch after performing finish]'\
  158. -n"[Don't tag this release]"\
  159. ':hotfix:__git_flow_hotfix_list'
  160. ;;
  161. *)
  162. _arguments \
  163. -v'[Verbose (more) output]'
  164. ;;
  165. esac
  166. ;;
  167. esac
  168. }
  169. __git-flow-feature ()
  170. {
  171. local curcontext="$curcontext" state line
  172. typeset -A opt_args
  173. _arguments -C \
  174. ':command:->command' \
  175. '*::options:->options'
  176. case $state in
  177. (command)
  178. local -a subcommands
  179. subcommands=(
  180. 'start:Start a new feature branch.'
  181. 'finish:Finish a feature branch.'
  182. 'list:List all your feature branches. (Alias to `git flow feature`)'
  183. 'publish: publish'
  184. 'track: track'
  185. 'diff: diff'
  186. 'rebase: rebase'
  187. 'checkout: checkout'
  188. 'pull: pull'
  189. )
  190. _describe -t commands 'git flow feature' subcommands
  191. _arguments \
  192. -v'[Verbose (more) output]'
  193. ;;
  194. (options)
  195. case $line[1] in
  196. (start)
  197. _arguments \
  198. -F'[Fetch from origin before performing finish]'\
  199. ':feature:__git_flow_feature_list'\
  200. ':branch-name:__git_branch_names'
  201. ;;
  202. (finish)
  203. _arguments \
  204. -F'[Fetch from origin before performing finish]' \
  205. -r'[Rebase instead of merge]'\
  206. -k'[Keep branch after performing finish]'\
  207. ':feature:__git_flow_feature_list'
  208. ;;
  209. (publish)
  210. _arguments \
  211. ':feature:__git_flow_feature_list'\
  212. ;;
  213. (track)
  214. _arguments \
  215. ':feature:__git_flow_feature_list'\
  216. ;;
  217. (diff)
  218. _arguments \
  219. ':branch:__git_flow_feature_list'\
  220. ;;
  221. (rebase)
  222. _arguments \
  223. -i'[Do an interactive rebase]' \
  224. ':branch:__git_flow_feature_list'
  225. ;;
  226. (checkout)
  227. _arguments \
  228. ':branch:__git_flow_feature_list'\
  229. ;;
  230. (pull)
  231. _arguments \
  232. ':remote:__git_remotes'\
  233. ':branch:__git_flow_feature_list'
  234. ;;
  235. *)
  236. _arguments \
  237. -v'[Verbose (more) output]'
  238. ;;
  239. esac
  240. ;;
  241. esac
  242. }
  243. __git_flow_version_list ()
  244. {
  245. local expl
  246. declare -a versions
  247. versions=(${${(f)"$(_call_program versions git flow release list 2> /dev/null | tr -d ' |*')"}})
  248. __git_command_successful || return
  249. _wanted versions expl 'version' compadd $versions
  250. }
  251. __git_flow_feature_list ()
  252. {
  253. local expl
  254. declare -a features
  255. features=(${${(f)"$(_call_program features git flow feature list 2> /dev/null | tr -d ' |*')"}})
  256. __git_command_successful || return
  257. _wanted features expl 'feature' compadd $features
  258. }
  259. __git_remotes () {
  260. local expl gitdir remotes
  261. gitdir=$(_call_program gitdir git rev-parse --git-dir 2>/dev/null)
  262. __git_command_successful || return
  263. remotes=(${${(f)"$(_call_program remotes git config --get-regexp '"^remote\..*\.url$"')"}//#(#b)remote.(*).url */$match[1]})
  264. __git_command_successful || return
  265. # TODO: Should combine the two instead of either or.
  266. if (( $#remotes > 0 )); then
  267. _wanted remotes expl remote compadd $* - $remotes
  268. else
  269. _wanted remotes expl remote _files $* - -W "($gitdir/remotes)" -g "$gitdir/remotes/*"
  270. fi
  271. }
  272. __git_flow_hotfix_list ()
  273. {
  274. local expl
  275. declare -a hotfixes
  276. hotfixes=(${${(f)"$(_call_program hotfixes git flow hotfix list 2> /dev/null | tr -d ' |*')"}})
  277. __git_command_successful || return
  278. _wanted hotfixes expl 'hotfix' compadd $hotfixes
  279. }
  280. __git_branch_names () {
  281. local expl
  282. declare -a branch_names
  283. branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/})
  284. __git_command_successful || return
  285. _wanted branch-names expl branch-name compadd $* - $branch_names
  286. }
  287. __git_command_successful () {
  288. if (( ${#pipestatus:#0} > 0 )); then
  289. _message 'not a git repository'
  290. return 1
  291. fi
  292. return 0
  293. }
  294. zstyle ':completion:*:*:git:*' user-commands flow:'description for foo'