blob: c4fefece4b186c058f65743812ac5418f9bc7ccf [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{
Justin Hilemanc24e2502010-11-26 16:03:14 -0500157 local subcommands="list start finish 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 ;;
169 *)
170 COMPREPLY=()
171 ;;
172 esac
173}
174
Milan Mitrović88a69872011-11-18 23:47:47 +0100175__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 Hileman5530efe2010-10-20 21:33:05 -0400191__git_flow_prefix ()
Justin Hilemanc5b58412010-04-26 13:06:11 -0400192{
Justin Hileman5530efe2010-10-20 21:33:05 -0400193 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 Matzko12cb3bc2011-04-29 13:58:36 -0500204 git branch 2> /dev/null | tr -d ' |*' | grep "^$prefix" | sed s,^$prefix,, | sort
Justin Hileman5530efe2010-10-20 21:33:05 -0400205}
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 Matzko12cb3bc2011-04-29 13:58:36 -0500211 git branch -r 2> /dev/null | sed "s/^ *//g" | grep "^$origin/$prefix" | sed s,^$origin/$prefix,, | sort
Justin Hilemanf144b1a2010-07-20 17:14:58 -0400212}
213
Justin Hileman91843f82010-10-05 20:04:28 -0400214# alias __git_find_on_cmdline for backwards compatibility
215if [ -z "`type -t __git_find_on_cmdline`" ]; then
216 alias __git_find_on_cmdline=__git_find_subcommand
217fi