blob: 4d5dc482d724466d9404e577999a52b8ffcccb21 [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 Driessenb866b012010-01-28 01:01:53 +010018 echo "usage: git flow feature [list]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010019 echo " git flow feature start <name> [<base>]"
Vincent Driessenea608952010-01-29 16:56:29 +010020 echo " git flow feature finish <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 # TODO
25 #echo ""
26 #echo "options:"
27 #echo "--option Explanation"
28 #echo ""
29 #echo "start-only options:"
30 #echo "--option Explanation"
31 #echo ""
32 #echo "finish-only options:"
33 #echo "--rebase Rebases the feature branch on top of develop, instead of merging"
34 #echo "--squash Squashes all commits of the feature branch into a single commit"
35 #echo " on develop"
36 #echo "--push Push to the origin repo when finished"
37}
38
Vincent Driessen186d2b52010-01-27 23:48:39 +010039cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010040 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010041}
42
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010043max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; }
44
Vincent Driessenb866b012010-01-28 01:01:53 +010045cmd_list() {
Vincent Driessen1b819232010-02-01 15:51:43 +010046 DEFINE_boolean verbose 0 'verbose (more) output' v
47 parse_args "$@"
48
Vincent Driessen186d2b52010-01-27 23:48:39 +010049 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
50 if [ -z "$FEATURE_BRANCHES" ]; then
51 warn "No feature branches exist."
52 exit 0
53 fi
Vincent Driessenf2536f42010-02-01 15:57:48 +010054
Vincent Driessenaa6d0162010-02-01 19:43:46 +010055 CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessenf2536f42010-02-01 15:57:48 +010056 SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
Vincent Driessenaa6d0162010-02-01 19:43:46 +010057 # determine column width first
58 width=0
59 for branch in $SHORT_NAMES; do
60 len=$(($(echo "$branch" | wc -c) - 1))
61 width=$(max $width $len)
62 done
63 width=$(($width + 3))
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010064
Vincent Driessenaa6d0162010-02-01 19:43:46 +010065 for branch in $SHORT_NAMES; do
66 fullname="$PREFIX$branch"
67 base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
68 develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
69 branch_sha=$(git rev-parse "$fullname")
70 if [ "$fullname" = "$CURRENT_BRANCH" ]; then
71 printf "* "
72 else
73 printf " "
74 fi
75 if [ $FLAGS_verbose -eq 1 ]; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010076 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +010077 if [ "$branch_sha" = "$develop_sha" ]; then
78 printf "(no commits yet)"
79 elif [ "$base" = "$branch_sha" ]; then
80 printf "(is behind develop, may ff)"
81 elif [ "$base" = "$develop_sha" ]; then
82 printf "(based on latest develop)"
83 else
84 printf "(may be rebased)"
85 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +010086 else
87 printf "%s" "$branch"
88 fi
89 echo
90 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010091}
92
Benedikt Böhm00ccea62010-01-26 12:39:36 +010093cmd_help() {
94 usage
95 exit 0
96}
97
Vincent Driessen22ef21a2010-01-29 16:43:37 +010098resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010099 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +0100100 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
101 echo "$1"
102 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100103 fi
104
105 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
106 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +0100107 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100108 # no prefix match, so take it literally
109 echo "$1"
110 else
Vincent Driessen1b819232010-02-01 15:51:43 +0100111 if [ $NUM_MATCHES -eq 1 ]; then
112 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
113 # so this one is safe
114 echo "$MATCHES" | sed "s $PREFIX g"
115 else
116 # multiple matches, cannot decide
117 warn "Multiple branches match for prefix '$1':"
118 for match in $MATCHES; do
119 warn "- $match"
120 done
121 die "Aborting. Use an unambiguous prefix."
122 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100123 fi
124}
125
Vincent Driessen1b819232010-02-01 15:51:43 +0100126require_name() {
127 if [ "$NAME" = "" ]; then
128 echo "Missing argument <name>"
129 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100130 exit 1
131 fi
132}
133
Vincent Driessen1b819232010-02-01 15:51:43 +0100134expand_name_arg_prefix() {
135 require_name
136 NAME=$(resolve_name_by_prefix "$NAME")
137 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100138 exit 1
139 fi
140 BRANCH=$PREFIX$NAME
141}
142
Vincent Driessenea608952010-01-29 16:56:29 +0100143parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100144 # parse options
145 FLAGS "$@" || exit $?
146 eval set -- "${FLAGS_ARGV}"
147
148 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100149 NAME="$1"
Vincent Driessen1b819232010-02-01 15:51:43 +0100150 BASE="${2:-$DEVELOP_BRANCH}"
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 Driessen1b819232010-02-01 15:51:43 +0100155 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100156 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100157 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100158
159 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100160 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100161 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100162
163 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100164 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100165 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100166 fi
167
Vincent Driessene034e4a2010-01-29 12:10:44 +0100168 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
169
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100170 # create branch
171 git checkout -b $BRANCH $BASE
172
173 echo
174 echo "Summary of actions:"
175 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
176 echo "- You are now on branch '$BRANCH'"
177 echo ""
178 echo "Now, start committing on your feature. When done, use:"
179 echo ""
180 echo " git flow finish feature $NAME"
181 echo
182}
183
184cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100185 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
186 DEFINE_boolean rebase 0 'rebase instead of merge' r
187 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
188 parse_args "$@"
189 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100190
191 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100192 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100193
Vincent Driessen49c7d022010-01-28 12:40:33 +0100194 # detect if we're restoring from a merge conflict
195 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
196 #
197 # TODO: detect that we're working on the correct branch here!
198 # The user need not necessarily have given the same $NAME twice here
199 # (although he/she should).
200 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100201
202 # TODO: gitflow_test_clean_working_tree() should provide an alternative
203 # exit code for "unmerged changes in working tree", which we should
204 # actually be testing for here
205 if gitflow_test_clean_working_tree; then
206 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
207
208 # Since the working tree is now clean, either the user did a
209 # succesfull merge manually, or the merge was cancelled.
210 # We detect this using gitflow_is_branch_merged_into()
211 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
212 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
213 helper_finish_cleanup
214 exit 0
215 else
216 # If the user cancelled the merge and decided to wait until later,
217 # that's fine. But we have to acknowledge this by removing the
218 # MERGE_BASE file and continuing normal execution of the finish
219 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
220 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100221 else
222 echo
223 echo "Merge conflicts not resolved yet, use:"
224 echo " git mergetool"
225 echo " git commit"
226 echo
227 echo "You can then complete the finish by running it again:"
228 echo " git flow feature finish $NAME"
229 echo
230 exit 1
231 fi
232 fi
233
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100234 # sanity checks
235 gitflow_require_clean_working_tree
236
Vincent Driessene034e4a2010-01-29 12:10:44 +0100237 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100238 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100239 git fetch -q $ORIGIN $BRANCH
240 fi
241
Benedikt Böhm350e7152010-01-26 13:05:05 +0100242 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
243 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100244 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100245 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100246 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100247 fi
248
249 # merge into BASE
250 git checkout $BASE
251 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
252 git merge --ff $BRANCH
253 else
254 git merge --no-ff $BRANCH
255 fi
256
Vincent Driessen49c7d022010-01-28 12:40:33 +0100257 if [ $? -ne 0 ]; then
258 # oops.. we have a merge conflict!
259 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100260 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100261 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
262 echo
263 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
264 echo " git mergetool"
265 echo " git commit"
266 echo
267 echo "You can then complete the finish by running it again:"
268 echo " git flow feature finish $NAME"
269 echo
270 exit 1
271 fi
272
273 # when no merge conflict is detected, just clean up the feature branch
274 helper_finish_cleanup
275}
276
277helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100278 # sanity checks
279 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100280 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100281
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100282 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100283 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100284 git branch -d $BRANCH
285
286 echo
287 echo "Summary of actions:"
288 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
289 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
290 echo "- Feature branch '$BRANCH' has been removed"
291 echo "- You are now on branch '$BASE'"
292 echo
293}
294
295cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100296 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100297 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100298
299 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100300 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100301 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100302 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100303 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100304
305 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100306 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100307 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100308
309 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100310 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100311 git config branch.$BRANCH.merge refs/heads/$BRANCH
312 git checkout $BRANCH
313
314 echo
315 echo "Summary of actions:"
316 echo "- A new remote branch '$BRANCH' was created"
317 echo "- The local branch '$BRANCH' was configured to track the remote branch"
318 echo "- You are now on branch '$BRANCH'"
319 echo
320}
321
322cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100323 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100324 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100325
326 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100327 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100328 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100329 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100330 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100331
332 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100333 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100334
335 echo
336 echo "Summary of actions:"
337 echo "- A new remote tracking branch '$BRANCH' was created"
338 echo "- You are now on branch '$BRANCH'"
339 echo
340}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100341
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100342cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100343 parse_args "$@"
344 expand_name_arg_prefix
345
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100346 # TODO: if this feature has been based on a non-develop branch, we really
347 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100348 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100349}