blob: e667417e15147bbee250e6804492372f0c75d000 [file] [log] [blame]
Benedikt Böhm00ccea62010-01-26 12:39:36 +01001#
Vincent Driessen6c2d30b2010-01-26 22:18:36 +01002# git-flow -- A collection of Git extensions to provide high-level
3# repository operations for Vincent Driessen's branching model.
Benedikt Böhm00ccea62010-01-26 12:39:36 +01004#
5# Original blog post presenting this model is found at:
Vincent Driessenddb350b2010-07-09 09:42:09 +02006# http://nvie.com/git-model
Benedikt Böhm00ccea62010-01-26 12:39:36 +01007#
8# Feel free to contribute to this project at:
9# http://github.com/nvie/gitflow
10#
Vincent Driessend72acba2010-04-04 16:10:17 +020011# Copyright 2010 Vincent Driessen. All rights reserved.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are met:
15#
16# 1. Redistributions of source code must retain the above copyright notice,
17# this list of conditions and the following disclaimer.
18#
19# 2. Redistributions in binary form must reproduce the above copyright
20# notice, this list of conditions and the following disclaimer in the
21# documentation and/or other materials provided with the distribution.
22#
23# THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26# EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
30# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34# The views and conclusions contained in the software and documentation are
35# those of the authors and should not be interpreted as representing official
36# policies, either expressed or implied, of Vincent Driessen.
Benedikt Böhm00ccea62010-01-26 12:39:36 +010037#
38
pccrdc902ed2012-04-21 00:23:55 -040039init() {
40 require_git_repo
41 require_gitflow_initialized
42 gitflow_load_settings
43 VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
44 PREFIX=$(git config --get gitflow.prefix.hotfix)
45}
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010046
Benedikt Böhm00ccea62010-01-26 12:39:36 +010047usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010048 echo "usage: git flow hotfix [list] [-v]"
Vincent Driessena2e41162010-02-24 01:37:07 +010049 echo " git flow hotfix start [-F] <version> [<base>]"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +020050 echo " git flow hotfix finish [-Fsumpk] <version>"
Peter Schröder0b324de2011-11-12 17:36:36 +010051 echo " git flow hotfix publish <version>"
Vedang Manerikar37f7d142012-03-12 17:03:11 +053052 echo " git flow hotfix track <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010053}
54
Vincent Driessen186d2b52010-01-27 23:48:39 +010055cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010056 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010057}
58
Vincent Driessenb866b012010-01-28 01:01:53 +010059cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010060 DEFINE_boolean verbose false 'verbose (more) output' v
61 parse_args "$@"
62
Vincent Driessenf46e2902010-02-15 23:01:52 +010063 local hotfix_branches
64 local current_branch
65 local short_names
Vincent Driessen7832d6e2010-02-21 21:31:03 +010066 hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010067 if [ -z "$hotfix_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010068 warn "No hotfix branches exist."
Randy Merrillb681b452010-06-26 13:14:15 +080069 warn ""
70 warn "You can start a new hotfix branch:"
71 warn ""
Stefan Schüßlerc4d0cb12010-12-02 18:55:06 +080072 warn " git flow hotfix start <version> [<base>]"
Randy Merrill4de01f22010-06-26 13:19:06 +080073 warn ""
Vincent Driessen186d2b52010-01-27 23:48:39 +010074 exit 0
75 fi
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010076 current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen68845232010-02-10 00:43:21 +010077 short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX g")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010078
Vincent Driessen3c337fb2010-02-04 11:30:18 +010079 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010080 local width=0
81 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010082 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010083 local len=${#branch}
Vincent Driessen3c337fb2010-02-04 11:30:18 +010084 width=$(max $width $len)
85 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010086 width=$(($width+3))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010087
Vincent Driessenf46e2902010-02-15 23:01:52 +010088 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010089 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010090 local fullname=$PREFIX$branch
91 local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
92 local master_sha=$(git rev-parse "$MASTER_BRANCH")
93 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010094 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010095 printf "* "
96 else
97 printf " "
98 fi
99 if flag verbose; then
100 printf "%-${width}s" "$branch"
101 if [ "$branch_sha" = "$master_sha" ]; then
102 printf "(no commits yet)"
103 else
Vincent Driessenf46e2902010-02-15 23:01:52 +0100104 local tagname=$(git name-rev --tags --no-undefined --name-only "$base")
105 local nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100106 if [ "$tagname" != "" ]; then
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100107 nicename=$tagname
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100108 else
Vincent Driessena4dd2232010-02-10 00:34:59 +0100109 nicename=$(git rev-parse --short "$base")
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100110 fi
111 printf "(based on $nicename)"
112 fi
113 else
114 printf "%s" "$branch"
115 fi
116 echo
117 done
Vincent Driessen186d2b52010-01-27 23:48:39 +0100118}
119
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100120cmd_help() {
121 usage
122 exit 0
123}
124
Vincent Driessenb866b012010-01-28 01:01:53 +0100125parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100126 # parse options
127 FLAGS "$@" || exit $?
128 eval set -- "${FLAGS_ARGV}"
129
130 # read arguments into global variables
Vincent Driessenf1eaa4e2011-02-03 16:11:25 +0100131 VERSION=$1
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100132 BRANCH=$PREFIX$VERSION
133}
134
135require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100136 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100137 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100138 usage
139 exit 1
140 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100141}
142
Vincent Driessen010252a2010-02-04 10:31:29 +0100143require_base_is_on_master() {
Adam Gibbinscf3da5a2010-08-22 19:13:34 +0100144 if ! git branch --no-color --contains "$BASE" 2>/dev/null \
Vincent Driessen010252a2010-02-04 10:31:29 +0100145 | sed 's/[* ] //g' \
146 | grep -q "^$MASTER_BRANCH\$"; then
147 die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
148 fi
149}
150
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100151require_no_existing_hotfix_branches() {
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100152 local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100153 local first_branch=$(echo ${hotfix_branches} | head -n1)
154 first_branch=${first_branch#$PREFIX}
155 [ -z "$hotfix_branches" ] || \
156 die "There is an existing hotfix branch ($first_branch). Finish that one first."
157}
158
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100159cmd_start() {
Vincent Driessende95e002010-07-22 15:11:17 +0200160 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100161 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100162 BASE=${2:-$MASTER_BRANCH}
Vincent Driessen010252a2010-02-04 10:31:29 +0100163 require_version_arg
164 require_base_is_on_master
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100165 require_no_existing_hotfix_branches
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100166
167 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100168 require_clean_working_tree
169 require_branch_absent "$BRANCH"
170 require_tag_absent "$VERSION_PREFIX$VERSION"
Vincent Driessenca73caf2010-02-07 19:46:38 +0100171 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100172 git fetch -q "$ORIGIN" "$MASTER_BRANCH"
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100173 fi
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300174 if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
Vincent Driessende95e002010-07-22 15:11:17 +0200175 require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
176 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100177
178 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100179 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100180
181 echo
182 echo "Summary of actions:"
183 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
184 echo "- You are now on branch '$BRANCH'"
185 echo
186 echo "Follow-up actions:"
187 echo "- Bump the version number now!"
188 echo "- Start committing your hot fixes"
189 echo "- When done, run:"
190 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100191 echo " git flow hotfix finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100192 echo
193}
194
Peter Schröder0b324de2011-11-12 17:36:36 +0100195cmd_publish() {
196 parse_args "$@"
197 require_version_arg
198
199 # sanity checks
200 require_clean_working_tree
201 require_branch "$BRANCH"
202 git fetch -q "$ORIGIN"
203 require_branch_absent "$ORIGIN/$BRANCH"
204
205 # create remote branch
206 git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
207 git fetch -q "$ORIGIN"
208
209 # configure remote tracking
210 git config "branch.$BRANCH.remote" "$ORIGIN"
211 git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
212 git checkout "$BRANCH"
213
214 echo
215 echo "Summary of actions:"
216 echo "- A new remote branch '$BRANCH' was created"
217 echo "- The local branch '$BRANCH' was configured to track the remote branch"
218 echo "- You are now on branch '$BRANCH'"
219 echo
220}
221
Vedang Manerikar37f7d142012-03-12 17:03:11 +0530222cmd_track() {
223 parse_args "$@"
224 require_version_arg
225
226 # sanity checks
227 require_clean_working_tree
228 require_branch_absent "$BRANCH"
229 git fetch -q "$ORIGIN"
230 require_branch "$ORIGIN/$BRANCH"
231
232 # create tracking branch
233 git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
234
235 echo
236 echo "Summary of actions:"
237 echo "- A new remote tracking branch '$BRANCH' was created"
238 echo "- You are now on branch '$BRANCH'"
239 echo
240}
241
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100242cmd_finish() {
Vincent Driessende95e002010-07-22 15:11:17 +0200243 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100244 DEFINE_boolean sign false "sign the release tag cryptographically" s
245 DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
246 DEFINE_string message "" "use the given tag message" m
Steve Streeting2e9ab492012-06-08 17:38:34 -0700247 DEFINE_string messagefile "" "use the contents of the given file as tag message" f
Vincent Driessenddba2df2010-02-19 06:42:18 +0100248 DEFINE_boolean push false "push to $ORIGIN after performing finish" p
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200249 DEFINE_boolean keep false "keep branch after performing finish" k
Vincent Driessenca8be522010-10-05 14:33:50 +0200250 DEFINE_boolean notag false "don't tag this release" n
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100251 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100252 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100253
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100254 # handle flags that imply other flags
255 if [ "$FLAGS_signingkey" != "" ]; then
256 FLAGS_sign=$FLAGS_TRUE
257 fi
258
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100259 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100260 require_branch "$BRANCH"
261 require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100262 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100263 git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100264 die "Could not fetch $MASTER_BRANCH from $ORIGIN."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100265 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100266 die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100267 fi
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300268 if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
Vincent Driessende95e002010-07-22 15:11:17 +0200269 require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
270 fi
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300271 if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
Vincent Driessende95e002010-07-22 15:11:17 +0200272 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
273 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100274
Vincent Driessen5fa47582010-02-09 00:31:33 +0100275 # try to merge into master
276 # in case a previous attempt to finish this release branch has failed,
277 # but the merge into master was successful, we skip it now
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100278 if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100279 git checkout "$MASTER_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100280 die "Could not check out $MASTER_BRANCH."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100281 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100282 die "There were merge conflicts."
283 # TODO: What do we do now?
284 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100285
Vincent Driessenca8be522010-10-05 14:33:50 +0200286 if noflag notag; then
287 # try to tag the release
288 # in case a previous attempt to finish this release branch has failed,
289 # but the tag was set successful, we skip it now
290 local tagname=$VERSION_PREFIX$VERSION
291 if ! git_tag_exists "$tagname"; then
292 local opts="-a"
293 flag sign && opts="$opts -s"
294 [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
295 [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
Steve Streeting2e9ab492012-06-08 17:38:34 -0700296 [ "$FLAGS_messagefile" != "" ] && opts="$opts -F '$FLAGS_messagefile'"
Randy Merrilld0df88d2011-10-13 21:09:26 -0700297 eval git tag $opts "$VERSION_PREFIX$VERSION" || \
Vincent Driessenca8be522010-10-05 14:33:50 +0200298 die "Tagging failed. Please run finish again to retry."
299 fi
Vincent Driessen5fa47582010-02-09 00:31:33 +0100300 fi
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100301
Vincent Driessen5fa47582010-02-09 00:31:33 +0100302 # try to merge into develop
303 # in case a previous attempt to finish this release branch has failed,
304 # but the merge into develop was successful, we skip it now
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100305 if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100306 git checkout "$DEVELOP_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100307 die "Could not check out $DEVELOP_BRANCH."
308
Vincent Driessend29e3152010-02-09 00:55:06 +0100309 # TODO: Actually, accounting for 'git describe' pays, so we should
310 # ideally git merge --no-ff $tagname here, instead!
Vincent Driessena4dd2232010-02-10 00:34:59 +0100311 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100312 die "There were merge conflicts."
313 # TODO: What do we do now?
314 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100315
316 # delete branch
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200317 if noflag keep; then
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200318 git branch -d "$BRANCH"
319 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100320
Vincent Driessenddba2df2010-02-19 06:42:18 +0100321 if flag push; then
322 git push "$ORIGIN" "$DEVELOP_BRANCH" || \
323 die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
324 git push "$ORIGIN" "$MASTER_BRANCH" || \
325 die "Could not push to $MASTER_BRANCH from $ORIGIN."
Vincent Driessenca8be522010-10-05 14:33:50 +0200326 if noflag notag; then
327 git push --tags "$ORIGIN" || \
328 die "Could not push tags to $ORIGIN."
329 fi
Vincent Driessenddba2df2010-02-19 06:42:18 +0100330 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100331
332 echo
333 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100334 echo "- Latest objects have been fetched from '$ORIGIN'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100335 echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
Vincent Driessenca8be522010-10-05 14:33:50 +0200336 if noflag notag; then
337 echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
338 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100339 echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200340 if flag keep; then
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200341 echo "- Hotfix branch '$BRANCH' is still available"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200342 else
343 echo "- Hotfix branch '$BRANCH' has been deleted"
344 fi
Vincent Driessenddba2df2010-02-19 06:42:18 +0100345 if flag push; then
346 echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
347 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100348 echo
349}