blob: 276c30d1b4bbbb699d4a18b4999823f4ce711f84 [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#
44# Copyright (c) 2010 [Justin Hileman](http://justinhileman.com)
45#
46# Distributed under the [MIT License](http://creativecommons.org/licenses/MIT/)
Justin Hilemanc5b58412010-04-26 13:06:11 -040047
48_git_flow ()
49{
Justin Hilemanb4e11fc2010-10-20 21:38:03 -040050 local subcommands="init feature release hotfix 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 ;;
74 *)
75 COMPREPLY=()
76 ;;
77 esac
78}
79
Justin Hilemanb2fd9a62010-10-20 21:30:30 -040080__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 Hilemanc5b58412010-04-26 13:06:11 -040090__git_flow_feature ()
91{
Justin Hilemanb2fd9a62010-10-20 21:30:30 -040092 local subcommands="list start finish publish track diff rebase checkout pull help"
Justin Hileman91843f82010-10-05 20:04:28 -040093 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -040094 if [ -z "$subcommand" ]; then
95 __gitcomp "$subcommands"
96 return
97 fi
98
99 case "$subcommand" in
Justin Hileman522c0262010-08-05 13:54:02 -0400100 pull)
101 __gitcomp "$(__git_remotes)"
102 return
103 ;;
104 checkout|finish|diff|rebase)
Justin Hileman5530efe2010-10-20 21:33:05 -0400105 __gitcomp "$(__git_flow_list_branches 'feature')"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400106 return
107 ;;
Justin Hileman522c0262010-08-05 13:54:02 -0400108 publish)
Justin Hileman5530efe2010-10-20 21:33:05 -0400109 __gitcomp "$(comm -23 <(__git_flow_list_branches 'feature') <(__git_flow_list_remote_branches 'feature'))"
Justin Hileman522c0262010-08-05 13:54:02 -0400110 return
111 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -0400112 track)
Justin Hilemanc05e6222010-11-26 16:04:05 -0500113 __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'feature') <(__git_flow_list_branches 'feature'))"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400114 return
115 ;;
116 *)
117 COMPREPLY=()
118 ;;
119 esac
120}
121
Justin Hilemanc5b58412010-04-26 13:06:11 -0400122__git_flow_release ()
123{
Justin Hilemana71065b2010-10-20 21:34:46 -0400124 local subcommands="list start finish track publish help"
Justin Hileman91843f82010-10-05 20:04:28 -0400125 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400126 if [ -z "$subcommand" ]; then
127 __gitcomp "$subcommands"
128 return
129 fi
130
131 case "$subcommand" in
132 finish)
Justin Hileman5530efe2010-10-20 21:33:05 -0400133 __gitcomp "$(__git_flow_list_branches 'release')"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400134 return
135 ;;
Justin Hilemana71065b2010-10-20 21:34:46 -0400136 publish)
137 __gitcomp "$(comm -23 <(__git_flow_list_branches 'release') <(__git_flow_list_remote_branches 'release'))"
138 return
139 ;;
140 track)
Justin Hilemanc05e6222010-11-26 16:04:05 -0500141 __gitcomp "$(comm -23 <(__git_flow_list_remote_branches 'release') <(__git_flow_list_branches 'release'))"
Justin Hilemana71065b2010-10-20 21:34:46 -0400142 return
143 ;;
Justin Hilemanc5b58412010-04-26 13:06:11 -0400144 *)
145 COMPREPLY=()
146 ;;
147 esac
148
149}
150
Justin Hilemanc5b58412010-04-26 13:06:11 -0400151__git_flow_hotfix ()
152{
Justin Hilemanc24e2502010-11-26 16:03:14 -0500153 local subcommands="list start finish help"
Justin Hileman91843f82010-10-05 20:04:28 -0400154 local subcommand="$(__git_find_on_cmdline "$subcommands")"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400155 if [ -z "$subcommand" ]; then
156 __gitcomp "$subcommands"
157 return
158 fi
159
160 case "$subcommand" in
161 finish)
Justin Hileman5530efe2010-10-20 21:33:05 -0400162 __gitcomp "$(__git_flow_list_branches 'hotfix')"
Justin Hilemanc5b58412010-04-26 13:06:11 -0400163 return
164 ;;
165 *)
166 COMPREPLY=()
167 ;;
168 esac
169}
170
Justin Hileman5530efe2010-10-20 21:33:05 -0400171__git_flow_prefix ()
Justin Hilemanc5b58412010-04-26 13:06:11 -0400172{
Justin Hileman5530efe2010-10-20 21:33:05 -0400173 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 Hilemanf144b1a2010-07-20 17:14:58 -0400192}
193
Justin Hileman91843f82010-10-05 20:04:28 -0400194# alias __git_find_on_cmdline for backwards compatibility
195if [ -z "`type -t __git_find_on_cmdline`" ]; then
196 alias __git_find_on_cmdline=__git_find_subcommand
197fi