blob: efa5002ea63fb1eb3e5f55223a5f86621f257035 [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>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010024}
25
Vincent Driessen186d2b52010-01-27 23:48:39 +010026cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010027 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010028}
29
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010030max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; }
31
Vincent Driessenb866b012010-01-28 01:01:53 +010032cmd_list() {
Vincent Driessen1b819232010-02-01 15:51:43 +010033 DEFINE_boolean verbose 0 'verbose (more) output' v
34 parse_args "$@"
35
Vincent Driessen186d2b52010-01-27 23:48:39 +010036 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
37 if [ -z "$FEATURE_BRANCHES" ]; then
38 warn "No feature branches exist."
39 exit 0
40 fi
Vincent Driessenf2536f42010-02-01 15:57:48 +010041
Vincent Driessenaa6d0162010-02-01 19:43:46 +010042 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessenf2536f42010-02-01 15:57:48 +010043 SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
Vincent Driessenaa6d0162010-02-01 19:43:46 +010044 # determine column width first
45 width=0
46 for branch in $SHORT_NAMES; do
47 len=$(($(echo "$branch" | wc -c) - 1))
48 width=$(max $width $len)
49 done
50 width=$(($width + 3))
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010051
Vincent Driessenaa6d0162010-02-01 19:43:46 +010052 for branch in $SHORT_NAMES; do
53 fullname="$PREFIX$branch"
54 base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
55 develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
56 branch_sha=$(git rev-parse "$fullname")
57 if [ "$fullname" = "$CURRENT_BRANCH" ]; then
58 printf "* "
59 else
60 printf " "
61 fi
62 if [ $FLAGS_verbose -eq 1 ]; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010063 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +010064 if [ "$branch_sha" = "$develop_sha" ]; then
65 printf "(no commits yet)"
66 elif [ "$base" = "$branch_sha" ]; then
67 printf "(is behind develop, may ff)"
68 elif [ "$base" = "$develop_sha" ]; then
69 printf "(based on latest develop)"
70 else
71 printf "(may be rebased)"
72 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +010073 else
74 printf "%s" "$branch"
75 fi
76 echo
77 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010078}
79
Benedikt Böhm00ccea62010-01-26 12:39:36 +010080cmd_help() {
81 usage
82 exit 0
83}
84
Vincent Driessen22ef21a2010-01-29 16:43:37 +010085resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010086 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010087 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
88 echo "$1"
89 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010090 fi
91
92 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
93 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +010094 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +010095 # no prefix match, so take it literally
96 echo "$1"
97 else
Vincent Driessen1b819232010-02-01 15:51:43 +010098 if [ $NUM_MATCHES -eq 1 ]; then
99 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
100 # so this one is safe
101 echo "$MATCHES" | sed "s $PREFIX g"
102 else
103 # multiple matches, cannot decide
104 warn "Multiple branches match for prefix '$1':"
105 for match in $MATCHES; do
106 warn "- $match"
107 done
108 die "Aborting. Use an unambiguous prefix."
109 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100110 fi
111}
112
Vincent Driessen1b819232010-02-01 15:51:43 +0100113require_name() {
114 if [ "$NAME" = "" ]; then
115 echo "Missing argument <name>"
116 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100117 exit 1
118 fi
119}
120
Vincent Driessen1b819232010-02-01 15:51:43 +0100121expand_name_arg_prefix() {
122 require_name
123 NAME=$(resolve_name_by_prefix "$NAME")
124 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100125 exit 1
126 fi
127 BRANCH=$PREFIX$NAME
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 Driessenb866b012010-01-28 01:01:53 +0100136 NAME="$1"
Vincent Driessen1b819232010-02-01 15:51:43 +0100137 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessen11560922010-02-01 21:58:37 +0100138 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100139}
140
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100141cmd_start() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100142 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100143 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100144 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100145
146 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100147 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100148 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100149
150 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100151 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100152 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100153 fi
154
Vincent Driessene034e4a2010-01-29 12:10:44 +0100155 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
156
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100157 # create branch
158 git checkout -b $BRANCH $BASE
159
160 echo
161 echo "Summary of actions:"
162 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
163 echo "- You are now on branch '$BRANCH'"
164 echo ""
165 echo "Now, start committing on your feature. When done, use:"
166 echo ""
167 echo " git flow finish feature $NAME"
168 echo
169}
170
171cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100172 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
173 DEFINE_boolean rebase 0 'rebase instead of merge' r
174 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
175 parse_args "$@"
176 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100177
178 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100179 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100180
Vincent Driessen49c7d022010-01-28 12:40:33 +0100181 # detect if we're restoring from a merge conflict
182 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
183 #
184 # TODO: detect that we're working on the correct branch here!
185 # The user need not necessarily have given the same $NAME twice here
186 # (although he/she should).
187 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100188
189 # TODO: gitflow_test_clean_working_tree() should provide an alternative
190 # exit code for "unmerged changes in working tree", which we should
191 # actually be testing for here
192 if gitflow_test_clean_working_tree; then
193 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
194
195 # Since the working tree is now clean, either the user did a
196 # succesfull merge manually, or the merge was cancelled.
197 # We detect this using gitflow_is_branch_merged_into()
198 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
199 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
200 helper_finish_cleanup
201 exit 0
202 else
203 # If the user cancelled the merge and decided to wait until later,
204 # that's fine. But we have to acknowledge this by removing the
205 # MERGE_BASE file and continuing normal execution of the finish
206 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
207 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100208 else
209 echo
210 echo "Merge conflicts not resolved yet, use:"
211 echo " git mergetool"
212 echo " git commit"
213 echo
214 echo "You can then complete the finish by running it again:"
215 echo " git flow feature finish $NAME"
216 echo
217 exit 1
218 fi
219 fi
220
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100221 # sanity checks
222 gitflow_require_clean_working_tree
223
Vincent Driessene034e4a2010-01-29 12:10:44 +0100224 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100225 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100226 git fetch -q $ORIGIN $BRANCH
227 fi
228
Benedikt Böhm350e7152010-01-26 13:05:05 +0100229 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
230 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100231 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100232 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100233 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100234 fi
235
236 # merge into BASE
237 git checkout $BASE
238 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
239 git merge --ff $BRANCH
240 else
241 git merge --no-ff $BRANCH
242 fi
243
Vincent Driessen49c7d022010-01-28 12:40:33 +0100244 if [ $? -ne 0 ]; then
245 # oops.. we have a merge conflict!
246 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100247 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100248 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
249 echo
250 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
251 echo " git mergetool"
252 echo " git commit"
253 echo
254 echo "You can then complete the finish by running it again:"
255 echo " git flow feature finish $NAME"
256 echo
257 exit 1
258 fi
259
260 # when no merge conflict is detected, just clean up the feature branch
261 helper_finish_cleanup
262}
263
264helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100265 # sanity checks
266 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100267 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100268
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100269 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100270 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100271 git branch -d $BRANCH
272
273 echo
274 echo "Summary of actions:"
275 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
276 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
277 echo "- Feature branch '$BRANCH' has been removed"
278 echo "- You are now on branch '$BASE'"
279 echo
280}
281
282cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100283 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100284 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100285
286 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100287 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100288 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100289 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100290 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100291
292 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100293 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100294 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100295
296 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100297 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100298 git config branch.$BRANCH.merge refs/heads/$BRANCH
299 git checkout $BRANCH
300
301 echo
302 echo "Summary of actions:"
303 echo "- A new remote branch '$BRANCH' was created"
304 echo "- The local branch '$BRANCH' was configured to track the remote branch"
305 echo "- You are now on branch '$BRANCH'"
306 echo
307}
308
309cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100310 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100311 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100312
313 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100314 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100315 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100316 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100317 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100318
319 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100320 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100321
322 echo
323 echo "Summary of actions:"
324 echo "- A new remote tracking branch '$BRANCH' was created"
325 echo "- You are now on branch '$BRANCH'"
326 echo
327}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100328
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100329cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100330 parse_args "$@"
Vincent Driessen11b1f412010-02-02 06:29:51 +0100331 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
332 if [ "$NAME" != "" ]; then
333 expand_name_arg_prefix
334 elif [ "$CURRENT_BRANCH" != "" ]; then
335 BRANCH="$CURRENT_BRANCH"
336 else
337 warn "The current HEAD is no feature branch."
338 warn "To diff a feature, specify a <name> argument."
339 usage
340 exit 1
341 fi
Vincent Driessen1b819232010-02-01 15:51:43 +0100342
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100343 # TODO: if this feature has been based on a non-develop branch, we really
344 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100345 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100346}