blob: db45d81158090e620544e6f3961bfe7b68bde45f [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
Vincent Driessenc5fcc012010-02-10 00:18:08 +010038 feature_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010039 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
Vincent Driessenc5fcc012010-02-10 00:18:08 +010057 typeset fullname=$PREFIX$branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010058 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 Driessend0991262010-02-06 21:19:07 +010089require_name_arg() {
Vincent Driessen1b819232010-02-01 15:51:43 +010090 if [ "$NAME" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010091 warn "Missing argument <name>"
Vincent Driessen1b819232010-02-01 15:51:43 +010092 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +010093 exit 1
94 fi
95}
96
Vincent Driessend0991262010-02-06 21:19:07 +010097expand_nameprefix_arg() {
98 require_name_arg
99
100 typeset expanded_name
101 typeset -i exitcode
102 expanded_name=$(resolve_nameprefix "$NAME" "$PREFIX")
103 exitcode=$?
104 case $exitcode in
105 0) NAME=$expanded_name
106 BRANCH=$PREFIX$NAME
107 ;;
108 *) exit 1 ;;
109 esac
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100110}
111
Vincent Driessend0991262010-02-06 21:19:07 +0100112expand_nameprefix_arg_or_current() {
Vincent Driessenc62633f2010-02-02 10:48:50 +0100113 if [ "$NAME" != "" ]; then
Vincent Driessend0991262010-02-06 21:19:07 +0100114 expand_nameprefix_arg
115 gitflow_require_branch "$PREFIX$NAME"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100116 else
Vincent Driessend0991262010-02-06 21:19:07 +0100117 typeset current_branch=$(gitflow_current_branch)
118 if startswith "$current_branch" "$PREFIX"; then
119 BRANCH=$current_branch
120 NAME=${BRANCH#$PREFIX}
121 else
122 warn "The current HEAD is no feature branch."
123 warn "To diff a feature, specify a <name> argument."
124 usage
125 exit 1
126 fi
Vincent Driessenc62633f2010-02-02 10:48:50 +0100127 fi
128}
129
Vincent Driessenea608952010-01-29 16:56:29 +0100130parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100131 # parse options
132 FLAGS "$@" || exit $?
133 eval set -- "${FLAGS_ARGV}"
134
135 # read arguments into global variables
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100136 NAME=$1
Vincent Driessen11560922010-02-01 21:58:37 +0100137 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100138}
139
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100140cmd_start() {
Vincent Driessen44174922010-02-02 23:53:21 +0100141 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100142 DEFINE_boolean force false 'force creation of feature branch (ignores dirty working tree)' f
Vincent Driessenea608952010-01-29 16:56:29 +0100143 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100144 BASE=${2:-$DEVELOP_BRANCH}
Vincent Driessend0991262010-02-06 21:19:07 +0100145 require_name_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100146
147 # sanity checks
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100148 if noflag force; then
149 gitflow_require_clean_working_tree
150 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100151 gitflow_require_branch_absent "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100152
153 # update the local repo with remote changes, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100154 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100155 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100156 fi
157
Vincent Driessena4dd2232010-02-10 00:34:59 +0100158 gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100159
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100161 if ! git checkout -b "$BRANCH" "$BASE"; then
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100162 die "Could not create feature branch '$BRANCH'"
163 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100164
165 echo
166 echo "Summary of actions:"
167 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
168 echo "- You are now on branch '$BRANCH'"
169 echo ""
170 echo "Now, start committing on your feature. When done, use:"
171 echo ""
172 echo " git flow finish feature $NAME"
173 echo
174}
175
176cmd_finish() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100177 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Vincent Driessen44174922010-02-02 23:53:21 +0100178 DEFINE_boolean rebase false 'rebase instead of merge' r
179 DEFINE_boolean squash false 'squash all commits when rebasing (implies --rebase)' s
Vincent Driessen1b819232010-02-01 15:51:43 +0100180 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100181 expand_nameprefix_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100182
183 # sanity checks
Vincent Driessena4dd2232010-02-10 00:34:59 +0100184 gitflow_require_branch "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100185
Vincent Driessen49c7d022010-01-28 12:40:33 +0100186 # detect if we're restoring from a merge conflict
187 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
188 #
189 # TODO: detect that we're working on the correct branch here!
190 # The user need not necessarily have given the same $NAME twice here
191 # (although he/she should).
192 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100193
194 # TODO: gitflow_test_clean_working_tree() should provide an alternative
195 # exit code for "unmerged changes in working tree", which we should
196 # actually be testing for here
197 if gitflow_test_clean_working_tree; then
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100198 FINISH_BASE=$(cat "$GIT_DIR/.gitflow/MERGE_BASE")
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100199
200 # Since the working tree is now clean, either the user did a
201 # succesfull merge manually, or the merge was cancelled.
202 # We detect this using gitflow_is_branch_merged_into()
Vincent Driessena4dd2232010-02-10 00:34:59 +0100203 if gitflow_is_branch_merged_into "$BRANCH" "$FINISH_BASE"; then
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100204 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
205 helper_finish_cleanup
206 exit 0
207 else
208 # If the user cancelled the merge and decided to wait until later,
209 # that's fine. But we have to acknowledge this by removing the
210 # MERGE_BASE file and continuing normal execution of the finish
211 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
212 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100213 else
214 echo
215 echo "Merge conflicts not resolved yet, use:"
216 echo " git mergetool"
217 echo " git commit"
218 echo
219 echo "You can then complete the finish by running it again:"
220 echo " git flow feature finish $NAME"
221 echo
222 exit 1
223 fi
224 fi
225
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100226 # sanity checks
227 gitflow_require_clean_working_tree
228
Vincent Driessene034e4a2010-01-29 12:10:44 +0100229 # update local repo with remote changes first, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100230 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100231 git fetch -q "$ORIGIN" "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100232 fi
233
Vincent Driessena4dd2232010-02-10 00:34:59 +0100234 if has "$ORIGIN/$BRANCH" "$REMOTE_BRANCHES"; then
235 gitflow_require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100236 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100237 gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100238
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100239 # if the user wants to rebase, do that first
Vincent Driessen44174922010-02-02 23:53:21 +0100240 if flag rebase; then
Vincent Driessen010252a2010-02-04 10:31:29 +0100241 if ! git flow feature rebase "$NAME" "$DEVELOP_BRANCH"; then
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100242 warn "Finish was aborted due to conflicts during rebase."
243 warn "Please finish the rebase manually now."
244 warn "When finished, re-run:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100245 warn " git flow feature finish '$NAME' '$DEVELOP_BRANCH'"
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100246 exit 1
247 fi
248 fi
249
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100250 # merge into BASE
Vincent Driessena4dd2232010-02-10 00:34:59 +0100251 git checkout "$DEVELOP_BRANCH"
252 if [ "$(git rev-list -n2 "$DEVELOP_BRANCH..$BRANCH" | wc -l)" -eq 1 ]; then
253 git merge --ff "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100254 else
Vincent Driessena4dd2232010-02-10 00:34:59 +0100255 git merge --no-ff "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100256 fi
257
Vincent Driessen49c7d022010-01-28 12:40:33 +0100258 if [ $? -ne 0 ]; then
259 # oops.. we have a merge conflict!
Vincent Driessen010252a2010-02-04 10:31:29 +0100260 # write the given $DEVELOP_BRANCH to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100261 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen010252a2010-02-04 10:31:29 +0100262 echo "$DEVELOP_BRANCH" > "$GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100263 echo
264 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
265 echo " git mergetool"
266 echo " git commit"
267 echo
268 echo "You can then complete the finish by running it again:"
269 echo " git flow feature finish $NAME"
270 echo
271 exit 1
272 fi
273
274 # when no merge conflict is detected, just clean up the feature branch
275 helper_finish_cleanup
276}
277
278helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100279 # sanity checks
Vincent Driessena4dd2232010-02-10 00:34:59 +0100280 gitflow_require_branch "$BRANCH"
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100281 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100282
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100283 # delete branch
Vincent Driessen44174922010-02-02 23:53:21 +0100284 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100285 git push "$ORIGIN" ":refs/heads/$BRANCH"
Vincent Driesseneec73c62010-02-02 16:14:16 +0100286 fi
Vincent Driessena4dd2232010-02-10 00:34:59 +0100287 git branch -d "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100288
289 echo
290 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100291 echo "- The feature branch '$BRANCH' was merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100292 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
293 echo "- Feature branch '$BRANCH' has been removed"
Vincent Driessen010252a2010-02-04 10:31:29 +0100294 echo "- You are now on branch '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100295 echo
296}
297
298cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100299 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100300 expand_nameprefix_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100301
302 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100303 gitflow_require_clean_working_tree
Vincent Driessena4dd2232010-02-10 00:34:59 +0100304 gitflow_require_branch "$BRANCH"
305 git fetch -q "$ORIGIN"
306 gitflow_require_branch_absent "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100307
308 # create remote branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100309 git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
310 git fetch -q "$ORIGIN"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100311
312 # configure remote tracking
Vincent Driessena4dd2232010-02-10 00:34:59 +0100313 git config "branch.$BRANCH.remote" "$ORIGIN"
314 git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
315 git checkout "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100316
317 echo
318 echo "Summary of actions:"
319 echo "- A new remote branch '$BRANCH' was created"
320 echo "- The local branch '$BRANCH' was configured to track the remote branch"
321 echo "- You are now on branch '$BRANCH'"
322 echo
323}
324
325cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100326 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100327 require_name_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100328
329 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100330 gitflow_require_clean_working_tree
Vincent Driessena4dd2232010-02-10 00:34:59 +0100331 gitflow_require_branch_absent "$BRANCH"
332 git fetch -q "$ORIGIN"
333 gitflow_require_branch "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100334
335 # create tracking branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100336 git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100337
338 echo
339 echo "Summary of actions:"
340 echo "- A new remote tracking branch '$BRANCH' was created"
341 echo "- You are now on branch '$BRANCH'"
342 echo
343}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100344
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100345cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100346 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100347
Vincent Driessen5474e462010-02-04 15:50:06 +0100348 if [ "$NAME" != "" ]; then
Vincent Driessend0991262010-02-06 21:19:07 +0100349 expand_nameprefix_arg
Vincent Driessena4dd2232010-02-10 00:34:59 +0100350 BASE=$(git merge-base "$DEVELOP_BRANCH" "$BRANCH")
351 git diff "$BASE..$BRANCH"
Vincent Driessen5474e462010-02-04 15:50:06 +0100352 else
Vincent Driessend0991262010-02-06 21:19:07 +0100353 if ! gitflow_current_branch | grep -q "^$PREFIX"; then
Vincent Driessen5474e462010-02-04 15:50:06 +0100354 die "Not on a feature branch. Name one explicitly."
355 fi
356
Vincent Driessena4dd2232010-02-10 00:34:59 +0100357 BASE=$(git merge-base "$DEVELOP_BRANCH" HEAD)
358 git diff "$BASE"
Vincent Driessen5474e462010-02-04 15:50:06 +0100359 fi
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100360}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100361
362cmd_rebase() {
Vincent Driessen44174922010-02-02 23:53:21 +0100363 DEFINE_boolean interactive false 'do an interactive rebase' i
Vincent Driessenc62633f2010-02-02 10:48:50 +0100364 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100365 expand_nameprefix_arg_or_current
Vincent Driessenc62633f2010-02-02 10:48:50 +0100366 warn "Will try to rebase '$NAME'..."
367 gitflow_require_clean_working_tree
368 gitflow_require_branch "$BRANCH"
369
370 git checkout -q "$BRANCH"
Vincent Driessen27592dd2010-02-06 14:45:39 +0100371 typeset OPTS=
Vincent Driessen44174922010-02-02 23:53:21 +0100372 if flag interactive; then
Vincent Driessenc62633f2010-02-02 10:48:50 +0100373 OPTS="$OPTS -i"
374 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100375 git rebase $OPTS "$DEVELOP_BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100376}