Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 1 | #!bash |
| 2 | # |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 3 | # git-flow-completion |
| 4 | # =================== |
| 5 | # |
Justin Hileman | 1a6cd16 | 2010-04-26 13:31:08 -0400 | [diff] [blame] | 6 | # Bash completion support for [git-flow](http://github.com/nvie/gitflow) |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 7 | # |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 8 | # The contained completion routines provide support for completing: |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 9 | # |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 10 | # * git-flow init and version |
| 11 | # * feature, hotfix and release branches |
Justin Hileman | b2fd9a6 | 2010-10-20 21:30:30 -0400 | [diff] [blame] | 12 | # * remote feature, hotfix and release branch names |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 13 | # |
| 14 | # |
| 15 | # Installation |
| 16 | # ------------ |
| 17 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 18 | # To achieve git-flow completion nirvana: |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 19 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 20 | # 0. Install git-completion. |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 21 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 22 | # 1. Install this file. Either: |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 23 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 24 | # a. Place it in a `bash-completion.d` folder: |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 25 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 26 | # * /etc/bash-completion.d |
| 27 | # * /usr/local/etc/bash-completion.d |
| 28 | # * ~/bash-completion.d |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 29 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 30 | # b. Or, copy it somewhere (e.g. ~/.git-flow-completion.sh) and put the following line in |
| 31 | # your .bashrc: |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 32 | # |
Justin Hileman | b20e97a | 2010-04-26 13:12:01 -0400 | [diff] [blame] | 33 | # source ~/.git-flow-completion.sh |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 34 | # |
Justin Hileman | 3e77edb | 2010-08-05 13:40:10 -0400 | [diff] [blame] | 35 | # 2. If you are using Git < 1.7.1: Edit git-completion.sh and add the following line to the giant |
Justin Hileman | d017360 | 2010-08-05 13:18:12 -0400 | [diff] [blame] | 36 | # $command case in _git: |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 37 | # |
| 38 | # flow) _git_flow ;; |
| 39 | # |
| 40 | # |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 41 | # The Fine Print |
| 42 | # -------------- |
| 43 | # |
Justin Hileman | 821fa74 | 2011-04-29 16:10:36 -0400 | [diff] [blame] | 44 | # Copyright (c) 2011 [Justin Hileman](http://justinhileman.com) |
Justin Hileman | fd37e37 | 2010-04-26 13:30:18 -0400 | [diff] [blame] | 45 | # |
| 46 | # Distributed under the [MIT License](http://creativecommons.org/licenses/MIT/) |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 47 | |
| 48 | _git_flow () |
| 49 | { |
Milan Mitrović | 88a6987 | 2011-11-18 23:47:47 +0100 | [diff] [blame] | 50 | local subcommands="init feature release hotfix support help version" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 51 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 52 | if [ -z "$subcommand" ]; then |
| 53 | __gitcomp "$subcommands" |
| 54 | return |
| 55 | fi |
| 56 | |
| 57 | case "$subcommand" in |
Justin Hileman | b2fd9a6 | 2010-10-20 21:30:30 -0400 | [diff] [blame] | 58 | init) |
| 59 | __git_flow_init |
| 60 | return |
| 61 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 62 | feature) |
| 63 | __git_flow_feature |
| 64 | return |
| 65 | ;; |
| 66 | release) |
| 67 | __git_flow_release |
| 68 | return |
| 69 | ;; |
| 70 | hotfix) |
| 71 | __git_flow_hotfix |
| 72 | return |
| 73 | ;; |
Milan Mitrović | 88a6987 | 2011-11-18 23:47:47 +0100 | [diff] [blame] | 74 | support) |
| 75 | __git_flow_support |
| 76 | return |
| 77 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 78 | *) |
| 79 | COMPREPLY=() |
| 80 | ;; |
| 81 | esac |
| 82 | } |
| 83 | |
Justin Hileman | b2fd9a6 | 2010-10-20 21:30:30 -0400 | [diff] [blame] | 84 | __git_flow_init () |
| 85 | { |
| 86 | local subcommands="help" |
| 87 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
| 88 | if [ -z "$subcommand" ]; then |
| 89 | __gitcomp "$subcommands" |
| 90 | return |
| 91 | fi |
| 92 | } |
| 93 | |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 94 | __git_flow_feature () |
| 95 | { |
Justin Hileman | b2fd9a6 | 2010-10-20 21:30:30 -0400 | [diff] [blame] | 96 | local subcommands="list start finish publish track diff rebase checkout pull help" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 97 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 98 | if [ -z "$subcommand" ]; then |
| 99 | __gitcomp "$subcommands" |
| 100 | return |
| 101 | fi |
| 102 | |
| 103 | case "$subcommand" in |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 104 | pull) |
| 105 | __gitcomp "$(__git_remotes)" |
| 106 | return |
| 107 | ;; |
| 108 | checkout|finish|diff|rebase) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 109 | __gitcomp "$(__git_flow_list_branches 'feature')" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 110 | return |
| 111 | ;; |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 112 | publish) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 113 | __gitcomp "$(comm -23 <(__git_flow_list_branches 'feature') <(__git_flow_list_remote_branches 'feature'))" |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 114 | return |
| 115 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 116 | track) |
Justin Hileman | c05e622 | 2010-11-26 16:04:05 -0500 | [diff] [blame] | 117 | __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'feature') <(__git_flow_list_branches 'feature'))" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 118 | return |
| 119 | ;; |
| 120 | *) |
| 121 | COMPREPLY=() |
| 122 | ;; |
| 123 | esac |
| 124 | } |
| 125 | |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 126 | __git_flow_release () |
| 127 | { |
Justin Hileman | a71065b | 2010-10-20 21:34:46 -0400 | [diff] [blame] | 128 | local subcommands="list start finish track publish help" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 129 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 130 | if [ -z "$subcommand" ]; then |
| 131 | __gitcomp "$subcommands" |
| 132 | return |
| 133 | fi |
| 134 | |
| 135 | case "$subcommand" in |
| 136 | finish) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 137 | __gitcomp "$(__git_flow_list_branches 'release')" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 138 | return |
| 139 | ;; |
Justin Hileman | a71065b | 2010-10-20 21:34:46 -0400 | [diff] [blame] | 140 | publish) |
| 141 | __gitcomp "$(comm -23 <(__git_flow_list_branches 'release') <(__git_flow_list_remote_branches 'release'))" |
| 142 | return |
| 143 | ;; |
| 144 | track) |
Justin Hileman | c05e622 | 2010-11-26 16:04:05 -0500 | [diff] [blame] | 145 | __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'release') <(__git_flow_list_branches 'release'))" |
Justin Hileman | a71065b | 2010-10-20 21:34:46 -0400 | [diff] [blame] | 146 | return |
| 147 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 148 | *) |
| 149 | COMPREPLY=() |
| 150 | ;; |
| 151 | esac |
| 152 | |
| 153 | } |
| 154 | |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 155 | __git_flow_hotfix () |
| 156 | { |
Justin Hileman | c24e250 | 2010-11-26 16:03:14 -0500 | [diff] [blame] | 157 | local subcommands="list start finish help" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 158 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 159 | if [ -z "$subcommand" ]; then |
| 160 | __gitcomp "$subcommands" |
| 161 | return |
| 162 | fi |
| 163 | |
| 164 | case "$subcommand" in |
| 165 | finish) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 166 | __gitcomp "$(__git_flow_list_branches 'hotfix')" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 167 | return |
| 168 | ;; |
| 169 | *) |
| 170 | COMPREPLY=() |
| 171 | ;; |
| 172 | esac |
| 173 | } |
| 174 | |
Milan Mitrović | 88a6987 | 2011-11-18 23:47:47 +0100 | [diff] [blame] | 175 | __git_flow_support () |
| 176 | { |
| 177 | local subcommands="list start help" |
| 178 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
| 179 | if [ -z "$subcommand" ]; then |
| 180 | __gitcomp "$subcommands" |
| 181 | return |
| 182 | fi |
| 183 | |
| 184 | case "$subcommand" in |
| 185 | *) |
| 186 | COMPREPLY=() |
| 187 | ;; |
| 188 | esac |
| 189 | } |
| 190 | |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 191 | __git_flow_prefix () |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 192 | { |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 193 | case "$1" in |
| 194 | feature|release|hotfix) |
| 195 | git config "gitflow.prefix.$1" 2> /dev/null || echo "$1/" |
| 196 | return |
| 197 | ;; |
| 198 | esac |
| 199 | } |
| 200 | |
| 201 | __git_flow_list_branches () |
| 202 | { |
| 203 | local prefix="$(__git_flow_prefix $1)" |
Austin Matzko | 12cb3bc | 2011-04-29 13:58:36 -0500 | [diff] [blame] | 204 | git branch 2> /dev/null | tr -d ' |*' | grep "^$prefix" | sed s,^$prefix,, | sort |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | __git_flow_list_remote_branches () |
| 208 | { |
| 209 | local prefix="$(__git_flow_prefix $1)" |
| 210 | local origin="$(git config gitflow.origin 2> /dev/null || echo "origin")" |
Austin Matzko | 12cb3bc | 2011-04-29 13:58:36 -0500 | [diff] [blame] | 211 | git branch -r 2> /dev/null | sed "s/^ *//g" | grep "^$origin/$prefix" | sed s,^$origin/$prefix,, | sort |
Justin Hileman | f144b1a | 2010-07-20 17:14:58 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 214 | # alias __git_find_on_cmdline for backwards compatibility |
| 215 | if [ -z "`type -t __git_find_on_cmdline`" ]; then |
| 216 | alias __git_find_on_cmdline=__git_find_subcommand |
| 217 | fi |