blob: 7ffa39b85ec3a0e2a1bfccd16c6305e7db2d9b4e [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
35 hotfix_branches="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
36 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')
41 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
47 typeset -i len=$(($(echo "$branch" | wc -c) - 1))
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
54 typeset fullname="$PREFIX$branch"
55 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 Driessen27592dd2010-02-06 14:45:39 +010068 typeset tagname=$(git name-rev --tags --no-undefined --name-only $base)
69 typeset nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +010070 if [ "$tagname" != "" ]; then
71 nicename="$tagname"
72 else
73 nicename="$(git rev-parse --short $base)"
74 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 Driessenb866b012010-01-28 01:01:53 +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
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100115cmd_start() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100116 DEFINE_boolean fetch true "fetch from $ORIGIN before finishing hotfix" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100117 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100118 BASE="${2:-$MASTER_BRANCH}"
119 require_version_arg
120 require_base_is_on_master
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100121
122 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100123 gitflow_require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100124 if flag fetch; then
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100125 git fetch -q $ORIGIN $MASTER_BRANCH
126 fi
Benedikt Böhm350e7152010-01-26 13:05:05 +0100127 gitflow_require_branches_equal $MASTER_BRANCH $ORIGIN/$MASTER_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100128 gitflow_require_branch_absent $BRANCH
129
130 # create branch
131 git checkout -b $BRANCH $BASE
132
133 echo
134 echo "Summary of actions:"
135 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
136 echo "- You are now on branch '$BRANCH'"
137 echo
138 echo "Follow-up actions:"
139 echo "- Bump the version number now!"
140 echo "- Start committing your hot fixes"
141 echo "- When done, run:"
142 echo
Vincent Driessen010252a2010-02-04 10:31:29 +0100143 echo " git flow hotfix finish '$VERSION'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100144 echo
145}
146
147cmd_finish() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100148 DEFINE_boolean fetch true "fetch from $ORIGIN before finishing hotfix" F
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100149 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100150 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100151
152 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100153 gitflow_require_clean_working_tree
Vincent Driessenca73caf2010-02-07 19:46:38 +0100154 if flag fetch; then
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100155 git fetch -q $ORIGIN $MASTER_BRANCH
156 git fetch -q $ORIGIN $DEVELOP_BRANCH
157 fi
Benedikt Böhm350e7152010-01-26 13:05:05 +0100158 gitflow_require_branches_equal $MASTER_BRANCH $ORIGIN/$MASTER_BRANCH
159 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160
Vincent Driessen010252a2010-02-04 10:31:29 +0100161 # merge into master
162 git checkout $MASTER_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100163 git merge --no-ff $BRANCH
Vincent Driessenab3dc492010-01-29 12:35:49 +0100164 git tag $VERSION_PREFIX$VERSION
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100165
166 # merge into develop if we fixed a master issue
Vincent Driessen010252a2010-02-04 10:31:29 +0100167 git checkout $DEVELOP_BRANCH
168 git merge --no-ff $BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100169
170 # delete branch
171 git branch -d $BRANCH
172
173 # TODO: Implement an optional push to master
174 # git push origin develop; git push origin master; git push --tags origin
175
176 echo
177 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100178 echo "- Latest objects have been fetched from '$ORIGIN'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100179 echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
Vincent Driessenab3dc492010-01-29 12:35:49 +0100180 echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100181 echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100182 echo "- Hotfix branch '$BRANCH' has been deleted"
183 echo
184}