blob: 6705856f14413dd158d80b179d203b66a988bf7b [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
Vincent Driessen7832d6e2010-02-21 21:31:03 +010039require_git_repo
40require_gitflow_initialized
Vincent Driessend72e4ac2010-02-16 21:33:51 +010041gitflow_load_settings
Nowell Striteff275fa2010-09-22 13:19:30 -040042VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
Vincent Driessenc1598bf2010-02-20 16:52:48 +010043PREFIX=$(git config --get gitflow.prefix.release)
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010044
Benedikt Böhm00ccea62010-01-26 12:39:36 +010045usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010046 echo "usage: git flow release [list] [-v]"
Vincent Driessena2e41162010-02-24 01:37:07 +010047 echo " git flow release start [-F] <version>"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +020048 echo " git flow release finish [-Fsumpk] <version>"
Felipe Talaverabbca9222010-09-30 12:25:08 -070049 echo " git flow release publish <name>"
50 echo " git flow release track <name>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010051}
52
Vincent Driessen170dc742010-01-28 00:20:51 +010053cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010054 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010055}
56
Vincent Driessenb866b012010-01-28 01:01:53 +010057cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010058 DEFINE_boolean verbose false 'verbose (more) output' v
59 parse_args "$@"
60
Vincent Driessenf46e2902010-02-15 23:01:52 +010061 local release_branches
62 local current_branch
63 local short_names
Vincent Driessen7832d6e2010-02-21 21:31:03 +010064 release_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010065 if [ -z "$release_branches" ]; then
Vincent Driessen170dc742010-01-28 00:20:51 +010066 warn "No release branches exist."
Randy Merrillb681b452010-06-26 13:14:15 +080067 warn ""
68 warn "You can start a new release branch:"
69 warn ""
70 warn " git flow release start <name> [<base>]"
Randy Merrill4de01f22010-06-26 13:19:06 +080071 warn ""
Vincent Driessen170dc742010-01-28 00:20:51 +010072 exit 0
73 fi
Vincent Driessen3c337fb2010-02-04 11:30:18 +010074
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010075 current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen68845232010-02-10 00:43:21 +010076 short_names=$(echo "$release_branches" | sed "s ^$PREFIX g")
Vincent Driessen27592dd2010-02-06 14:45:39 +010077
Vincent Driessen3c337fb2010-02-04 11:30:18 +010078 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010079 local width=0
80 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010081 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010082 local len=${#branch}
Vincent Driessen3c337fb2010-02-04 11:30:18 +010083 width=$(max $width $len)
84 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010085 width=$(($width+3))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010086
Vincent Driessenf46e2902010-02-15 23:01:52 +010087 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010088 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010089 local fullname=$PREFIX$branch
90 local base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
91 local develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
92 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010093 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010094 printf "* "
95 else
96 printf " "
97 fi
98 if flag verbose; then
99 printf "%-${width}s" "$branch"
100 if [ "$branch_sha" = "$develop_sha" ]; then
101 printf "(no commits yet)"
102 else
Vincent Driessenf46e2902010-02-15 23:01:52 +0100103 local nicename=$(git rev-parse --short "$base")
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100104 printf "(based on $nicename)"
105 fi
106 else
107 printf "%s" "$branch"
108 fi
109 echo
110 done
Vincent Driessen170dc742010-01-28 00:20:51 +0100111}
112
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100113cmd_help() {
114 usage
115 exit 0
116}
117
Vincent Driessenb866b012010-01-28 01:01:53 +0100118parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100119 # parse options
120 FLAGS "$@" || exit $?
121 eval set -- "${FLAGS_ARGV}"
122
123 # read arguments into global variables
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200124 VERSION=${!#}
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100125 BRANCH=$PREFIX$VERSION
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100126}
127
128require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100129 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100130 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100131 usage
132 exit 1
133 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100134}
135
Vincent Driessen010252a2010-02-04 10:31:29 +0100136require_base_is_on_develop() {
Adam Gibbinscf3da5a2010-08-22 19:13:34 +0100137 if ! git branch --no-color --contains "$BASE" 2>/dev/null \
Vincent Driessen010252a2010-02-04 10:31:29 +0100138 | sed 's/[* ] //g' \
139 | grep -q "^$DEVELOP_BRANCH\$"; then
140 die "fatal: Given base '$BASE' is not a valid commit on '$DEVELOP_BRANCH'."
141 fi
142}
143
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100144require_no_existing_release_branches() {
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100145 local release_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100146 local first_branch=$(echo ${release_branches} | head -n1)
147 first_branch=${first_branch#$PREFIX}
148 [ -z "$release_branches" ] || \
149 die "There is an existing release branch ($first_branch). Finish that one first."
150}
151
Vincent Driessend256bbf2010-10-05 08:59:08 +0200152require_not_on_release_branch() {
Vincent Driessenb9765da2010-10-05 08:43:34 +0200153 if [ "$BRANCH" = "$(git_current_branch)"]; then
Vincent Driessen4d8239a2010-10-05 08:43:56 +0200154 die "You cannot be in the '$BRANCH' branch when you finish it."
Felipe Talavera92c16a92010-09-30 13:47:02 -0700155 fi
156}
157
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100158cmd_start() {
Vincent Driessende95e002010-07-22 15:11:17 +0200159 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100161 BASE=${2:-$DEVELOP_BRANCH}
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100162 require_version_arg
Vincent Driessen010252a2010-02-04 10:31:29 +0100163 require_base_is_on_develop
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100164 require_no_existing_release_branches
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100165
166 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100167 require_clean_working_tree
168 require_branch_absent "$BRANCH"
169 require_tag_absent "$VERSION_PREFIX$VERSION"
Vincent Driessenca73caf2010-02-07 19:46:38 +0100170 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100171 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100172 fi
Vincent Driessende95e002010-07-22 15:11:17 +0200173 if has "$ORIGIN/$DEVELOP_BRANCH" "$(git_remote_branches)"; then
174 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
175 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100176
177 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100178 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100179
180 echo
181 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100182 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100183 echo "- You are now on branch '$BRANCH'"
184 echo
185 echo "Follow-up actions:"
186 echo "- Bump the version number now!"
187 echo "- Start committing last-minute fixes in preparing your release"
188 echo "- When done, run:"
189 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100190 echo " git flow release finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100191 echo
192}
193
194cmd_finish() {
Vincent Driessende95e002010-07-22 15:11:17 +0200195 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100196 DEFINE_boolean sign false "sign the release tag cryptographically" s
197 DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
198 DEFINE_string message "" "use the given tag message" m
Vincent Driessenddba2df2010-02-19 06:42:18 +0100199 DEFINE_boolean push false "push to $ORIGIN after performing finish" p
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200200 DEFINE_boolean keep false 'keep branch after performing finish' k
Jason L. Shiffer6809f0e2010-02-18 23:57:08 -0500201
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100202 parse_args "$@"
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100203 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100204
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100205 # handle flags that imply other flags
206 if [ "$FLAGS_signingkey" != "" ]; then
207 FLAGS_sign=$FLAGS_TRUE
208 fi
209
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100210 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100211 require_branch "$BRANCH"
212 require_clean_working_tree
Vincent Driessend256bbf2010-10-05 08:59:08 +0200213 require_not_on_release_branch
Vincent Driessenca73caf2010-02-07 19:46:38 +0100214 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100215 git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100216 die "Could not fetch $MASTER_BRANCH from $ORIGIN."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100217 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100218 die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100219 fi
Vincent Driessende95e002010-07-22 15:11:17 +0200220 if has "$ORIGIN/$MASTER_BRANCH" "$(git_remote_branches)"; then
221 require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
222 fi
223 if has "$ORIGIN/$DEVELOP_BRANCH" "$(git_remote_branches)"; then
224 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
225 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100226
Vincent Driessen5fa47582010-02-09 00:31:33 +0100227 # try to merge into master
228 # in case a previous attempt to finish this release branch has failed,
229 # but the merge into master was successful, we skip it now
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100230 if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100231 git checkout "$MASTER_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100232 die "Could not check out $MASTER_BRANCH."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100233 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100234 die "There were merge conflicts."
235 # TODO: What do we do now?
236 fi
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100237
Vincent Driessen5fa47582010-02-09 00:31:33 +0100238 # try to tag the release
239 # in case a previous attempt to finish this release branch has failed,
240 # but the tag was set successful, we skip it now
Vincent Driessenf46e2902010-02-15 23:01:52 +0100241 local tagname=$VERSION_PREFIX$VERSION
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100242 if ! git_tag_exists "$tagname"; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100243 local opts="-a"
Vincent Driessen5fa47582010-02-09 00:31:33 +0100244 flag sign && opts="$opts -s"
245 [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
246 [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
247 git tag $opts "$tagname" || \
248 die "Tagging failed. Please run finish again to retry."
249 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100250
Vincent Driessen5fa47582010-02-09 00:31:33 +0100251 # try to merge into develop
252 # in case a previous attempt to finish this release branch has failed,
253 # but the merge into develop was successful, we skip it now
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100254 if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100255 git checkout "$DEVELOP_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100256 die "Could not check out $DEVELOP_BRANCH."
Vincent Driessend29e3152010-02-09 00:55:06 +0100257
258 # TODO: Actually, accounting for 'git describe' pays, so we should
259 # ideally git merge --no-ff $tagname here, instead!
Vincent Driessena4dd2232010-02-10 00:34:59 +0100260 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100261 die "There were merge conflicts."
262 # TODO: What do we do now?
263 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100264
265 # delete branch
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200266 if flag keep; then
267 echo "Keep this branch" > /dev/null
268 else
269 git branch -d "$BRANCH"
270 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100271
Vincent Driessenddba2df2010-02-19 06:42:18 +0100272 if flag push; then
273 git push "$ORIGIN" "$DEVELOP_BRANCH" || \
274 die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
275 git push "$ORIGIN" "$MASTER_BRANCH" || \
276 die "Could not push to $MASTER_BRANCH from $ORIGIN."
277 git push --tags "$ORIGIN" || \
278 die "Could not push tags to $ORIGIN."
Felipe Talavera4c90d922010-09-30 13:30:03 -0700279 git push "$ORIGIN" :"$BRANCH" || \
280 die "Could not delete the remote $BRANCH in $ORIGIN."
Vincent Driessenddba2df2010-02-19 06:42:18 +0100281 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100282
283 echo
284 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100285 echo "- Latest objects have been fetched from '$ORIGIN'"
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100286 echo "- Release branch has been merged into '$MASTER_BRANCH'"
Vincent Driessen5fa47582010-02-09 00:31:33 +0100287 echo "- The release was tagged '$tagname'"
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100288 echo "- Release branch has been back-merged into '$DEVELOP_BRANCH'"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200289 if flag keep; then
290 echo "- Release branch '$BRANCH' has been kept"
291 else
292 echo "- Release branch '$BRANCH' has been deleted"
293 fi
Vincent Driessenddba2df2010-02-19 06:42:18 +0100294 if flag push; then
295 echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
Felipe Talavera4c90d922010-09-30 13:30:03 -0700296 echo "- Release branch '$BRANCH' in '$ORIGIN' has been deleted."
Vincent Driessenddba2df2010-02-19 06:42:18 +0100297 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100298 echo
299}
Felipe Talaverafbcf4a02010-09-30 12:03:01 -0700300
301cmd_publish() {
302 parse_args "$@"
Felipe Talaveradf706ac2010-09-30 13:11:34 -0700303 require_version_arg
Felipe Talaverafbcf4a02010-09-30 12:03:01 -0700304
305 # sanity checks
306 require_clean_working_tree
307 require_branch "$BRANCH"
308 git fetch -q "$ORIGIN"
309 require_branch_absent "$ORIGIN/$BRANCH"
310
311 # create remote branch
312 git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
313 git fetch -q "$ORIGIN"
314
315 # configure remote tracking
316 git config "branch.$BRANCH.remote" "$ORIGIN"
317 git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
318 git checkout "$BRANCH"
319
320 echo
321 echo "Summary of actions:"
322 echo "- A new remote branch '$BRANCH' was created"
323 echo "- The local branch '$BRANCH' was configured to track the remote branch"
324 echo "- You are now on branch '$BRANCH'"
325 echo
326}
327
328cmd_track() {
329 parse_args "$@"
Felipe Talaveradf706ac2010-09-30 13:11:34 -0700330 require_version_arg
Felipe Talaverafbcf4a02010-09-30 12:03:01 -0700331
332 # sanity checks
333 require_clean_working_tree
334 require_branch_absent "$BRANCH"
335 git fetch -q "$ORIGIN"
336 require_branch "$ORIGIN/$BRANCH"
337
338 # create tracking branch
339 git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
340
341 echo
342 echo "Summary of actions:"
343 echo "- A new remote tracking branch '$BRANCH' was created"
344 echo "- You are now on branch '$BRANCH'"
345 echo
346}