blob: 5e42b15eb78fb2aec343ae25a496d00723af1945 [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
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010015PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
16
Benedikt Böhm00ccea62010-01-26 12:39:36 +010017usage() {
Vincent Driessenb866b012010-01-28 01:01:53 +010018 echo "usage: git flow feature [list]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010019 echo " git flow feature start <name> [<base>]"
Vincent Driessenea608952010-01-29 16:56:29 +010020 echo " git flow feature finish <name|nameprefix> [<base>]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010021 echo " git flow feature publish <name>"
22 echo " git flow feature track <name>"
Vincent Driessenea608952010-01-29 16:56:29 +010023 echo " git flow feature diff <name|nameprefix>"
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 "--option Explanation"
31 #echo ""
32 #echo "finish-only options:"
33 #echo "--rebase Rebases the feature branch on top of develop, instead of merging"
34 #echo "--squash Squashes all commits of the feature branch into a single commit"
35 #echo " on develop"
36 #echo "--push Push to the origin repo when finished"
37}
38
Vincent Driessen186d2b52010-01-27 23:48:39 +010039cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010040 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010041}
42
Vincent Driessenb866b012010-01-28 01:01:53 +010043cmd_list() {
Vincent Driessen1b819232010-02-01 15:51:43 +010044 DEFINE_boolean verbose 0 'verbose (more) output' v
45 parse_args "$@"
46
Vincent Driessen186d2b52010-01-27 23:48:39 +010047 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
48 if [ -z "$FEATURE_BRANCHES" ]; then
49 warn "No feature branches exist."
50 exit 0
51 fi
52 echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g"
53}
54
Benedikt Böhm00ccea62010-01-26 12:39:36 +010055cmd_help() {
56 usage
57 exit 0
58}
59
Vincent Driessen22ef21a2010-01-29 16:43:37 +010060resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010061 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010062 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
63 echo "$1"
64 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010065 fi
66
67 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
68 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +010069 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +010070 # no prefix match, so take it literally
71 echo "$1"
72 else
Vincent Driessen1b819232010-02-01 15:51:43 +010073 if [ $NUM_MATCHES -eq 1 ]; then
74 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
75 # so this one is safe
76 echo "$MATCHES" | sed "s $PREFIX g"
77 else
78 # multiple matches, cannot decide
79 warn "Multiple branches match for prefix '$1':"
80 for match in $MATCHES; do
81 warn "- $match"
82 done
83 die "Aborting. Use an unambiguous prefix."
84 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +010085 fi
86}
87
Vincent Driessen1b819232010-02-01 15:51:43 +010088require_name() {
89 if [ "$NAME" = "" ]; then
90 echo "Missing argument <name>"
91 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +010092 exit 1
93 fi
94}
95
Vincent Driessen1b819232010-02-01 15:51:43 +010096expand_name_arg_prefix() {
97 require_name
98 NAME=$(resolve_name_by_prefix "$NAME")
99 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100100 exit 1
101 fi
102 BRANCH=$PREFIX$NAME
103}
104
Vincent Driessenea608952010-01-29 16:56:29 +0100105parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100106 # parse options
107 FLAGS "$@" || exit $?
108 eval set -- "${FLAGS_ARGV}"
109
110 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100111 NAME="$1"
Vincent Driessen1b819232010-02-01 15:51:43 +0100112 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessenb866b012010-01-28 01:01:53 +0100113}
114
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100115cmd_start() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100116 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100117 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100118 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100119
120 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100121 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100122 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100123
124 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100125 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100126 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100127 fi
128
Vincent Driessene034e4a2010-01-29 12:10:44 +0100129 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
130
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100131 # create branch
132 git checkout -b $BRANCH $BASE
133
134 echo
135 echo "Summary of actions:"
136 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
137 echo "- You are now on branch '$BRANCH'"
138 echo ""
139 echo "Now, start committing on your feature. When done, use:"
140 echo ""
141 echo " git flow finish feature $NAME"
142 echo
143}
144
145cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100146 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
147 DEFINE_boolean rebase 0 'rebase instead of merge' r
148 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
149 parse_args "$@"
150 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100151
152 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100153 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100154
Vincent Driessen49c7d022010-01-28 12:40:33 +0100155 # detect if we're restoring from a merge conflict
156 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
157 #
158 # TODO: detect that we're working on the correct branch here!
159 # The user need not necessarily have given the same $NAME twice here
160 # (although he/she should).
161 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100162
163 # TODO: gitflow_test_clean_working_tree() should provide an alternative
164 # exit code for "unmerged changes in working tree", which we should
165 # actually be testing for here
166 if gitflow_test_clean_working_tree; then
167 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
168
169 # Since the working tree is now clean, either the user did a
170 # succesfull merge manually, or the merge was cancelled.
171 # We detect this using gitflow_is_branch_merged_into()
172 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
173 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
174 helper_finish_cleanup
175 exit 0
176 else
177 # If the user cancelled the merge and decided to wait until later,
178 # that's fine. But we have to acknowledge this by removing the
179 # MERGE_BASE file and continuing normal execution of the finish
180 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
181 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100182 else
183 echo
184 echo "Merge conflicts not resolved yet, use:"
185 echo " git mergetool"
186 echo " git commit"
187 echo
188 echo "You can then complete the finish by running it again:"
189 echo " git flow feature finish $NAME"
190 echo
191 exit 1
192 fi
193 fi
194
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100195 # sanity checks
196 gitflow_require_clean_working_tree
197
Vincent Driessene034e4a2010-01-29 12:10:44 +0100198 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100199 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100200 git fetch -q $ORIGIN $BRANCH
201 fi
202
Benedikt Böhm350e7152010-01-26 13:05:05 +0100203 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
204 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100205 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100206 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100207 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100208 fi
209
210 # merge into BASE
211 git checkout $BASE
212 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
213 git merge --ff $BRANCH
214 else
215 git merge --no-ff $BRANCH
216 fi
217
Vincent Driessen49c7d022010-01-28 12:40:33 +0100218 if [ $? -ne 0 ]; then
219 # oops.. we have a merge conflict!
220 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100221 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100222 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
223 echo
224 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
225 echo " git mergetool"
226 echo " git commit"
227 echo
228 echo "You can then complete the finish by running it again:"
229 echo " git flow feature finish $NAME"
230 echo
231 exit 1
232 fi
233
234 # when no merge conflict is detected, just clean up the feature branch
235 helper_finish_cleanup
236}
237
238helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100239 # sanity checks
240 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100241 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100242
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100243 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100244 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100245 git branch -d $BRANCH
246
247 echo
248 echo "Summary of actions:"
249 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
250 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
251 echo "- Feature branch '$BRANCH' has been removed"
252 echo "- You are now on branch '$BASE'"
253 echo
254}
255
256cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100257 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100258 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100259
260 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100261 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100262 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100263 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100264 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100265
266 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100267 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100268 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100269
270 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100271 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100272 git config branch.$BRANCH.merge refs/heads/$BRANCH
273 git checkout $BRANCH
274
275 echo
276 echo "Summary of actions:"
277 echo "- A new remote branch '$BRANCH' was created"
278 echo "- The local branch '$BRANCH' was configured to track the remote branch"
279 echo "- You are now on branch '$BRANCH'"
280 echo
281}
282
283cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100284 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100285 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100286
287 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100288 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100289 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100290 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100291 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100292
293 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100294 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100295
296 echo
297 echo "Summary of actions:"
298 echo "- A new remote tracking branch '$BRANCH' was created"
299 echo "- You are now on branch '$BRANCH'"
300 echo
301}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100302
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100303cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100304 parse_args "$@"
305 expand_name_arg_prefix
306
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100307 # TODO: if this feature has been based on a non-develop branch, we really
308 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100309 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100310}