blob: e535ad4d42ffa51f7c902e51819b815055145e74 [file] [log] [blame]
Justin Hilemanc5b58412010-04-26 13:06:11 -04001#!bash
2#
Justin Hilemanfd37e372010-04-26 13:30:18 -04003# git-flow-completion
4# ===================
5#
Justin Hileman1a6cd162010-04-26 13:31:08 -04006# Bash completion support for [git-flow](http://github.com/nvie/gitflow)
Justin Hilemanfd37e372010-04-26 13:30:18 -04007#
Justin Hilemanc5b58412010-04-26 13:06:11 -04008# The contained completion routines provide support for completing:
Justin Hilemanfd37e372010-04-26 13:30:18 -04009#
Justin Hilemanc5b58412010-04-26 13:06:11 -040010# * git-flow init and version
11# * feature, hotfix and release branches
Justin Hilemanb2fd9a62010-10-20 21:30:30 -040012# * remote feature, hotfix and release branch names
Justin Hilemanfd37e372010-04-26 13:30:18 -040013#
14#
15# Installation
16# ------------
17#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040018# To achieve git-flow completion nirvana:
Justin Hilemanfd37e372010-04-26 13:30:18 -040019#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040020# 0. Install git-completion.
Justin Hilemanfd37e372010-04-26 13:30:18 -040021#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040022# 1. Install this file. Either:
Justin Hilemanfd37e372010-04-26 13:30:18 -040023#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040024# a. Place it in a `bash-completion.d` folder:
Justin Hilemanfd37e372010-04-26 13:30:18 -040025#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040026# * /etc/bash-completion.d
27# * /usr/local/etc/bash-completion.d
28# * ~/bash-completion.d
Justin Hilemanfd37e372010-04-26 13:30:18 -040029#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040030# b. Or, copy it somewhere (e.g. ~/.git-flow-completion.sh) and put the following line in
31# your .bashrc:
Justin Hilemanfd37e372010-04-26 13:30:18 -040032#
Justin Hilemanb20e97a2010-04-26 13:12:01 -040033# source ~/.git-flow-completion.sh
Justin Hilemanfd37e372010-04-26 13:30:18 -040034#
Justin Hileman3e77edb2010-08-05 13:40:10 -040035# 2. If you are using Git < 1.7.1: Edit git-completion.sh and add the following line to the giant
Justin Hilemand0173602010-08-05 13:18:12 -040036# $command case in _git:
Justin Hilemanfd37e372010-04-26 13:30:18 -040037#
38# flow) _git_flow ;;
39#
40#
Justin Hilemanfd37e372010-04-26 13:30:18 -040041# The Fine Print
42# --------------
43#
Justin Hileman821fa742011-04-29 16:10:36 -040044# Copyright (c) 2011 [Justin Hileman](http://justinhileman.com)
Justin Hilemanfd37e372010-04-26 13:30:18 -040045#
46# Distributed under the [MIT License](http://creativecommons.org/licenses/MIT/)
Justin Hilemanc5b58412010-04-26 13:06:11 -040047
48_git_flow ()
49{
Milan Mitrović88a69872011-11-18 23:47:47 +010050 local subcommands="init feature release hotfix support help version"
Justin Hileman91843f82010-10-05 20:04:28 -040051 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -040052 if [ -z "$subcommand" ]; then
53 __gitcomp "$subcommands"
54 return
55 fi
56
57 case "$subcommand" in
Justin Hilemanb2fd9a62010-10-20 21:30:30 -040058 init)
59 __git_flow_init
60 return
61 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -040062 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ć88a69872011-11-18 23:47:47 +010074 support)
75 __git_flow_support
76 return
77 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -040078 *)
79 COMPREPLY=()
80 ;;
81 esac
82}
83
Justin Hilemanb2fd9a62010-10-20 21:30:30 -040084__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 Hilemanc5b58412010-04-26 13:06:11 -040094__git_flow_feature ()
95{
Justin Hilemanb2fd9a62010-10-20 21:30:30 -040096 local subcommands="list start finish publish track diff rebase checkout pull help"
Justin Hileman91843f82010-10-05 20:04:28 -040097 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -040098 if [ -z "$subcommand" ]; then
99 __gitcomp "$subcommands"
100 return
101 fi
102
103 case "$subcommand" in
Justin Hileman522c0262010-08-05 13:54:02 -0400104 pull)
105 __gitcomp "$(__git_remotes)"
106 return
107 ;;
108 checkout|finish|diff|rebase)
Justin Hileman5530efe2010-10-20 21:33:05 -0400109 __gitcomp "$(__git_flow_list_branches 'feature')"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400110 return
111 ;;
Justin Hileman522c0262010-08-05 13:54:02 -0400112 publish)
Justin Hileman5530efe2010-10-20 21:33:05 -0400113 __gitcomp "$(comm -23 <(__git_flow_list_branches 'feature') <(__git_flow_list_remote_branches 'feature'))"
Justin Hileman522c0262010-08-05 13:54:02 -0400114 return
115 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -0400116 track)
Justin Hilemanc05e6222010-11-26 16:04:05 -0500117 __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'feature') <(__git_flow_list_branches 'feature'))"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400118 return
119 ;;
120 *)
121 COMPREPLY=()
122 ;;
123 esac
124}
125
Justin Hilemanc5b58412010-04-26 13:06:11 -0400126__git_flow_release ()
127{
Justin Hilemana71065b2010-10-20 21:34:46 -0400128 local subcommands="list start finish track publish help"
Justin Hileman91843f82010-10-05 20:04:28 -0400129 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400130 if [ -z "$subcommand" ]; then
131 __gitcomp "$subcommands"
132 return
133 fi
134
135 case "$subcommand" in
136 finish)
Justin Hileman5530efe2010-10-20 21:33:05 -0400137 __gitcomp "$(__git_flow_list_branches 'release')"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400138 return
139 ;;
Justin Hilemana71065b2010-10-20 21:34:46 -0400140 publish)
141 __gitcomp "$(comm -23 <(__git_flow_list_branches 'release') <(__git_flow_list_remote_branches 'release'))"
142 return
143 ;;
144 track)
Justin Hilemanc05e6222010-11-26 16:04:05 -0500145 __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'release') <(__git_flow_list_branches 'release'))"
Justin Hilemana71065b2010-10-20 21:34:46 -0400146 return
147 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -0400148 *)
149 COMPREPLY=()
150 ;;
151 esac
152
153}
154
Justin Hilemanc5b58412010-04-26 13:06:11 -0400155__git_flow_hotfix ()
156{
Corin Langosch54564f42012-08-06 10:48:35 +0300157 local subcommands="list start finish track publish help"
Justin Hileman91843f82010-10-05 20:04:28 -0400158 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400159 if [ -z "$subcommand" ]; then
160 __gitcomp "$subcommands"
161 return
162 fi
163
164 case "$subcommand" in
165 finish)
Justin Hileman5530efe2010-10-20 21:33:05 -0400166 __gitcomp "$(__git_flow_list_branches 'hotfix')"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400167 return
168 ;;
Corin Langosch54564f42012-08-06 10:48:35 +0300169 publish)
170 __gitcomp "$(comm -23 <(__git_flow_list_branches 'hotfix') <(__git_flow_list_remote_branches 'hotfix'))"
171 return
172 ;;
173 track)
174 __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'hotfix') <(__git_flow_list_branches 'hotfix'))"
175 return
176 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -0400177 *)
178 COMPREPLY=()
179 ;;
180 esac
181}
182
Milan Mitrović88a69872011-11-18 23:47:47 +0100183__git_flow_support ()
184{
185 local subcommands="list start help"
186 local subcommand="$(__git_find_on_cmdline "$subcommands")"
187 if [ -z "$subcommand" ]; then
188 __gitcomp "$subcommands"
189 return
190 fi
191
192 case "$subcommand" in
193 *)
194 COMPREPLY=()
195 ;;
196 esac
197}
198
Justin Hileman5530efe2010-10-20 21:33:05 -0400199__git_flow_prefix ()
Justin Hilemanc5b58412010-04-26 13:06:11 -0400200{
Justin Hileman5530efe2010-10-20 21:33:05 -0400201 case "$1" in
202 feature|release|hotfix)
203 git config "gitflow.prefix.$1" 2> /dev/null || echo "$1/"
204 return
205 ;;
206 esac
207}
208
209__git_flow_list_branches ()
210{
211 local prefix="$(__git_flow_prefix $1)"
Austin Matzko12cb3bc2011-04-29 13:58:36 -0500212 git branch 2> /dev/null | tr -d ' |*' | grep "^$prefix" | sed s,^$prefix,, | sort
Justin Hileman5530efe2010-10-20 21:33:05 -0400213}
214
215__git_flow_list_remote_branches ()
216{
217 local prefix="$(__git_flow_prefix $1)"
218 local origin="$(git config gitflow.origin 2> /dev/null || echo "origin")"
Austin Matzko12cb3bc2011-04-29 13:58:36 -0500219 git branch -r 2> /dev/null | sed "s/^ *//g" | grep "^$origin/$prefix" | sed s,^$origin/$prefix,, | sort
Justin Hilemanf144b1a2010-07-20 17:14:58 -0400220}
221
Justin Hileman91843f82010-10-05 20:04:28 -0400222# alias __git_find_on_cmdline for backwards compatibility
223if [ -z "`type -t __git_find_on_cmdline`" ]; then
224 alias __git_find_on_cmdline=__git_find_subcommand
225fi