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 |
| 12 | # * remote feature branch names (for `git-flow feature track`) |
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 | fad617d | 2010-04-26 17:33:11 -0400 | [diff] [blame] | 50 | local subcommands="init feature release hotfix" |
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 |
| 58 | feature) |
| 59 | __git_flow_feature |
| 60 | return |
| 61 | ;; |
| 62 | release) |
| 63 | __git_flow_release |
| 64 | return |
| 65 | ;; |
| 66 | hotfix) |
| 67 | __git_flow_hotfix |
| 68 | return |
| 69 | ;; |
| 70 | *) |
| 71 | COMPREPLY=() |
| 72 | ;; |
| 73 | esac |
| 74 | } |
| 75 | |
| 76 | __git_flow_feature () |
| 77 | { |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 78 | local subcommands="list start finish publish track diff rebase checkout pull" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 79 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 80 | if [ -z "$subcommand" ]; then |
| 81 | __gitcomp "$subcommands" |
| 82 | return |
| 83 | fi |
| 84 | |
| 85 | case "$subcommand" in |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 86 | pull) |
| 87 | __gitcomp "$(__git_remotes)" |
| 88 | return |
| 89 | ;; |
| 90 | checkout|finish|diff|rebase) |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 91 | __gitcomp "$(__git_flow_list_features)" |
| 92 | return |
| 93 | ;; |
Justin Hileman | 522c026 | 2010-08-05 13:54:02 -0400 | [diff] [blame] | 94 | publish) |
| 95 | __gitcomp "$(comm -23 <(__git_flow_list_features) <(__git_flow_list_remote_features))" |
| 96 | return |
| 97 | ;; |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 98 | track) |
| 99 | __gitcomp "$(__git_flow_list_remote_features)" |
| 100 | return |
| 101 | ;; |
| 102 | *) |
| 103 | COMPREPLY=() |
| 104 | ;; |
| 105 | esac |
| 106 | } |
| 107 | |
| 108 | __git_flow_list_features () |
| 109 | { |
Justin Hileman | 763f0f9 | 2010-08-05 13:24:36 -0400 | [diff] [blame] | 110 | git flow feature list 2> /dev/null | tr -d ' |*' |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | __git_flow_list_remote_features () |
| 114 | { |
Justin Hileman | 289fb03 | 2010-09-27 21:42:16 -0400 | [diff] [blame] | 115 | git branch -r 2> /dev/null | grep "origin/$(__git_flow_feature_prefix)" | awk '{ sub(/^origin\/$(__git_flow_feature_prefix)/, "", $1); print }' |
| 116 | } |
| 117 | |
| 118 | __git_flow_feature_prefix () |
| 119 | { |
| 120 | git config gitflow.prefix.feature 2> /dev/null || echo "feature/" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | __git_flow_release () |
| 124 | { |
| 125 | local subcommands="list start finish" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 126 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 127 | if [ -z "$subcommand" ]; then |
| 128 | __gitcomp "$subcommands" |
| 129 | return |
| 130 | fi |
| 131 | |
| 132 | case "$subcommand" in |
| 133 | finish) |
| 134 | __gitcomp "$(__git_flow_list_releases)" |
| 135 | return |
| 136 | ;; |
| 137 | *) |
| 138 | COMPREPLY=() |
| 139 | ;; |
| 140 | esac |
| 141 | |
| 142 | } |
| 143 | |
| 144 | __git_flow_list_releases () |
| 145 | { |
Justin Hileman | 9189187 | 2010-04-26 17:30:53 -0400 | [diff] [blame] | 146 | git flow release list 2> /dev/null |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | __git_flow_hotfix () |
| 150 | { |
| 151 | local subcommands="list start finish" |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 152 | local subcommand="$(__git_find_on_cmdline "$subcommands")" |
Justin Hileman | c5b5841 | 2010-04-26 13:06:11 -0400 | [diff] [blame] | 153 | if [ -z "$subcommand" ]; then |
| 154 | __gitcomp "$subcommands" |
| 155 | return |
| 156 | fi |
| 157 | |
| 158 | case "$subcommand" in |
| 159 | finish) |
| 160 | __gitcomp "$(__git_flow_list_hotfixes)" |
| 161 | return |
| 162 | ;; |
| 163 | *) |
| 164 | COMPREPLY=() |
| 165 | ;; |
| 166 | esac |
| 167 | } |
| 168 | |
| 169 | __git_flow_list_hotfixes () |
| 170 | { |
Justin Hileman | 9189187 | 2010-04-26 17:30:53 -0400 | [diff] [blame] | 171 | git flow hotfix list 2> /dev/null |
Justin Hileman | f144b1a | 2010-07-20 17:14:58 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Justin Hileman | 91843f8 | 2010-10-05 20:04:28 -0400 | [diff] [blame] | 174 | # alias __git_find_on_cmdline for backwards compatibility |
| 175 | if [ -z "`type -t __git_find_on_cmdline`" ]; then |
| 176 | alias __git_find_on_cmdline=__git_find_subcommand |
| 177 | fi |