blob: 357eb78a75a0dbe0b8274c53bdd7ea7c4cb155ec [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
Vincent Driessenab3dc492010-01-29 12:35:49 +010017VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010018PREFIX=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
19
Benedikt Böhm00ccea62010-01-26 12:39:36 +010020usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010021 echo "usage: git flow hotfix [list] [-v]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010022 echo " git flow hotfix start <version> [<base>]"
Vincent Driessen010252a2010-02-04 10:31:29 +010023 echo " git flow hotfix finish <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010024}
25
Vincent Driessen186d2b52010-01-27 23:48:39 +010026cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010027 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010028}
29
Vincent Driessenb866b012010-01-28 01:01:53 +010030cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010031 DEFINE_boolean verbose false 'verbose (more) output' v
32 parse_args "$@"
33
Vincent Driessenf46e2902010-02-15 23:01:52 +010034 local hotfix_branches
35 local current_branch
36 local short_names
Vincent Driessen21c7aa92010-02-16 20:57:35 +010037 hotfix_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010038 if [ -z "$hotfix_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010039 warn "No hotfix branches exist."
40 exit 0
41 fi
Vincent Driessen27592dd2010-02-06 14:45:39 +010042 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen68845232010-02-10 00:43:21 +010043 short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX g")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010044
Vincent Driessen3c337fb2010-02-04 11:30:18 +010045 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010046 local width=0
47 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010048 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010049 local len=${#branch}
Vincent Driessen3c337fb2010-02-04 11:30:18 +010050 width=$(max $width $len)
51 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010052 width=$(($width+3))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010053
Vincent Driessenf46e2902010-02-15 23:01:52 +010054 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010055 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010056 local fullname=$PREFIX$branch
57 local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
58 local master_sha=$(git rev-parse "$MASTER_BRANCH")
59 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010060 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010061 printf "* "
62 else
63 printf " "
64 fi
65 if flag verbose; then
66 printf "%-${width}s" "$branch"
67 if [ "$branch_sha" = "$master_sha" ]; then
68 printf "(no commits yet)"
69 else
Vincent Driessenf46e2902010-02-15 23:01:52 +010070 local tagname=$(git name-rev --tags --no-undefined --name-only "$base")
71 local nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +010072 if [ "$tagname" != "" ]; then
Vincent Driessenc5fcc012010-02-10 00:18:08 +010073 nicename=$tagname
Vincent Driessen3c337fb2010-02-04 11:30:18 +010074 else
Vincent Driessena4dd2232010-02-10 00:34:59 +010075 nicename=$(git rev-parse --short "$base")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010076 fi
77 printf "(based on $nicename)"
78 fi
79 else
80 printf "%s" "$branch"
81 fi
82 echo
83 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010084}
85
Benedikt Böhm00ccea62010-01-26 12:39:36 +010086cmd_help() {
87 usage
88 exit 0
89}
90
Vincent Driessenb866b012010-01-28 01:01:53 +010091parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010092 # parse options
93 FLAGS "$@" || exit $?
94 eval set -- "${FLAGS_ARGV}"
95
96 # read arguments into global variables
Vincent Driessenc5fcc012010-02-10 00:18:08 +010097 VERSION=$1
Vincent Driessen3c337fb2010-02-04 11:30:18 +010098 BRANCH=$PREFIX$VERSION
99}
100
101require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100102 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100103 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100104 usage
105 exit 1
106 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100107}
108
Vincent Driessen010252a2010-02-04 10:31:29 +0100109require_base_is_on_master() {
110 if ! git branch --contains "$BASE" 2>/dev/null \
111 | sed 's/[* ] //g' \
112 | grep -q "^$MASTER_BRANCH\$"; then
113 die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
114 fi
115}
116
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100117require_no_existing_hotfix_branches() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100118 local hotfix_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100119 local first_branch=$(echo ${hotfix_branches} | head -n1)
120 first_branch=${first_branch#$PREFIX}
121 [ -z "$hotfix_branches" ] || \
122 die "There is an existing hotfix branch ($first_branch). Finish that one first."
123}
124
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100125cmd_start() {
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100126 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100127 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100128 BASE=${2:-$MASTER_BRANCH}
Vincent Driessen010252a2010-02-04 10:31:29 +0100129 require_version_arg
130 require_base_is_on_master
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100131 require_no_existing_hotfix_branches
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100132
133 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100134 gitflow_require_clean_working_tree
Vincent Driessena4dd2232010-02-10 00:34:59 +0100135 gitflow_require_branch_absent "$BRANCH"
136 gitflow_require_tag_absent "$VERSION_PREFIX$VERSION"
Vincent Driessenca73caf2010-02-07 19:46:38 +0100137 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100138 git fetch -q "$ORIGIN" "$MASTER_BRANCH"
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100139 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100140 gitflow_require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100141
142 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100143 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100144
145 echo
146 echo "Summary of actions:"
147 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
148 echo "- You are now on branch '$BRANCH'"
149 echo
150 echo "Follow-up actions:"
151 echo "- Bump the version number now!"
152 echo "- Start committing your hot fixes"
153 echo "- When done, run:"
154 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100155 echo " git flow hotfix finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100156 echo
157}
158
159cmd_finish() {
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100160 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
161 DEFINE_boolean sign false "sign the release tag cryptographically" s
162 DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
163 DEFINE_string message "" "use the given tag message" m
Vincent Driessenddba2df2010-02-19 06:42:18 +0100164 DEFINE_boolean push false "push to $ORIGIN after performing finish" p
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100165 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100166 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100167
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100168 # handle flags that imply other flags
169 if [ "$FLAGS_signingkey" != "" ]; then
170 FLAGS_sign=$FLAGS_TRUE
171 fi
172
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100173 # sanity checks
Vincent Driessena4dd2232010-02-10 00:34:59 +0100174 gitflow_require_branch "$BRANCH"
Vincent Driessen48386442010-01-29 10:30:40 +0100175 gitflow_require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100176 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100177 git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100178 die "Could not fetch $MASTER_BRANCH from $ORIGIN."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100179 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100180 die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100181 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100182 gitflow_require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
183 gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100184
Vincent Driessen5fa47582010-02-09 00:31:33 +0100185 # try to merge into master
186 # in case a previous attempt to finish this release branch has failed,
187 # but the merge into master was successful, we skip it now
Vincent Driessena4dd2232010-02-10 00:34:59 +0100188 if ! gitflow_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
189 git checkout "$MASTER_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100190 die "Could not check out $MASTER_BRANCH."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100191 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100192 die "There were merge conflicts."
193 # TODO: What do we do now?
194 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100195
Vincent Driessen5fa47582010-02-09 00:31:33 +0100196 # try to tag the release
197 # in case a previous attempt to finish this release branch has failed,
198 # but the tag was set successful, we skip it now
Vincent Driessenf46e2902010-02-15 23:01:52 +0100199 local tagname=$VERSION_PREFIX$VERSION
Vincent Driessena4dd2232010-02-10 00:34:59 +0100200 if ! gitflow_tag_exists "$tagname"; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100201 local opts="-a"
Vincent Driessen5fa47582010-02-09 00:31:33 +0100202 flag sign && opts="$opts -s"
203 [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
204 [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
205 git tag $opts "$VERSION_PREFIX$VERSION" || \
206 die "Tagging failed. Please run finish again to retry."
207 fi
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100208
Vincent Driessen5fa47582010-02-09 00:31:33 +0100209 # try to merge into develop
210 # in case a previous attempt to finish this release branch has failed,
211 # but the merge into develop was successful, we skip it now
Vincent Driessena4dd2232010-02-10 00:34:59 +0100212 if ! gitflow_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
213 git checkout "$DEVELOP_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100214 die "Could not check out $DEVELOP_BRANCH."
215
Vincent Driessend29e3152010-02-09 00:55:06 +0100216 # TODO: Actually, accounting for 'git describe' pays, so we should
217 # ideally git merge --no-ff $tagname here, instead!
Vincent Driessena4dd2232010-02-10 00:34:59 +0100218 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100219 die "There were merge conflicts."
220 # TODO: What do we do now?
221 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100222
223 # delete branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100224 git branch -d "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100225
Vincent Driessenddba2df2010-02-19 06:42:18 +0100226 if flag push; then
227 git push "$ORIGIN" "$DEVELOP_BRANCH" || \
228 die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
229 git push "$ORIGIN" "$MASTER_BRANCH" || \
230 die "Could not push to $MASTER_BRANCH from $ORIGIN."
231 git push --tags "$ORIGIN" || \
232 die "Could not push tags to $ORIGIN."
233 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100234
235 echo
236 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100237 echo "- Latest objects have been fetched from '$ORIGIN'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100238 echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
Vincent Driessenab3dc492010-01-29 12:35:49 +0100239 echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100240 echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100241 echo "- Hotfix branch '$BRANCH' has been deleted"
Vincent Driessenddba2df2010-02-19 06:42:18 +0100242 if flag push; then
243 echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
244 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100245 echo
246}