blob: e3ea035335cb892d4bf2a1555cba069704752694 [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:
6# http://nvie.com/archives/323
7#
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
Vincent Driessenab3dc492010-01-29 12:35:49 +010042VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
Vincent Driessenc1598bf2010-02-20 16:52:48 +010043PREFIX=$(git config --get gitflow.prefix.hotfix)
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 hotfix [list] [-v]"
Vincent Driessena2e41162010-02-24 01:37:07 +010047 echo " git flow hotfix start [-F] <version> [<base>]"
48 echo " git flow hotfix finish [-Fsump] <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010049}
50
Vincent Driessen186d2b52010-01-27 23:48:39 +010051cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010052 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010053}
54
Vincent Driessenb866b012010-01-28 01:01:53 +010055cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010056 DEFINE_boolean verbose false 'verbose (more) output' v
57 parse_args "$@"
58
Vincent Driessenf46e2902010-02-15 23:01:52 +010059 local hotfix_branches
60 local current_branch
61 local short_names
Vincent Driessen7832d6e2010-02-21 21:31:03 +010062 hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010063 if [ -z "$hotfix_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010064 warn "No hotfix branches exist."
Randy Merrillb681b452010-06-26 13:14:15 +080065 warn ""
66 warn "You can start a new hotfix branch:"
67 warn ""
68 warn " git flow hotfix start <name> [<base>]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010069 exit 0
70 fi
Vincent Driessen27592dd2010-02-06 14:45:39 +010071 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen68845232010-02-10 00:43:21 +010072 short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX g")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010073
Vincent Driessen3c337fb2010-02-04 11:30:18 +010074 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010075 local width=0
76 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010077 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010078 local len=${#branch}
Vincent Driessen3c337fb2010-02-04 11:30:18 +010079 width=$(max $width $len)
80 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010081 width=$(($width+3))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010082
Vincent Driessenf46e2902010-02-15 23:01:52 +010083 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010084 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010085 local fullname=$PREFIX$branch
86 local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
87 local master_sha=$(git rev-parse "$MASTER_BRANCH")
88 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010089 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010090 printf "* "
91 else
92 printf " "
93 fi
94 if flag verbose; then
95 printf "%-${width}s" "$branch"
96 if [ "$branch_sha" = "$master_sha" ]; then
97 printf "(no commits yet)"
98 else
Vincent Driessenf46e2902010-02-15 23:01:52 +010099 local tagname=$(git name-rev --tags --no-undefined --name-only "$base")
100 local nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100101 if [ "$tagname" != "" ]; then
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100102 nicename=$tagname
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100103 else
Vincent Driessena4dd2232010-02-10 00:34:59 +0100104 nicename=$(git rev-parse --short "$base")
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100105 fi
106 printf "(based on $nicename)"
107 fi
108 else
109 printf "%s" "$branch"
110 fi
111 echo
112 done
Vincent Driessen186d2b52010-01-27 23:48:39 +0100113}
114
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100115cmd_help() {
116 usage
117 exit 0
118}
119
Vincent Driessenb866b012010-01-28 01:01:53 +0100120parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100121 # parse options
122 FLAGS "$@" || exit $?
123 eval set -- "${FLAGS_ARGV}"
124
125 # read arguments into global variables
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100126 VERSION=$1
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100127 BRANCH=$PREFIX$VERSION
128}
129
130require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100131 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100132 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100133 usage
134 exit 1
135 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100136}
137
Vincent Driessen010252a2010-02-04 10:31:29 +0100138require_base_is_on_master() {
139 if ! git branch --contains "$BASE" 2>/dev/null \
140 | sed 's/[* ] //g' \
141 | grep -q "^$MASTER_BRANCH\$"; then
142 die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
143 fi
144}
145
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100146require_no_existing_hotfix_branches() {
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100147 local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100148 local first_branch=$(echo ${hotfix_branches} | head -n1)
149 first_branch=${first_branch#$PREFIX}
150 [ -z "$hotfix_branches" ] || \
151 die "There is an existing hotfix branch ($first_branch). Finish that one first."
152}
153
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100154cmd_start() {
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100155 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100156 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100157 BASE=${2:-$MASTER_BRANCH}
Vincent Driessen010252a2010-02-04 10:31:29 +0100158 require_version_arg
159 require_base_is_on_master
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100160 require_no_existing_hotfix_branches
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100161
162 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100163 require_clean_working_tree
164 require_branch_absent "$BRANCH"
165 require_tag_absent "$VERSION_PREFIX$VERSION"
Vincent Driessenca73caf2010-02-07 19:46:38 +0100166 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100167 git fetch -q "$ORIGIN" "$MASTER_BRANCH"
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100168 fi
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100169 require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100170
171 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100172 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100173
174 echo
175 echo "Summary of actions:"
176 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
177 echo "- You are now on branch '$BRANCH'"
178 echo
179 echo "Follow-up actions:"
180 echo "- Bump the version number now!"
181 echo "- Start committing your hot fixes"
182 echo "- When done, run:"
183 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100184 echo " git flow hotfix finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100185 echo
186}
187
188cmd_finish() {
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100189 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
190 DEFINE_boolean sign false "sign the release tag cryptographically" s
191 DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
192 DEFINE_string message "" "use the given tag message" m
Vincent Driessenddba2df2010-02-19 06:42:18 +0100193 DEFINE_boolean push false "push to $ORIGIN after performing finish" p
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100194 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100195 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100196
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100197 # handle flags that imply other flags
198 if [ "$FLAGS_signingkey" != "" ]; then
199 FLAGS_sign=$FLAGS_TRUE
200 fi
201
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100202 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100203 require_branch "$BRANCH"
204 require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100205 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100206 git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100207 die "Could not fetch $MASTER_BRANCH from $ORIGIN."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100208 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100209 die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100210 fi
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100211 require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
212 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100213
Vincent Driessen5fa47582010-02-09 00:31:33 +0100214 # try to merge into master
215 # in case a previous attempt to finish this release branch has failed,
216 # but the merge into master was successful, we skip it now
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100217 if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100218 git checkout "$MASTER_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100219 die "Could not check out $MASTER_BRANCH."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100220 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100221 die "There were merge conflicts."
222 # TODO: What do we do now?
223 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100224
Vincent Driessen5fa47582010-02-09 00:31:33 +0100225 # try to tag the release
226 # in case a previous attempt to finish this release branch has failed,
227 # but the tag was set successful, we skip it now
Vincent Driessenf46e2902010-02-15 23:01:52 +0100228 local tagname=$VERSION_PREFIX$VERSION
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100229 if ! git_tag_exists "$tagname"; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100230 local opts="-a"
Vincent Driessen5fa47582010-02-09 00:31:33 +0100231 flag sign && opts="$opts -s"
232 [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
233 [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
234 git tag $opts "$VERSION_PREFIX$VERSION" || \
235 die "Tagging failed. Please run finish again to retry."
236 fi
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100237
Vincent Driessen5fa47582010-02-09 00:31:33 +0100238 # try to merge into develop
239 # in case a previous attempt to finish this release branch has failed,
240 # but the merge into develop was successful, we skip it now
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100241 if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100242 git checkout "$DEVELOP_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100243 die "Could not check out $DEVELOP_BRANCH."
244
Vincent Driessend29e3152010-02-09 00:55:06 +0100245 # TODO: Actually, accounting for 'git describe' pays, so we should
246 # ideally git merge --no-ff $tagname here, instead!
Vincent Driessena4dd2232010-02-10 00:34:59 +0100247 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100248 die "There were merge conflicts."
249 # TODO: What do we do now?
250 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100251
252 # delete branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100253 git branch -d "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100254
Vincent Driessenddba2df2010-02-19 06:42:18 +0100255 if flag push; then
256 git push "$ORIGIN" "$DEVELOP_BRANCH" || \
257 die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
258 git push "$ORIGIN" "$MASTER_BRANCH" || \
259 die "Could not push to $MASTER_BRANCH from $ORIGIN."
260 git push --tags "$ORIGIN" || \
261 die "Could not push tags to $ORIGIN."
262 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100263
264 echo
265 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100266 echo "- Latest objects have been fetched from '$ORIGIN'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100267 echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
Vincent Driessenab3dc492010-01-29 12:35:49 +0100268 echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100269 echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100270 echo "- Hotfix branch '$BRANCH' has been deleted"
Vincent Driessenddba2df2010-02-19 06:42:18 +0100271 if flag push; then
272 echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
273 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100274 echo
275}