blob: 57d58722ac5b9a7dc9ffed0d5de58fbab5bdbc3c [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>]"
20 echo " git flow feature finish [-rsFv] <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>"
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 Driessen1adbc3e2010-01-30 16:28:20 +010031max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; }
32
Vincent Driessenb866b012010-01-28 01:01:53 +010033cmd_list() {
Vincent Driessen44174922010-02-02 23:53:21 +010034 DEFINE_boolean verbose false 'verbose (more) output' v
Vincent Driessen1b819232010-02-01 15:51:43 +010035 parse_args "$@"
36
Vincent Driessen186d2b52010-01-27 23:48:39 +010037 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
38 if [ -z "$FEATURE_BRANCHES" ]; then
39 warn "No feature branches exist."
40 exit 0
41 fi
Vincent Driessenf2536f42010-02-01 15:57:48 +010042
Vincent Driessenaa6d0162010-02-01 19:43:46 +010043 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessenf2536f42010-02-01 15:57:48 +010044 SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
Vincent Driessenaa6d0162010-02-01 19:43:46 +010045 # determine column width first
46 width=0
47 for branch in $SHORT_NAMES; do
48 len=$(($(echo "$branch" | wc -c) - 1))
49 width=$(max $width $len)
50 done
51 width=$(($width + 3))
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010052
Vincent Driessenaa6d0162010-02-01 19:43:46 +010053 for branch in $SHORT_NAMES; do
54 fullname="$PREFIX$branch"
55 base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
56 develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
57 branch_sha=$(git rev-parse "$fullname")
58 if [ "$fullname" = "$CURRENT_BRANCH" ]; then
59 printf "* "
60 else
61 printf " "
62 fi
Vincent Driessen44174922010-02-02 23:53:21 +010063 if flag verbose; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010064 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +010065 if [ "$branch_sha" = "$develop_sha" ]; then
66 printf "(no commits yet)"
67 elif [ "$base" = "$branch_sha" ]; then
68 printf "(is behind develop, may ff)"
69 elif [ "$base" = "$develop_sha" ]; then
70 printf "(based on latest develop)"
71 else
72 printf "(may be rebased)"
73 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +010074 else
75 printf "%s" "$branch"
76 fi
77 echo
78 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010079}
80
Benedikt Böhm00ccea62010-01-26 12:39:36 +010081cmd_help() {
82 usage
83 exit 0
84}
85
Vincent Driessen22ef21a2010-01-29 16:43:37 +010086resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010087 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010088 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
89 echo "$1"
90 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010091 fi
92
93 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
94 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +010095 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +010096 # no prefix match, so take it literally
97 echo "$1"
98 else
Vincent Driessen1b819232010-02-01 15:51:43 +010099 if [ $NUM_MATCHES -eq 1 ]; then
100 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
101 # so this one is safe
102 echo "$MATCHES" | sed "s $PREFIX g"
103 else
104 # multiple matches, cannot decide
105 warn "Multiple branches match for prefix '$1':"
106 for match in $MATCHES; do
107 warn "- $match"
108 done
109 die "Aborting. Use an unambiguous prefix."
110 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100111 fi
112}
113
Vincent Driessen1b819232010-02-01 15:51:43 +0100114require_name() {
115 if [ "$NAME" = "" ]; then
116 echo "Missing argument <name>"
117 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100118 exit 1
119 fi
120}
121
Vincent Driessen1b819232010-02-01 15:51:43 +0100122expand_name_arg_prefix() {
123 require_name
124 NAME=$(resolve_name_by_prefix "$NAME")
125 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100126 exit 1
127 fi
128 BRANCH=$PREFIX$NAME
129}
130
Vincent Driessenc62633f2010-02-02 10:48:50 +0100131expand_name_arg_prefix_or_current() {
132 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
133 if [ "$NAME" != "" ]; then
134 expand_name_arg_prefix
135 elif [ "$CURRENT_BRANCH" != "" ]; then
136 BRANCH="$CURRENT_BRANCH"
137 NAME=$(echo "$BRANCH" | sed "s?$PREFIX??g")
138 else
139 warn "The current HEAD is no feature branch."
140 warn "To diff a feature, specify a <name> argument."
141 usage
142 exit 1
143 fi
144}
145
Vincent Driessenea608952010-01-29 16:56:29 +0100146parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100147 # parse options
148 FLAGS "$@" || exit $?
149 eval set -- "${FLAGS_ARGV}"
150
151 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100152 NAME="$1"
Vincent Driessen1b819232010-02-01 15:51:43 +0100153 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessen11560922010-02-01 21:58:37 +0100154 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100155}
156
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100157cmd_start() {
Vincent Driessen44174922010-02-02 23:53:21 +0100158 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100159 DEFINE_boolean force false 'force creation of feature branch (ignores dirty working tree)' f
Vincent Driessenea608952010-01-29 16:56:29 +0100160 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100161 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100162
163 # sanity checks
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100164 if noflag force; then
165 gitflow_require_clean_working_tree
166 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100167 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100168
169 # update the local repo with remote changes, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100170 if flag fetch; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100171 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100172 fi
173
Vincent Driessene034e4a2010-01-29 12:10:44 +0100174 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
175
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100176 # create branch
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100177 if ! git checkout -b $BRANCH $BASE; then
178 die "Could not create feature branch '$BRANCH'"
179 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100180
181 echo
182 echo "Summary of actions:"
183 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
184 echo "- You are now on branch '$BRANCH'"
185 echo ""
186 echo "Now, start committing on your feature. When done, use:"
187 echo ""
188 echo " git flow finish feature $NAME"
189 echo
190}
191
192cmd_finish() {
Vincent Driessen44174922010-02-02 23:53:21 +0100193 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
194 DEFINE_boolean rebase false 'rebase instead of merge' r
195 DEFINE_boolean squash false 'squash all commits when rebasing (implies --rebase)' s
Vincent Driessen1b819232010-02-01 15:51:43 +0100196 parse_args "$@"
197 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100198
199 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100200 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100201
Vincent Driessen49c7d022010-01-28 12:40:33 +0100202 # detect if we're restoring from a merge conflict
203 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
204 #
205 # TODO: detect that we're working on the correct branch here!
206 # The user need not necessarily have given the same $NAME twice here
207 # (although he/she should).
208 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100209
210 # TODO: gitflow_test_clean_working_tree() should provide an alternative
211 # exit code for "unmerged changes in working tree", which we should
212 # actually be testing for here
213 if gitflow_test_clean_working_tree; then
214 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
215
216 # Since the working tree is now clean, either the user did a
217 # succesfull merge manually, or the merge was cancelled.
218 # We detect this using gitflow_is_branch_merged_into()
219 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
220 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
221 helper_finish_cleanup
222 exit 0
223 else
224 # If the user cancelled the merge and decided to wait until later,
225 # that's fine. But we have to acknowledge this by removing the
226 # MERGE_BASE file and continuing normal execution of the finish
227 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
228 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100229 else
230 echo
231 echo "Merge conflicts not resolved yet, use:"
232 echo " git mergetool"
233 echo " git commit"
234 echo
235 echo "You can then complete the finish by running it again:"
236 echo " git flow feature finish $NAME"
237 echo
238 exit 1
239 fi
240 fi
241
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100242 # sanity checks
243 gitflow_require_clean_working_tree
244
Vincent Driessene034e4a2010-01-29 12:10:44 +0100245 # update local repo with remote changes first, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100246 if flag fetch; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100247 git fetch -q $ORIGIN $BRANCH
248 fi
249
Benedikt Böhm350e7152010-01-26 13:05:05 +0100250 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
251 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100252 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100253 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100254 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100255 fi
256
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100257 # if the user wants to rebase, do that first
Vincent Driessen44174922010-02-02 23:53:21 +0100258 if flag rebase; then
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100259 if ! git flow feature rebase "$NAME" "$BASE"; then
260 warn "Finish was aborted due to conflicts during rebase."
261 warn "Please finish the rebase manually now."
262 warn "When finished, re-run:"
263 warn " git flow feature finish '$NAME' '$BASE'"
264 exit 1
265 fi
266 fi
267
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100268 # merge into BASE
269 git checkout $BASE
Vincent Driessen9cf56732010-02-02 11:57:51 +0100270 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" -eq 1 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100271 git merge --ff $BRANCH
272 else
273 git merge --no-ff $BRANCH
274 fi
275
Vincent Driessen49c7d022010-01-28 12:40:33 +0100276 if [ $? -ne 0 ]; then
277 # oops.. we have a merge conflict!
278 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100279 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100280 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
281 echo
282 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
283 echo " git mergetool"
284 echo " git commit"
285 echo
286 echo "You can then complete the finish by running it again:"
287 echo " git flow feature finish $NAME"
288 echo
289 exit 1
290 fi
291
292 # when no merge conflict is detected, just clean up the feature branch
293 helper_finish_cleanup
294}
295
296helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100297 # sanity checks
298 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100299 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100300
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100301 # delete branch
Vincent Driessen44174922010-02-02 23:53:21 +0100302 if flag fetch; then
Vincent Driesseneec73c62010-02-02 16:14:16 +0100303 git push $ORIGIN :refs/heads/$BRANCH
304 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100305 git branch -d $BRANCH
306
307 echo
308 echo "Summary of actions:"
309 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
310 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
311 echo "- Feature branch '$BRANCH' has been removed"
312 echo "- You are now on branch '$BASE'"
313 echo
314}
315
316cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100317 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100318 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100319
320 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100321 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100322 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100323 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100324 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100325
326 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100327 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100328 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100329
330 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100331 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100332 git config branch.$BRANCH.merge refs/heads/$BRANCH
333 git checkout $BRANCH
334
335 echo
336 echo "Summary of actions:"
337 echo "- A new remote branch '$BRANCH' was created"
338 echo "- The local branch '$BRANCH' was configured to track the remote branch"
339 echo "- You are now on branch '$BRANCH'"
340 echo
341}
342
343cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100344 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100345 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100346
347 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100348 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100349 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100350 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100351 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100352
353 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100354 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100355
356 echo
357 echo "Summary of actions:"
358 echo "- A new remote tracking branch '$BRANCH' was created"
359 echo "- You are now on branch '$BRANCH'"
360 echo
361}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100362
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100363cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100364 parse_args "$@"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100365 expand_name_arg_prefix_or_current
Vincent Driessen1b819232010-02-01 15:51:43 +0100366
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100367 # TODO: if this feature has been based on a non-develop branch, we really
368 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100369 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100370}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100371
372cmd_rebase() {
Vincent Driessen44174922010-02-02 23:53:21 +0100373 DEFINE_boolean interactive false 'do an interactive rebase' i
Vincent Driessenc62633f2010-02-02 10:48:50 +0100374 parse_args "$@"
375 expand_name_arg_prefix_or_current
376 warn "Will try to rebase '$NAME'..."
377 gitflow_require_clean_working_tree
378 gitflow_require_branch "$BRANCH"
379
380 git checkout -q "$BRANCH"
381 OPTS=
Vincent Driessen44174922010-02-02 23:53:21 +0100382 if flag interactive; then
Vincent Driessenc62633f2010-02-02 10:48:50 +0100383 OPTS="$OPTS -i"
384 fi
385 git rebase $OPTS "$BASE"
386}