blob: 29614616cf2235b39e47580b52f78a8e45a61a7f [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 Driessen1b819232010-02-01 15:51:43 +010034 DEFINE_boolean verbose 0 'verbose (more) output' v
35 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
63 if [ $FLAGS_verbose -eq 1 ]; 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 Driessen1b819232010-02-01 15:51:43 +0100158 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100159 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100160 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100161
162 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100163 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100164 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100165
166 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100167 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100168 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100169 fi
170
Vincent Driessene034e4a2010-01-29 12:10:44 +0100171 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
172
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100173 # create branch
174 git checkout -b $BRANCH $BASE
175
176 echo
177 echo "Summary of actions:"
178 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
179 echo "- You are now on branch '$BRANCH'"
180 echo ""
181 echo "Now, start committing on your feature. When done, use:"
182 echo ""
183 echo " git flow finish feature $NAME"
184 echo
185}
186
187cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100188 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
189 DEFINE_boolean rebase 0 'rebase instead of merge' r
190 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
191 parse_args "$@"
192 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100193
194 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100195 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100196
Vincent Driessen49c7d022010-01-28 12:40:33 +0100197 # detect if we're restoring from a merge conflict
198 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
199 #
200 # TODO: detect that we're working on the correct branch here!
201 # The user need not necessarily have given the same $NAME twice here
202 # (although he/she should).
203 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100204
205 # TODO: gitflow_test_clean_working_tree() should provide an alternative
206 # exit code for "unmerged changes in working tree", which we should
207 # actually be testing for here
208 if gitflow_test_clean_working_tree; then
209 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
210
211 # Since the working tree is now clean, either the user did a
212 # succesfull merge manually, or the merge was cancelled.
213 # We detect this using gitflow_is_branch_merged_into()
214 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
215 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
216 helper_finish_cleanup
217 exit 0
218 else
219 # If the user cancelled the merge and decided to wait until later,
220 # that's fine. But we have to acknowledge this by removing the
221 # MERGE_BASE file and continuing normal execution of the finish
222 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
223 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100224 else
225 echo
226 echo "Merge conflicts not resolved yet, use:"
227 echo " git mergetool"
228 echo " git commit"
229 echo
230 echo "You can then complete the finish by running it again:"
231 echo " git flow feature finish $NAME"
232 echo
233 exit 1
234 fi
235 fi
236
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100237 # sanity checks
238 gitflow_require_clean_working_tree
239
Vincent Driessene034e4a2010-01-29 12:10:44 +0100240 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100241 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100242 git fetch -q $ORIGIN $BRANCH
243 fi
244
Benedikt Böhm350e7152010-01-26 13:05:05 +0100245 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
246 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100247 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100248 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100249 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100250 fi
251
252 # merge into BASE
253 git checkout $BASE
254 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
255 git merge --ff $BRANCH
256 else
257 git merge --no-ff $BRANCH
258 fi
259
Vincent Driessen49c7d022010-01-28 12:40:33 +0100260 if [ $? -ne 0 ]; then
261 # oops.. we have a merge conflict!
262 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100263 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100264 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
265 echo
266 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
267 echo " git mergetool"
268 echo " git commit"
269 echo
270 echo "You can then complete the finish by running it again:"
271 echo " git flow feature finish $NAME"
272 echo
273 exit 1
274 fi
275
276 # when no merge conflict is detected, just clean up the feature branch
277 helper_finish_cleanup
278}
279
280helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100281 # sanity checks
282 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100283 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100284
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100285 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100286 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100287 git branch -d $BRANCH
288
289 echo
290 echo "Summary of actions:"
291 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
292 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
293 echo "- Feature branch '$BRANCH' has been removed"
294 echo "- You are now on branch '$BASE'"
295 echo
296}
297
298cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100299 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100300 expand_name_arg_prefix
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
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100304 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100305 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100306 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100307
308 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100309 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100310 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100311
312 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100313 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100314 git config branch.$BRANCH.merge refs/heads/$BRANCH
315 git checkout $BRANCH
316
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 Driessen1b819232010-02-01 15:51:43 +0100327 require_name
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
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100331 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100332 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100333 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100334
335 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +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 Driessenc62633f2010-02-02 10:48:50 +0100347 expand_name_arg_prefix_or_current
Vincent Driessen1b819232010-02-01 15:51:43 +0100348
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100349 # TODO: if this feature has been based on a non-develop branch, we really
350 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100351 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100352}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100353
354cmd_rebase() {
355 DEFINE_boolean interactive 0 'do an interactive rebase' i
356 parse_args "$@"
357 expand_name_arg_prefix_or_current
358 warn "Will try to rebase '$NAME'..."
359 gitflow_require_clean_working_tree
360 gitflow_require_branch "$BRANCH"
361
362 git checkout -q "$BRANCH"
363 OPTS=
364 if [ $FLAGS_interactive -eq 1 ]; then
365 OPTS="$OPTS -i"
366 fi
367 git rebase $OPTS "$BASE"
368}