blob: b0df9aced4f3e78e2b670e4969d309c393f8b27f [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 Driessenab3dc492010-01-29 12:35:49 +010015VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010016PREFIX=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
17
Benedikt Böhm00ccea62010-01-26 12:39:36 +010018usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010019 echo "usage: git flow hotfix [list] [-v]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010020 echo " git flow hotfix start <version> [<base>]"
Vincent Driessen010252a2010-02-04 10:31:29 +010021 echo " git flow hotfix finish <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010022}
23
Vincent Driessen186d2b52010-01-27 23:48:39 +010024cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010025 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010026}
27
Vincent Driessenb866b012010-01-28 01:01:53 +010028cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010029 DEFINE_boolean verbose false 'verbose (more) output' v
30 parse_args "$@"
31
Vincent Driessen27592dd2010-02-06 14:45:39 +010032 typeset hotfix_branches
33 typeset current_branch
34 typeset short_names
Vincent Driessenc5fcc012010-02-10 00:18:08 +010035 hotfix_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010036 if [ -z "$hotfix_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010037 warn "No hotfix branches exist."
38 exit 0
39 fi
Vincent Driessen27592dd2010-02-06 14:45:39 +010040 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen68845232010-02-10 00:43:21 +010041 short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX g")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010042
Vincent Driessen3c337fb2010-02-04 11:30:18 +010043 # determine column width first
Vincent Driessen27592dd2010-02-06 14:45:39 +010044 typeset -i width=0
45 typeset branch
46 for branch in $short_names; do
Vincent Driessenb673c442010-02-10 00:43:53 +010047 typeset -i len=${#branch}
Vincent Driessen3c337fb2010-02-04 11:30:18 +010048 width=$(max $width $len)
49 done
Vincent Driessen27592dd2010-02-06 14:45:39 +010050 width=width+3
Vincent Driessen3c337fb2010-02-04 11:30:18 +010051
Vincent Driessen27592dd2010-02-06 14:45:39 +010052 typeset branch
53 for branch in $short_names; do
Vincent Driessenc5fcc012010-02-10 00:18:08 +010054 typeset fullname=$PREFIX$branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010055 typeset base=$(git merge-base "$fullname" "$MASTER_BRANCH")
56 typeset master_sha=$(git rev-parse "$MASTER_BRANCH")
57 typeset branch_sha=$(git rev-parse "$fullname")
58 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010059 printf "* "
60 else
61 printf " "
62 fi
63 if flag verbose; then
64 printf "%-${width}s" "$branch"
65 if [ "$branch_sha" = "$master_sha" ]; then
66 printf "(no commits yet)"
67 else
Vincent Driessena4dd2232010-02-10 00:34:59 +010068 typeset tagname=$(git name-rev --tags --no-undefined --name-only "$base")
Vincent Driessen27592dd2010-02-06 14:45:39 +010069 typeset nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +010070 if [ "$tagname" != "" ]; then
Vincent Driessenc5fcc012010-02-10 00:18:08 +010071 nicename=$tagname
Vincent Driessen3c337fb2010-02-04 11:30:18 +010072 else
Vincent Driessena4dd2232010-02-10 00:34:59 +010073 nicename=$(git rev-parse --short "$base")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010074 fi
75 printf "(based on $nicename)"
76 fi
77 else
78 printf "%s" "$branch"
79 fi
80 echo
81 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010082}
83
Benedikt Böhm00ccea62010-01-26 12:39:36 +010084cmd_help() {
85 usage
86 exit 0
87}
88
Vincent Driessenb866b012010-01-28 01:01:53 +010089parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010090 # parse options
91 FLAGS "$@" || exit $?
92 eval set -- "${FLAGS_ARGV}"
93
94 # read arguments into global variables
Vincent Driessenc5fcc012010-02-10 00:18:08 +010095 VERSION=$1
Vincent Driessen3c337fb2010-02-04 11:30:18 +010096 BRANCH=$PREFIX$VERSION
97}
98
99require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100100 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100101 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100102 usage
103 exit 1
104 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100105}
106
Vincent Driessen010252a2010-02-04 10:31:29 +0100107require_base_is_on_master() {
108 if ! git branch --contains "$BASE" 2>/dev/null \
109 | sed 's/[* ] //g' \
110 | grep -q "^$MASTER_BRANCH\$"; then
111 die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
112 fi
113}
114
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100115require_no_existing_hotfix_branches() {
116 local hotfix_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")
117 local first_branch=$(echo ${hotfix_branches} | head -n1)
118 first_branch=${first_branch#$PREFIX}
119 [ -z "$hotfix_branches" ] || \
120 die "There is an existing hotfix branch ($first_branch). Finish that one first."
121}
122
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100123cmd_start() {
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100124 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100125 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100126 BASE=${2:-$MASTER_BRANCH}
Vincent Driessen010252a2010-02-04 10:31:29 +0100127 require_version_arg
128 require_base_is_on_master
Vincent Driessen6d64d2c2010-02-16 06:41:13 +0100129 require_no_existing_hotfix_branches
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100130
131 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100132 gitflow_require_clean_working_tree
Vincent Driessena4dd2232010-02-10 00:34:59 +0100133 gitflow_require_branch_absent "$BRANCH"
134 gitflow_require_tag_absent "$VERSION_PREFIX$VERSION"
Vincent Driessenca73caf2010-02-07 19:46:38 +0100135 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100136 git fetch -q "$ORIGIN" "$MASTER_BRANCH"
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100137 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100138 gitflow_require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100139
140 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100141 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100142
143 echo
144 echo "Summary of actions:"
145 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
146 echo "- You are now on branch '$BRANCH'"
147 echo
148 echo "Follow-up actions:"
149 echo "- Bump the version number now!"
150 echo "- Start committing your hot fixes"
151 echo "- When done, run:"
152 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100153 echo " git flow hotfix finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100154 echo
155}
156
157cmd_finish() {
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100158 DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
159 DEFINE_boolean sign false "sign the release tag cryptographically" s
160 DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
161 DEFINE_string message "" "use the given tag message" m
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100162 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100163 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100164
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100165 # handle flags that imply other flags
166 if [ "$FLAGS_signingkey" != "" ]; then
167 FLAGS_sign=$FLAGS_TRUE
168 fi
169
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100170 # sanity checks
Vincent Driessena4dd2232010-02-10 00:34:59 +0100171 gitflow_require_branch "$BRANCH"
Vincent Driessen48386442010-01-29 10:30:40 +0100172 gitflow_require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100173 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100174 git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100175 die "Could not fetch $MASTER_BRANCH from $ORIGIN."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100176 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100177 die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100178 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100179 gitflow_require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
180 gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100181
Vincent Driessen5fa47582010-02-09 00:31:33 +0100182 # try to merge into master
183 # in case a previous attempt to finish this release branch has failed,
184 # but the merge into master was successful, we skip it now
Vincent Driessena4dd2232010-02-10 00:34:59 +0100185 if ! gitflow_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
186 git checkout "$MASTER_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100187 die "Could not check out $MASTER_BRANCH."
Vincent Driessena4dd2232010-02-10 00:34:59 +0100188 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100189 die "There were merge conflicts."
190 # TODO: What do we do now?
191 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100192
Vincent Driessen5fa47582010-02-09 00:31:33 +0100193 # try to tag the release
194 # in case a previous attempt to finish this release branch has failed,
195 # but the tag was set successful, we skip it now
196 typeset tagname=$VERSION_PREFIX$VERSION
Vincent Driessena4dd2232010-02-10 00:34:59 +0100197 if ! gitflow_tag_exists "$tagname"; then
Vincent Driessen5fa47582010-02-09 00:31:33 +0100198 typeset opts="-a"
199 flag sign && opts="$opts -s"
200 [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
201 [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
202 git tag $opts "$VERSION_PREFIX$VERSION" || \
203 die "Tagging failed. Please run finish again to retry."
204 fi
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100205
Vincent Driessen5fa47582010-02-09 00:31:33 +0100206 # try to merge into develop
207 # in case a previous attempt to finish this release branch has failed,
208 # but the merge into develop was successful, we skip it now
Vincent Driessena4dd2232010-02-10 00:34:59 +0100209 if ! gitflow_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
210 git checkout "$DEVELOP_BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100211 die "Could not check out $DEVELOP_BRANCH."
212
Vincent Driessend29e3152010-02-09 00:55:06 +0100213 # TODO: Actually, accounting for 'git describe' pays, so we should
214 # ideally git merge --no-ff $tagname here, instead!
Vincent Driessena4dd2232010-02-10 00:34:59 +0100215 git merge --no-ff "$BRANCH" || \
Vincent Driessen5fa47582010-02-09 00:31:33 +0100216 die "There were merge conflicts."
217 # TODO: What do we do now?
218 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100219
220 # delete branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100221 git branch -d "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100222
223 # TODO: Implement an optional push to master
224 # git push origin develop; git push origin master; git push --tags origin
225
226 echo
227 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100228 echo "- Latest objects have been fetched from '$ORIGIN'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100229 echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
Vincent Driessenab3dc492010-01-29 12:35:49 +0100230 echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100231 echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100232 echo "- Hotfix branch '$BRANCH' has been deleted"
233 echo
234}