git-flow.plugin.zsh 7.8 KB

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