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 | # |
| 44 | # Copyright (c) 2010 [Justin Hileman](http://justinhileman.com) |
| 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 | { |
Justin Hileman | b4e11fc | 2010-10-20 21:38:03 -0400 | [diff] [blame] | 50 | local subcommands="init feature release hotfix 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 | ;; |
| 74 | *) |
| 75 | COMPREPLY=() |
| 76 | ;; |
| 77 | esac |
| 78 | } |
| 79 | |
Justin Hileman | b2fd9a6 | 2010-10-20 21:30:30 -0400 | [diff] [blame] | 80 | __git_flow_init () |
| 81 | { |
| 82 | local subcommands="help" |
| 83 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
| 84 | if [ -z "$subcommand" ]; then |
| 85 | __gitcomp "$subcommands" |
| 86 | return |
| 87 | fi |
| 88 | } |
| 89 | |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 90 | __git_flow_feature () |
| 91 | { |
Justin Hileman | b2fd9a6 | 2010-10-20 21:30:30 -0400 | [diff] [blame] | 92 | local subcommands="list start finish publish track diff rebase checkout pull help" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 93 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 94 | if [ -z "$subcommand" ]; then |
| 95 | __gitcomp "$subcommands" |
| 96 | return |
| 97 | fi |
| 98 | |
| 99 | case "$subcommand" in |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 100 | pull) |
| 101 | __gitcomp "$(__git_remotes)" |
| 102 | return |
| 103 | ;; |
| 104 | checkout|finish|diff|rebase) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 105 | __gitcomp "$(__git_flow_list_branches 'feature')" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 106 | return |
| 107 | ;; |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 108 | publish) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 109 | __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] | 110 | return |
| 111 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 112 | track) |
Justin Hileman | c05e622 | 2010-11-26 16:04:05 -0500 | [diff] [blame] | 113 | __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] | 114 | return |
| 115 | ;; |
| 116 | *) |
| 117 | COMPREPLY=() |
| 118 | ;; |
| 119 | esac |
| 120 | } |
| 121 | |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 122 | __git_flow_release () |
| 123 | { |
Justin Hileman | a71065b | 2010-10-20 21:34:46 -0400 | [diff] [blame] | 124 | local subcommands="list start finish track publish help" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 125 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 126 | if [ -z "$subcommand" ]; then |
| 127 | __gitcomp "$subcommands" |
| 128 | return |
| 129 | fi |
| 130 | |
| 131 | case "$subcommand" in |
| 132 | finish) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 133 | __gitcomp "$(__git_flow_list_branches 'release')" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 134 | return |
| 135 | ;; |
Justin Hileman | a71065b | 2010-10-20 21:34:46 -0400 | [diff] [blame] | 136 | publish) |
| 137 | __gitcomp "$(comm -23 <(__git_flow_list_branches 'release') <(__git_flow_list_remote_branches 'release'))" |
| 138 | return |
| 139 | ;; |
| 140 | track) |
Justin Hileman | c05e622 | 2010-11-26 16:04:05 -0500 | [diff] [blame] | 141 | __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] | 142 | return |
| 143 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 144 | *) |
| 145 | COMPREPLY=() |
| 146 | ;; |
| 147 | esac |
| 148 | |
| 149 | } |
| 150 | |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 151 | __git_flow_hotfix () |
| 152 | { |
Justin Hileman | c24e250 | 2010-11-26 16:03:14 -0500 | [diff] [blame] | 153 | local subcommands="list start finish help" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 154 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 155 | if [ -z "$subcommand" ]; then |
| 156 | __gitcomp "$subcommands" |
| 157 | return |
| 158 | fi |
| 159 | |
| 160 | case "$subcommand" in |
| 161 | finish) |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 162 | __gitcomp "$(__git_flow_list_branches 'hotfix')" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 163 | return |
| 164 | ;; |
| 165 | *) |
| 166 | COMPREPLY=() |
| 167 | ;; |
| 168 | esac |
| 169 | } |
| 170 | |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 171 | __git_flow_prefix () |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 172 | { |
Justin Hileman | 5530efe | 2010-10-20 21:33:05 -0400 | [diff] [blame] | 173 | case "$1" in |
| 174 | feature|release|hotfix) |
| 175 | git config "gitflow.prefix.$1" 2> /dev/null || echo "$1/" |
| 176 | return |
| 177 | ;; |
| 178 | esac |
| 179 | } |
| 180 | |
| 181 | __git_flow_list_branches () |
| 182 | { |
| 183 | local prefix="$(__git_flow_prefix $1)" |
| 184 | git branch 2> /dev/null | tr -d ' |*' | grep "^$prefix" | sed s,^$prefix,, |
| 185 | } |
| 186 | |
| 187 | __git_flow_list_remote_branches () |
| 188 | { |
| 189 | local prefix="$(__git_flow_prefix $1)" |
| 190 | local origin="$(git config gitflow.origin 2> /dev/null || echo "origin")" |
| 191 | git branch -r 2> /dev/null | sed "s/^ *//g" | grep "^$origin/$prefix" | sed s,^$origin/$prefix,, |
Justin Hileman | f144b1a | 2010-07-20 17:14:58 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 194 | # alias __git_find_on_cmdline for backwards compatibility |
| 195 | if [ -z "`type -t __git_find_on_cmdline`" ]; then |
| 196 | alias __git_find_on_cmdline=__git_find_subcommand |
| 197 | fi |