blob: 35fed5b12467f20563d1994a0ff5bff0daf7e585 [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/)
Vincent Driessen2acfffd2010-01-29 12:37:22 +010017FLAG_FETCH=1
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010018
Benedikt Böhm00ccea62010-01-26 12:39:36 +010019usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010020 echo "usage: git flow hotfix [list] [-v]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010021 echo " git flow hotfix start <version> [<base>]"
Vincent Driessen010252a2010-02-04 10:31:29 +010022 echo " git flow hotfix finish <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010023}
24
Vincent Driessen186d2b52010-01-27 23:48:39 +010025cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010026 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010027}
28
Vincent Driessenb866b012010-01-28 01:01:53 +010029cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010030 DEFINE_boolean verbose false 'verbose (more) output' v
31 parse_args "$@"
32
Vincent Driessen27592dd2010-02-06 14:45:39 +010033 typeset hotfix_branches
34 typeset current_branch
35 typeset short_names
36 hotfix_branches="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
37 if [ -z "$hotfix_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010038 warn "No hotfix branches exist."
39 exit 0
40 fi
Vincent Driessen27592dd2010-02-06 14:45:39 +010041 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
42 short_names=$(echo "$hotfix_branches" | sed "s?^$PREFIX??g")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010043
Vincent Driessen3c337fb2010-02-04 11:30:18 +010044 # determine column width first
Vincent Driessen27592dd2010-02-06 14:45:39 +010045 typeset -i width=0
46 typeset branch
47 for branch in $short_names; do
48 typeset -i len=$(($(echo "$branch" | wc -c) - 1))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010049 width=$(max $width $len)
50 done
Vincent Driessen27592dd2010-02-06 14:45:39 +010051 width=width+3
Vincent Driessen3c337fb2010-02-04 11:30:18 +010052
Vincent Driessen27592dd2010-02-06 14:45:39 +010053 typeset branch
54 for branch in $short_names; do
55 typeset fullname="$PREFIX$branch"
56 typeset base=$(git merge-base "$fullname" "$MASTER_BRANCH")
57 typeset master_sha=$(git rev-parse "$MASTER_BRANCH")
58 typeset branch_sha=$(git rev-parse "$fullname")
59 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010060 printf "* "
61 else
62 printf " "
63 fi
64 if flag verbose; then
65 printf "%-${width}s" "$branch"
66 if [ "$branch_sha" = "$master_sha" ]; then
67 printf "(no commits yet)"
68 else
Vincent Driessen27592dd2010-02-06 14:45:39 +010069 typeset tagname=$(git name-rev --tags --no-undefined --name-only $base)
70 typeset nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +010071 if [ "$tagname" != "" ]; then
72 nicename="$tagname"
73 else
74 nicename="$(git rev-parse --short $base)"
75 fi
76 printf "(based on $nicename)"
77 fi
78 else
79 printf "%s" "$branch"
80 fi
81 echo
82 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010083}
84
Benedikt Böhm00ccea62010-01-26 12:39:36 +010085cmd_help() {
86 usage
87 exit 0
88}
89
Vincent Driessenb866b012010-01-28 01:01:53 +010090parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010091 # parse options
92 FLAGS "$@" || exit $?
93 eval set -- "${FLAGS_ARGV}"
94
95 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +010096 VERSION="$1"
Vincent Driessen3c337fb2010-02-04 11:30:18 +010097 BRANCH=$PREFIX$VERSION
98}
99
100require_version_arg() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100101 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100102 warn "Missing argument <version>"
Vincent Driessenb866b012010-01-28 01:01:53 +0100103 usage
104 exit 1
105 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100106}
107
Vincent Driessen010252a2010-02-04 10:31:29 +0100108require_base_is_on_master() {
109 if ! git branch --contains "$BASE" 2>/dev/null \
110 | sed 's/[* ] //g' \
111 | grep -q "^$MASTER_BRANCH\$"; then
112 die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
113 fi
114}
115
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100116cmd_start() {
117 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 Driessen2acfffd2010-01-29 12:37:22 +0100124 if [ $FLAG_FETCH -eq 1 ]; then
125 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() {
148 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100149 require_version_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100150
151 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100152 gitflow_require_clean_working_tree
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100153 if [ $FLAG_FETCH -eq 1 ]; then
154 git fetch -q $ORIGIN $MASTER_BRANCH
155 git fetch -q $ORIGIN $DEVELOP_BRANCH
156 fi
Benedikt Böhm350e7152010-01-26 13:05:05 +0100157 gitflow_require_branches_equal $MASTER_BRANCH $ORIGIN/$MASTER_BRANCH
158 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100159
Vincent Driessen010252a2010-02-04 10:31:29 +0100160 # merge into master
161 git checkout $MASTER_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100162 git merge --no-ff $BRANCH
Vincent Driessenab3dc492010-01-29 12:35:49 +0100163 git tag $VERSION_PREFIX$VERSION
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100164
165 # merge into develop if we fixed a master issue
Vincent Driessen010252a2010-02-04 10:31:29 +0100166 git checkout $DEVELOP_BRANCH
167 git merge --no-ff $BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100168
169 # delete branch
170 git branch -d $BRANCH
171
172 # TODO: Implement an optional push to master
173 # git push origin develop; git push origin master; git push --tags origin
174
175 echo
176 echo "Summary of actions:"
Benedikt Böhm350e7152010-01-26 13:05:05 +0100177 echo "- Latest objects have been fetched from '$ORIGIN'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100178 echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
Vincent Driessenab3dc492010-01-29 12:35:49 +0100179 echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
Vincent Driessen010252a2010-02-04 10:31:29 +0100180 echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100181 echo "- Hotfix branch '$BRANCH' has been deleted"
182 echo
183}