blob: c76d323b597f02c3d229b60ac59ddec8ccb41ece [file] [log] [blame]
Robert R Evansa4d02422010-10-02 15:07:29 -07001#!bash
2#
3# git-flow-completion
4# ===================
5#
6# Bash completion support for [git-flow](http://github.com/nvie/gitflow)
7#
8# The contained completion routines provide support for completing:
9#
10# * git-flow init and version
11# * feature, hotfix and release branches
12# * remote feature branch names (for `git-flow feature track`)
13#
14#
15# Installation
16# ------------
17#
18# To achieve git-flow completion nirvana:
19#
20# 0. Install git-completion.
21#
22# 1. Install this file. Either:
23#
24# a. Place it in a `bash-completion.d` folder:
25#
26# * /etc/bash-completion.d
27# * /usr/local/etc/bash-completion.d
28# * ~/bash-completion.d
29#
30# b. Or, copy it somewhere (e.g. ~/.git-flow-completion.sh) and put the following line in
31# your .bashrc:
32#
33# source ~/.git-flow-completion.sh
34#
35# 2. If you are using Git < 1.7.1: Edit git-completion.sh and add the following line to the giant
36# $command case in _git:
37#
38# flow) _git_flow ;;
39#
40#
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/)
47
48_git_flow ()
49{
50 local subcommands="init feature release hotfix"
51 local subcommand="$(__git_find_subcommand "$subcommands")"
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{
78 local subcommands="list start finish publish track diff rebase checkout pull"
79 local subcommand="$(__git_find_subcommand "$subcommands")"
80 if [ -z "$subcommand" ]; then
81 __gitcomp "$subcommands"
82 return
83 fi
84
85 case "$subcommand" in
86 pull)
87 __gitcomp "$(__git_remotes)"
88 return
89 ;;
90 checkout|finish|diff|rebase)
91 __gitcomp "$(__git_flow_list_features)"
92 return
93 ;;
94 publish)
95 __gitcomp "$(comm -23 <(__git_flow_list_features) <(__git_flow_list_remote_features))"
96 return
97 ;;
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{
110 git flow feature list 2> /dev/null | tr -d ' |*'
111}
112
113__git_flow_list_remote_features ()
114{
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/"
121}
122
123__git_flow_release ()
124{
125 local subcommands="list start finish"
126 local subcommand="$(__git_find_subcommand "$subcommands")"
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{
146 git flow release list 2> /dev/null
147}
148
149__git_flow_hotfix ()
150{
151 local subcommands="list start finish"
152 local subcommand="$(__git_find_subcommand "$subcommands")"
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{
171 git flow hotfix list 2> /dev/null
172}
173
174# temporarily wrap __git_find_on_cmdline() for backwards compatibility
175if [ -z "`type -t __git_find_subcommand`" ]; then
176 alias __git_find_subcommand=__git_find_on_cmdline
177fi