blob: f25ae7ff98aaba56252c50f23b4493050235e4f5 [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 Driessenb03cf962010-02-01 22:00:39 +010018 echo "usage: git flow feature [list] [-v]"
19 echo " git flow feature start [-Fv] <name> [<base>]"
Vincent Driessen010252a2010-02-04 10:31:29 +010020 echo " git flow feature finish [-rsFv] <name|nameprefix>"
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>"
Vincent Driessenc62633f2010-02-02 10:48:50 +010024 echo " git flow feature rebase [-i] <name|nameprefix>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010025}
26
Vincent Driessen186d2b52010-01-27 23:48:39 +010027cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010028 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010029}
30
Vincent Driessenb866b012010-01-28 01:01:53 +010031cmd_list() {
Vincent Driessen44174922010-02-02 23:53:21 +010032 DEFINE_boolean verbose false 'verbose (more) output' v
Vincent Driessen1b819232010-02-01 15:51:43 +010033 parse_args "$@"
34
Vincent Driessen27592dd2010-02-06 14:45:39 +010035 typeset feature_branches
36 typeset current_branch
37 typeset short_names
38 feature_branches="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
39 if [ -z "$feature_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010040 warn "No feature branches exist."
41 exit 0
42 fi
Vincent Driessen27592dd2010-02-06 14:45:39 +010043 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
44 short_names=$(echo "$feature_branches" | sed "s ^$PREFIX g")
Vincent Driessenf2536f42010-02-01 15:57:48 +010045
Vincent Driessenaa6d0162010-02-01 19:43:46 +010046 # determine column width first
Vincent Driessen27592dd2010-02-06 14:45:39 +010047 typeset -i width=0
48 typeset branch
49 for branch in $short_names; do
50 typeset -i len=${#branch}
Vincent Driessenaa6d0162010-02-01 19:43:46 +010051 width=$(max $width $len)
52 done
Vincent Driessen27592dd2010-02-06 14:45:39 +010053 width=width+3
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010054
Vincent Driessen27592dd2010-02-06 14:45:39 +010055 typeset branch
56 for branch in $short_names; do
57 typeset fullname="$PREFIX$branch"
58 typeset base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
59 typeset develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
60 typeset branch_sha=$(git rev-parse "$fullname")
61 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessenaa6d0162010-02-01 19:43:46 +010062 printf "* "
63 else
64 printf " "
65 fi
Vincent Driessen44174922010-02-02 23:53:21 +010066 if flag verbose; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010067 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +010068 if [ "$branch_sha" = "$develop_sha" ]; then
69 printf "(no commits yet)"
70 elif [ "$base" = "$branch_sha" ]; then
71 printf "(is behind develop, may ff)"
72 elif [ "$base" = "$develop_sha" ]; then
73 printf "(based on latest develop)"
74 else
75 printf "(may be rebased)"
76 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +010077 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 Driessen22ef21a2010-01-29 16:43:37 +010089resolve_name_by_prefix() {
Vincent Driessen27592dd2010-02-06 14:45:39 +010090 typeset matches
91 typeset -i num_matches
92
Vincent Driessen2e1856b2010-01-29 15:18:13 +010093 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010094 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
95 echo "$1"
96 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010097 fi
98
Vincent Driessen27592dd2010-02-06 14:45:39 +010099 matches="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
100 num_matches=$(echo "$matches" | wc -l)
101 if [ -z "$matches" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100102 # no prefix match, so take it literally
103 echo "$1"
104 else
Vincent Driessen27592dd2010-02-06 14:45:39 +0100105 if [ $num_matches -eq 1 ]; then
106 echo "${matches#$PREFIX}"
Vincent Driessen1b819232010-02-01 15:51:43 +0100107 else
108 # multiple matches, cannot decide
109 warn "Multiple branches match for prefix '$1':"
Vincent Driessen27592dd2010-02-06 14:45:39 +0100110 for match in $matches; do
Vincent Driessen1b819232010-02-01 15:51:43 +0100111 warn "- $match"
112 done
113 die "Aborting. Use an unambiguous prefix."
114 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100115 fi
116}
117
Vincent Driessen1b819232010-02-01 15:51:43 +0100118require_name() {
119 if [ "$NAME" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100120 warn "Missing argument <name>"
Vincent Driessen1b819232010-02-01 15:51:43 +0100121 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100122 exit 1
123 fi
124}
125
Vincent Driessen1b819232010-02-01 15:51:43 +0100126expand_name_arg_prefix() {
127 require_name
128 NAME=$(resolve_name_by_prefix "$NAME")
129 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100130 exit 1
131 fi
132 BRANCH=$PREFIX$NAME
133}
134
Vincent Driessenc62633f2010-02-02 10:48:50 +0100135expand_name_arg_prefix_or_current() {
Vincent Driessen27592dd2010-02-06 14:45:39 +0100136 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessenc62633f2010-02-02 10:48:50 +0100137 if [ "$NAME" != "" ]; then
138 expand_name_arg_prefix
Vincent Driessen27592dd2010-02-06 14:45:39 +0100139 elif [ "$current_branch" != "" ]; then
140 BRANCH="$current_branch"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100141 NAME=$(echo "$BRANCH" | sed "s?$PREFIX??g")
142 else
143 warn "The current HEAD is no feature branch."
144 warn "To diff a feature, specify a <name> argument."
145 usage
146 exit 1
147 fi
148}
149
Vincent Driessenea608952010-01-29 16:56:29 +0100150parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100151 # parse options
152 FLAGS "$@" || exit $?
153 eval set -- "${FLAGS_ARGV}"
154
155 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100156 NAME="$1"
Vincent Driessen11560922010-02-01 21:58:37 +0100157 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100158}
159
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160cmd_start() {
Vincent Driessen44174922010-02-02 23:53:21 +0100161 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100162 DEFINE_boolean force false 'force creation of feature branch (ignores dirty working tree)' f
Vincent Driessenea608952010-01-29 16:56:29 +0100163 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100164 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessen1b819232010-02-01 15:51:43 +0100165 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100166
167 # sanity checks
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100168 if noflag force; then
169 gitflow_require_clean_working_tree
170 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100171 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100172
173 # update the local repo with remote changes, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100174 if flag fetch; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100175 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100176 fi
177
Vincent Driessene034e4a2010-01-29 12:10:44 +0100178 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
179
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100180 # create branch
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100181 if ! git checkout -b $BRANCH $BASE; then
182 die "Could not create feature branch '$BRANCH'"
183 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100184
185 echo
186 echo "Summary of actions:"
187 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
188 echo "- You are now on branch '$BRANCH'"
189 echo ""
190 echo "Now, start committing on your feature. When done, use:"
191 echo ""
192 echo " git flow finish feature $NAME"
193 echo
194}
195
196cmd_finish() {
Vincent Driessen44174922010-02-02 23:53:21 +0100197 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
198 DEFINE_boolean rebase false 'rebase instead of merge' r
199 DEFINE_boolean squash false 'squash all commits when rebasing (implies --rebase)' s
Vincent Driessen1b819232010-02-01 15:51:43 +0100200 parse_args "$@"
201 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100202
203 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100204 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100205
Vincent Driessen49c7d022010-01-28 12:40:33 +0100206 # detect if we're restoring from a merge conflict
207 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
208 #
209 # TODO: detect that we're working on the correct branch here!
210 # The user need not necessarily have given the same $NAME twice here
211 # (although he/she should).
212 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100213
214 # TODO: gitflow_test_clean_working_tree() should provide an alternative
215 # exit code for "unmerged changes in working tree", which we should
216 # actually be testing for here
217 if gitflow_test_clean_working_tree; then
218 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
219
220 # Since the working tree is now clean, either the user did a
221 # succesfull merge manually, or the merge was cancelled.
222 # We detect this using gitflow_is_branch_merged_into()
223 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
224 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
225 helper_finish_cleanup
226 exit 0
227 else
228 # If the user cancelled the merge and decided to wait until later,
229 # that's fine. But we have to acknowledge this by removing the
230 # MERGE_BASE file and continuing normal execution of the finish
231 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
232 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100233 else
234 echo
235 echo "Merge conflicts not resolved yet, use:"
236 echo " git mergetool"
237 echo " git commit"
238 echo
239 echo "You can then complete the finish by running it again:"
240 echo " git flow feature finish $NAME"
241 echo
242 exit 1
243 fi
244 fi
245
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100246 # sanity checks
247 gitflow_require_clean_working_tree
248
Vincent Driessene034e4a2010-01-29 12:10:44 +0100249 # update local repo with remote changes first, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100250 if flag fetch; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100251 git fetch -q $ORIGIN $BRANCH
252 fi
253
Benedikt Böhm350e7152010-01-26 13:05:05 +0100254 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
255 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100256 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100257 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100258
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100259 # if the user wants to rebase, do that first
Vincent Driessen44174922010-02-02 23:53:21 +0100260 if flag rebase; then
Vincent Driessen010252a2010-02-04 10:31:29 +0100261 if ! git flow feature rebase "$NAME" "$DEVELOP_BRANCH"; then
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100262 warn "Finish was aborted due to conflicts during rebase."
263 warn "Please finish the rebase manually now."
264 warn "When finished, re-run:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100265 warn " git flow feature finish '$NAME' '$DEVELOP_BRANCH'"
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100266 exit 1
267 fi
268 fi
269
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100270 # merge into BASE
Vincent Driessen010252a2010-02-04 10:31:29 +0100271 git checkout $DEVELOP_BRANCH
272 if [ "$(git rev-list -n2 $DEVELOP_BRANCH..$BRANCH | wc -l)" -eq 1 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100273 git merge --ff $BRANCH
274 else
275 git merge --no-ff $BRANCH
276 fi
277
Vincent Driessen49c7d022010-01-28 12:40:33 +0100278 if [ $? -ne 0 ]; then
279 # oops.. we have a merge conflict!
Vincent Driessen010252a2010-02-04 10:31:29 +0100280 # write the given $DEVELOP_BRANCH to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100281 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen010252a2010-02-04 10:31:29 +0100282 echo "$DEVELOP_BRANCH" > "$GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100283 echo
284 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
285 echo " git mergetool"
286 echo " git commit"
287 echo
288 echo "You can then complete the finish by running it again:"
289 echo " git flow feature finish $NAME"
290 echo
291 exit 1
292 fi
293
294 # when no merge conflict is detected, just clean up the feature branch
295 helper_finish_cleanup
296}
297
298helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100299 # sanity checks
300 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100301 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100302
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100303 # delete branch
Vincent Driessen44174922010-02-02 23:53:21 +0100304 if flag fetch; then
Vincent Driesseneec73c62010-02-02 16:14:16 +0100305 git push $ORIGIN :refs/heads/$BRANCH
306 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100307 git branch -d $BRANCH
308
309 echo
310 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100311 echo "- The feature branch '$BRANCH' was merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100312 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
313 echo "- Feature branch '$BRANCH' has been removed"
Vincent Driessen010252a2010-02-04 10:31:29 +0100314 echo "- You are now on branch '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100315 echo
316}
317
318cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100319 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100320 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100321
322 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100323 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100324 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100325 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100326 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100327
328 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100329 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100330 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100331
332 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100333 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100334 git config branch.$BRANCH.merge refs/heads/$BRANCH
335 git checkout $BRANCH
336
337 echo
338 echo "Summary of actions:"
339 echo "- A new remote branch '$BRANCH' was created"
340 echo "- The local branch '$BRANCH' was configured to track the remote branch"
341 echo "- You are now on branch '$BRANCH'"
342 echo
343}
344
345cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100346 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100347 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100348
349 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100350 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100351 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100352 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100353 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100354
355 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100356 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100357
358 echo
359 echo "Summary of actions:"
360 echo "- A new remote tracking branch '$BRANCH' was created"
361 echo "- You are now on branch '$BRANCH'"
362 echo
363}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100364
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100365cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100366 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100367
Vincent Driessen5474e462010-02-04 15:50:06 +0100368 if [ "$NAME" != "" ]; then
369 expand_name_arg_prefix_or_current
370 BASE=$(git merge-base $DEVELOP_BRANCH $BRANCH)
371 git diff $BASE..$BRANCH
372 else
Vincent Driessen27592dd2010-02-06 14:45:39 +0100373 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
374 if ! echo "$current_branch" | grep -q "^$PREFIX"; then
Vincent Driessen5474e462010-02-04 15:50:06 +0100375 die "Not on a feature branch. Name one explicitly."
376 fi
377
378 BASE=$(git merge-base $DEVELOP_BRANCH HEAD)
379 git diff $BASE
380 fi
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100381}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100382
383cmd_rebase() {
Vincent Driessen44174922010-02-02 23:53:21 +0100384 DEFINE_boolean interactive false 'do an interactive rebase' i
Vincent Driessenc62633f2010-02-02 10:48:50 +0100385 parse_args "$@"
386 expand_name_arg_prefix_or_current
387 warn "Will try to rebase '$NAME'..."
388 gitflow_require_clean_working_tree
389 gitflow_require_branch "$BRANCH"
390
391 git checkout -q "$BRANCH"
Vincent Driessen27592dd2010-02-06 14:45:39 +0100392 typeset OPTS=
Vincent Driessen44174922010-02-02 23:53:21 +0100393 if flag interactive; then
Vincent Driessenc62633f2010-02-02 10:48:50 +0100394 OPTS="$OPTS -i"
395 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100396 git rebase $OPTS "$DEVELOP_BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100397}