blob: 38e17907e8c1f8381ad99d3fbe5ec3ded894fbd0 [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#
11# Copyright (c) 2010 by Vincent Driessen
12# Copyright (c) 2010 by Benedikt Böhm
13#
14
Vincent Driessend72e4ac2010-02-16 21:33:51 +010015gitflow_require_git_repo
16gitflow_load_settings
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010017VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
18PREFIX=$(git config --get gitflow.prefix.release || echo release/)
19
Benedikt Böhm00ccea62010-01-26 12:39:36 +010020usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010021 echo "usage: git flow release [list] [-v]"
Vincent Driessen170dc742010-01-28 00:20:51 +010022 echo " git flow release start <version>"
23 echo " git flow release finish <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010024 # TODO
25 #echo ""
26 #echo "options:"
27 #echo "--option Explanation"
28 #echo ""
29 #echo "start-only options:"
30 #echo "--bump <script>"
31 #echo " Run the given script to auto-update the version number"
32 #echo ""
33 #echo "finish-only options:"
34 #echo "--push Push to the origin repo when finished"
35}
36
Vincent Driessen170dc742010-01-28 00:20:51 +010037cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010038 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010039}
40
Vincent Driessenb866b012010-01-28 01:01:53 +010041cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010042 DEFINE_boolean verbose false 'verbose (more) output' v
43 parse_args "$@"
44
Vincent Driessenf46e2902010-02-15 23:01:52 +010045 local release_branches
46 local current_branch
47 local short_names
Vincent Driessen21c7aa92010-02-16 20:57:35 +010048 release_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010049 if [ -z "$release_branches" ]; then
Vincent Driessen170dc742010-01-28 00:20:51 +010050 warn "No release branches exist."
51 exit 0
52 fi
Vincent Driessen3c337fb2010-02-04 11:30:18 +010053
Vincent Driessen27592dd2010-02-06 14:45:39 +010054 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen68845232010-02-10 00:43:21 +010055 short_names=$(echo "$release_branches" | sed "s ^$PREFIX g")
Vincent Driessen27592dd2010-02-06 14:45:39 +010056
Vincent Driessen3c337fb2010-02-04 11:30:18 +010057 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010058 local width=0
59 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010060 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010061 local len=${#branch}
Vincent Driessen3c337fb2010-02-04 11:30:18 +010062 width=$(max $width $len)
63 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010064 width=$(($width+3))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010065
Vincent Driessenf46e2902010-02-15 23:01:52 +010066 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010067 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010068 local fullname=$PREFIX$branch
69 local base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
70 local develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
71 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010072 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010073 printf "* "
74 else
75 printf " "
76 fi
77 if flag verbose; then
78 printf "%-${width}s" "$branch"
79 if [ "$branch_sha" = "$develop_sha" ]; then
80 printf "(no commits yet)"
81 else
Vincent Driessenf46e2902010-02-15 23:01:52 +010082 local nicename=$(git rev-parse --short "$base")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010083 printf "(based on $nicename)"
84 fi
85 else
86 printf "%s" "$branch"
87 fi
88 echo
89 done
Vincent Driessen170dc742010-01-28 00:20:51 +010090}
91
Benedikt Böhm00ccea62010-01-26 12:39:36 +010092cmd_help() {
93 usage
94 exit 0
95}
96
Vincent Driessenb866b012010-01-28 01:01:53 +010097parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010098 # parse options
99 FLAGS "$@" || exit $?
100 eval set -- "${FLAGS_ARGV}"
101
102 # read arguments into global variables
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100103 VERSION=$1
104 BRANCH=$PREFIX$VERSION
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100105}
106
107require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100108 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100109 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100110 usage
111 exit 1
112 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100113}
114
Vincent Driessen010252a2010-02-04 10:31:29 +0100115require_base_is_on_develop() {
116 if ! git branch --contains "$BASE" 2>/dev/null \
117 | sed 's/[* ] //g' \
118 | grep -q "^$DEVELOP_BRANCH\$"; then
119 die "fatal: Given base '$BASE' is not a valid commit on '$DEVELOP_BRANCH'."
120 fi
121}
122
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100123require_no_existing_release_branches() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100124 local release_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100125 local first_branch=$(echo ${release_branches} | head -n1)
126 first_branch=${first_branch#$PREFIX}
127 [ -z "$release_branches" ] || \
128 die "There is an existing release branch ($first_branch). Finish that one first."
129}
130
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100131cmd_start() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100132 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100133 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100134 BASE=${2:-$DEVELOP_BRANCH}
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100135 require_version_arg
Vincent Driessen010252a2010-02-04 10:31:29 +0100136 require_base_is_on_develop
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100137 require_no_existing_release_branches
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100138
139 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100140 gitflow_require_clean_working_tree
Vincent Driessena4dd2232010-02-10 00:34:59 +0100141 gitflow_require_branch_absent "$BRANCH"
142 gitflow_require_tag_absent "$VERSION_PREFIX$VERSION"
Vincent Driessenca73caf2010-02-07 19:46:38 +0100143 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100144 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100145 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100146 gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100147
148 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100149 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100150
151 echo
152 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100153 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100154 echo "- You are now on branch '$BRANCH'"
155 echo
156 echo "Follow-up actions:"
157 echo "- Bump the version number now!"
158 echo "- Start committing last-minute fixes in preparing your release"
159 echo "- When done, run:"
160 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100161 echo " git flow release finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100162 echo
163}
164
165cmd_finish() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100166 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100167 DEFINE_boolean sign false "sign the release tag cryptographically" s
168 DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
169 DEFINE_string message "" "use the given tag message" m
Vincent Driessenddba2df2010-02-19 06:42:18 +0100170 DEFINE_boolean push false "push to $ORIGIN after performing finish" p
Jason L. Shiffer6809f0e2010-02-18 23:57:08 -0500171
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100172 parse_args "$@"
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100173 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100174
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100175 # handle flags that imply other flags
176 if [ "$FLAGS_signingkey" != "" ]; then
177 FLAGS_sign=$FLAGS_TRUE
178 fi
179
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100180 # sanity checks
Vincent Driessena4dd2232010-02-10 00:34:59 +0100181 gitflow_require_branch "$BRANCH"
Vincent Driessen48386442010-01-29 10:30:40 +0100182 gitflow_require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100183 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100184 git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100185 die "Could not fetch $MASTER_BRANCH from $ORIGIN."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100186 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100187 die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100188 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100189 gitflow_require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
190 gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100191
Vincent Driessen5fa47582010-02-09 00:31:33 +0100192 # try to merge into master
193 # in case a previous attempt to finish this release branch has failed,
194 # but the merge into master was successful, we skip it now
Vincent Driessena4dd2232010-02-10 00:34:59 +0100195 if ! gitflow_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
196 git checkout "$MASTER_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100197 die "Could not check out $MASTER_BRANCH."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100198 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100199 die "There were merge conflicts."
200 # TODO: What do we do now?
201 fi
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100202
Vincent Driessen5fa47582010-02-09 00:31:33 +0100203 # try to tag the release
204 # in case a previous attempt to finish this release branch has failed,
205 # but the tag was set successful, we skip it now
Vincent Driessenf46e2902010-02-15 23:01:52 +0100206 local tagname=$VERSION_PREFIX$VERSION
Vincent Driessena4dd2232010-02-10 00:34:59 +0100207 if ! gitflow_tag_exists "$tagname"; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100208 local opts="-a"
Vincent Driessen5fa47582010-02-09 00:31:33 +0100209 flag sign && opts="$opts -s"
210 [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
211 [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
212 git tag $opts "$tagname" || \
213 die "Tagging failed. Please run finish again to retry."
214 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100215
Vincent Driessen5fa47582010-02-09 00:31:33 +0100216 # try to merge into develop
217 # in case a previous attempt to finish this release branch has failed,
218 # but the merge into develop was successful, we skip it now
Vincent Driessena4dd2232010-02-10 00:34:59 +0100219 if ! gitflow_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
220 git checkout "$DEVELOP_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100221 die "Could not check out $DEVELOP_BRANCH."
Vincent Driessend29e3152010-02-09 00:55:06 +0100222
223 # TODO: Actually, accounting for 'git describe' pays, so we should
224 # ideally git merge --no-ff $tagname here, instead!
Vincent Driessena4dd2232010-02-10 00:34:59 +0100225 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100226 die "There were merge conflicts."
227 # TODO: What do we do now?
228 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100229
230 # delete branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100231 git branch -d "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100232
Vincent Driessenddba2df2010-02-19 06:42:18 +0100233 if flag push; then
234 git push "$ORIGIN" "$DEVELOP_BRANCH" || \
235 die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
236 git push "$ORIGIN" "$MASTER_BRANCH" || \
237 die "Could not push to $MASTER_BRANCH from $ORIGIN."
238 git push --tags "$ORIGIN" || \
239 die "Could not push tags to $ORIGIN."
240 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100241
242 echo
243 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100244 echo "- Latest objects have been fetched from '$ORIGIN'"
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100245 echo "- Release branch has been merged into '$MASTER_BRANCH'"
Vincent Driessen5fa47582010-02-09 00:31:33 +0100246 echo "- The release was tagged '$tagname'"
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100247 echo "- Release branch has been back-merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100248 echo "- Release branch '$BRANCH' has been deleted"
Vincent Driessenddba2df2010-02-19 06:42:18 +0100249 if flag push; then
250 echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
251 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100252 echo
253}