blob: 33dab30237860a7bc3e497d490e3c01577580138 [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 Driessen186d2b52010-01-27 23:48:39 +010035 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
36 if [ -z "$FEATURE_BRANCHES" ]; then
37 warn "No feature branches exist."
38 exit 0
39 fi
Vincent Driessenf2536f42010-02-01 15:57:48 +010040
Vincent Driessenaa6d0162010-02-01 19:43:46 +010041 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessenf2536f42010-02-01 15:57:48 +010042 SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
Vincent Driessenaa6d0162010-02-01 19:43:46 +010043 # determine column width first
44 width=0
45 for branch in $SHORT_NAMES; do
46 len=$(($(echo "$branch" | wc -c) - 1))
47 width=$(max $width $len)
48 done
49 width=$(($width + 3))
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010050
Vincent Driessenaa6d0162010-02-01 19:43:46 +010051 for branch in $SHORT_NAMES; do
52 fullname="$PREFIX$branch"
53 base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
54 develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
55 branch_sha=$(git rev-parse "$fullname")
56 if [ "$fullname" = "$CURRENT_BRANCH" ]; then
57 printf "* "
58 else
59 printf " "
60 fi
Vincent Driessen44174922010-02-02 23:53:21 +010061 if flag verbose; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010062 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +010063 if [ "$branch_sha" = "$develop_sha" ]; then
64 printf "(no commits yet)"
65 elif [ "$base" = "$branch_sha" ]; then
66 printf "(is behind develop, may ff)"
67 elif [ "$base" = "$develop_sha" ]; then
68 printf "(based on latest develop)"
69 else
70 printf "(may be rebased)"
71 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +010072 else
73 printf "%s" "$branch"
74 fi
75 echo
76 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010077}
78
Benedikt Böhm00ccea62010-01-26 12:39:36 +010079cmd_help() {
80 usage
81 exit 0
82}
83
Vincent Driessen22ef21a2010-01-29 16:43:37 +010084resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010085 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010086 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
87 echo "$1"
88 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010089 fi
90
91 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
92 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +010093 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +010094 # no prefix match, so take it literally
95 echo "$1"
96 else
Vincent Driessen1b819232010-02-01 15:51:43 +010097 if [ $NUM_MATCHES -eq 1 ]; then
98 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
99 # so this one is safe
100 echo "$MATCHES" | sed "s $PREFIX g"
101 else
102 # multiple matches, cannot decide
103 warn "Multiple branches match for prefix '$1':"
104 for match in $MATCHES; do
105 warn "- $match"
106 done
107 die "Aborting. Use an unambiguous prefix."
108 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100109 fi
110}
111
Vincent Driessen1b819232010-02-01 15:51:43 +0100112require_name() {
113 if [ "$NAME" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100114 warn "Missing argument <name>"
Vincent Driessen1b819232010-02-01 15:51:43 +0100115 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100116 exit 1
117 fi
118}
119
Vincent Driessen1b819232010-02-01 15:51:43 +0100120expand_name_arg_prefix() {
121 require_name
122 NAME=$(resolve_name_by_prefix "$NAME")
123 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100124 exit 1
125 fi
126 BRANCH=$PREFIX$NAME
127}
128
Vincent Driessenc62633f2010-02-02 10:48:50 +0100129expand_name_arg_prefix_or_current() {
130 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
131 if [ "$NAME" != "" ]; then
132 expand_name_arg_prefix
133 elif [ "$CURRENT_BRANCH" != "" ]; then
134 BRANCH="$CURRENT_BRANCH"
135 NAME=$(echo "$BRANCH" | sed "s?$PREFIX??g")
136 else
137 warn "The current HEAD is no feature branch."
138 warn "To diff a feature, specify a <name> argument."
139 usage
140 exit 1
141 fi
142}
143
Vincent Driessenea608952010-01-29 16:56:29 +0100144parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100145 # parse options
146 FLAGS "$@" || exit $?
147 eval set -- "${FLAGS_ARGV}"
148
149 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100150 NAME="$1"
Vincent Driessen11560922010-02-01 21:58:37 +0100151 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100152}
153
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100154cmd_start() {
Vincent Driessen44174922010-02-02 23:53:21 +0100155 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100156 DEFINE_boolean force false 'force creation of feature branch (ignores dirty working tree)' f
Vincent Driessenea608952010-01-29 16:56:29 +0100157 parse_args "$@"
Vincent Driessen010252a2010-02-04 10:31:29 +0100158 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessen1b819232010-02-01 15:51:43 +0100159 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160
161 # sanity checks
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100162 if noflag force; then
163 gitflow_require_clean_working_tree
164 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100165 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100166
167 # update the local repo with remote changes, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100168 if flag fetch; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100169 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100170 fi
171
Vincent Driessene034e4a2010-01-29 12:10:44 +0100172 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
173
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100174 # create branch
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100175 if ! git checkout -b $BRANCH $BASE; then
176 die "Could not create feature branch '$BRANCH'"
177 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100178
179 echo
180 echo "Summary of actions:"
181 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
182 echo "- You are now on branch '$BRANCH'"
183 echo ""
184 echo "Now, start committing on your feature. When done, use:"
185 echo ""
186 echo " git flow finish feature $NAME"
187 echo
188}
189
190cmd_finish() {
Vincent Driessen44174922010-02-02 23:53:21 +0100191 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
192 DEFINE_boolean rebase false 'rebase instead of merge' r
193 DEFINE_boolean squash false 'squash all commits when rebasing (implies --rebase)' s
Vincent Driessen1b819232010-02-01 15:51:43 +0100194 parse_args "$@"
195 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100196
197 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100198 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100199
Vincent Driessen49c7d022010-01-28 12:40:33 +0100200 # detect if we're restoring from a merge conflict
201 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
202 #
203 # TODO: detect that we're working on the correct branch here!
204 # The user need not necessarily have given the same $NAME twice here
205 # (although he/she should).
206 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100207
208 # TODO: gitflow_test_clean_working_tree() should provide an alternative
209 # exit code for "unmerged changes in working tree", which we should
210 # actually be testing for here
211 if gitflow_test_clean_working_tree; then
212 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
213
214 # Since the working tree is now clean, either the user did a
215 # succesfull merge manually, or the merge was cancelled.
216 # We detect this using gitflow_is_branch_merged_into()
217 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
218 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
219 helper_finish_cleanup
220 exit 0
221 else
222 # If the user cancelled the merge and decided to wait until later,
223 # that's fine. But we have to acknowledge this by removing the
224 # MERGE_BASE file and continuing normal execution of the finish
225 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
226 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100227 else
228 echo
229 echo "Merge conflicts not resolved yet, use:"
230 echo " git mergetool"
231 echo " git commit"
232 echo
233 echo "You can then complete the finish by running it again:"
234 echo " git flow feature finish $NAME"
235 echo
236 exit 1
237 fi
238 fi
239
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100240 # sanity checks
241 gitflow_require_clean_working_tree
242
Vincent Driessene034e4a2010-01-29 12:10:44 +0100243 # update local repo with remote changes first, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100244 if flag fetch; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100245 git fetch -q $ORIGIN $BRANCH
246 fi
247
Benedikt Böhm350e7152010-01-26 13:05:05 +0100248 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
249 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100250 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100251 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100252
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100253 # if the user wants to rebase, do that first
Vincent Driessen44174922010-02-02 23:53:21 +0100254 if flag rebase; then
Vincent Driessen010252a2010-02-04 10:31:29 +0100255 if ! git flow feature rebase "$NAME" "$DEVELOP_BRANCH"; then
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100256 warn "Finish was aborted due to conflicts during rebase."
257 warn "Please finish the rebase manually now."
258 warn "When finished, re-run:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100259 warn " git flow feature finish '$NAME' '$DEVELOP_BRANCH'"
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100260 exit 1
261 fi
262 fi
263
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100264 # merge into BASE
Vincent Driessen010252a2010-02-04 10:31:29 +0100265 git checkout $DEVELOP_BRANCH
266 if [ "$(git rev-list -n2 $DEVELOP_BRANCH..$BRANCH | wc -l)" -eq 1 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100267 git merge --ff $BRANCH
268 else
269 git merge --no-ff $BRANCH
270 fi
271
Vincent Driessen49c7d022010-01-28 12:40:33 +0100272 if [ $? -ne 0 ]; then
273 # oops.. we have a merge conflict!
Vincent Driessen010252a2010-02-04 10:31:29 +0100274 # write the given $DEVELOP_BRANCH to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100275 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen010252a2010-02-04 10:31:29 +0100276 echo "$DEVELOP_BRANCH" > "$GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100277 echo
278 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
279 echo " git mergetool"
280 echo " git commit"
281 echo
282 echo "You can then complete the finish by running it again:"
283 echo " git flow feature finish $NAME"
284 echo
285 exit 1
286 fi
287
288 # when no merge conflict is detected, just clean up the feature branch
289 helper_finish_cleanup
290}
291
292helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100293 # sanity checks
294 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100295 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100296
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100297 # delete branch
Vincent Driessen44174922010-02-02 23:53:21 +0100298 if flag fetch; then
Vincent Driesseneec73c62010-02-02 16:14:16 +0100299 git push $ORIGIN :refs/heads/$BRANCH
300 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100301 git branch -d $BRANCH
302
303 echo
304 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100305 echo "- The feature branch '$BRANCH' was merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100306 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
307 echo "- Feature branch '$BRANCH' has been removed"
Vincent Driessen010252a2010-02-04 10:31:29 +0100308 echo "- You are now on branch '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100309 echo
310}
311
312cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100313 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100314 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100315
316 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100317 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100318 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100319 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100320 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100321
322 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100323 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100324 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100325
326 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100327 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100328 git config branch.$BRANCH.merge refs/heads/$BRANCH
329 git checkout $BRANCH
330
331 echo
332 echo "Summary of actions:"
333 echo "- A new remote branch '$BRANCH' was created"
334 echo "- The local branch '$BRANCH' was configured to track the remote branch"
335 echo "- You are now on branch '$BRANCH'"
336 echo
337}
338
339cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100340 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100341 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100342
343 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100344 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100345 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100346 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100347 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100348
349 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100350 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100351
352 echo
353 echo "Summary of actions:"
354 echo "- A new remote tracking branch '$BRANCH' was created"
355 echo "- You are now on branch '$BRANCH'"
356 echo
357}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100358
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100359cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100360 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100361
Vincent Driessen5474e462010-02-04 15:50:06 +0100362 if [ "$NAME" != "" ]; then
363 expand_name_arg_prefix_or_current
364 BASE=$(git merge-base $DEVELOP_BRANCH $BRANCH)
365 git diff $BASE..$BRANCH
366 else
367 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
368 if ! echo "$CURRENT_BRANCH" | grep -q "^$PREFIX"; then
369 die "Not on a feature branch. Name one explicitly."
370 fi
371
372 BASE=$(git merge-base $DEVELOP_BRANCH HEAD)
373 git diff $BASE
374 fi
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100375}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100376
377cmd_rebase() {
Vincent Driessen44174922010-02-02 23:53:21 +0100378 DEFINE_boolean interactive false 'do an interactive rebase' i
Vincent Driessenc62633f2010-02-02 10:48:50 +0100379 parse_args "$@"
380 expand_name_arg_prefix_or_current
381 warn "Will try to rebase '$NAME'..."
382 gitflow_require_clean_working_tree
383 gitflow_require_branch "$BRANCH"
384
385 git checkout -q "$BRANCH"
386 OPTS=
Vincent Driessen44174922010-02-02 23:53:21 +0100387 if flag interactive; then
Vincent Driessenc62633f2010-02-02 10:48:50 +0100388 OPTS="$OPTS -i"
389 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100390 git rebase $OPTS "$DEVELOP_BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100391}