blob: 7369a8297ffdf58839603b011315893911f33fcc [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/)
Vincent Driessene034e4a2010-01-29 12:10:44 +010016FLAG_FETCH=0
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010017
Benedikt Böhm00ccea62010-01-26 12:39:36 +010018usage() {
Vincent Driessenb866b012010-01-28 01:01:53 +010019 echo "usage: git flow feature [list]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010020 echo " git flow feature start <name> [<base>]"
21 echo " git flow feature finish <name> [<base>]"
22 echo " git flow feature publish <name>"
23 echo " git flow feature track <name>"
24 echo " git flow feature diff <name>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010025 # TODO
26 #echo ""
27 #echo "options:"
28 #echo "--option Explanation"
29 #echo ""
30 #echo "start-only options:"
31 #echo "--option Explanation"
32 #echo ""
33 #echo "finish-only options:"
34 #echo "--rebase Rebases the feature branch on top of develop, instead of merging"
35 #echo "--squash Squashes all commits of the feature branch into a single commit"
36 #echo " on develop"
37 #echo "--push Push to the origin repo when finished"
38}
39
Vincent Driessen186d2b52010-01-27 23:48:39 +010040cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010041 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010042}
43
Vincent Driessenb866b012010-01-28 01:01:53 +010044cmd_list() {
Vincent Driessen186d2b52010-01-27 23:48:39 +010045 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
46 if [ -z "$FEATURE_BRANCHES" ]; then
47 warn "No feature branches exist."
48 exit 0
49 fi
50 echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g"
51}
52
Benedikt Böhm00ccea62010-01-26 12:39:36 +010053cmd_help() {
54 usage
55 exit 0
56}
57
Vincent Driessen2e1856b2010-01-29 15:18:13 +010058parse_feature_rev() {
59 # first, check if there is a perfect match
60 if echo "$LOCAL_BRANCHES" | grep -q "^$PREFIX/$1\$"; then
61 echo "$PREFIX/$1"
62 fi
63
64 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
65 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
66 if [ $NUM_MATCHES -eq 1 ]; then
67 echo "$MATCHES"
68 elif [ $NUM_MATCHES -eq 0 ]; then
69 # no prefix match, so take it literally
70 echo "$1"
71 else
72 # multiple matches, cannot decide
73 warn "Multiple branches match for prefix '$1':"
74 for match in $MATCHES; do
75 warn "- $match"
76 done
77 die "Aborting. Use an unambiguous prefix."
78 fi
79}
80
81get_name_from_arg() {
82 NAME=$(parse_feature_rev "$1")
83 if [ -z "$NAME" ]; then
84 exit 1
85 fi
86}
87
Vincent Driessenb866b012010-01-28 01:01:53 +010088parse_args() {
Vincent Driessene034e4a2010-01-29 12:10:44 +010089 # TODO: When we have a nice structured way of parsing flags with getopt,
Vincent Driessen2acfffd2010-01-29 12:37:22 +010090 # implement the following flags:
91 # --fetch, to set FLAG_FETCH=1
92 # --no-fetch, to set FLAG_FETCH=0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010093 get_name_from_arg "$1"
94 echo $NAME
95 #exit 1 # debug!
96 BASE="${2:-$DEVELOP_BRANCH}"
97 if [ "$NAME" = "" ]; then
98 echo "Missing argument <name>."
99 usage
100 exit 1
101 fi
102 BRANCH=$PREFIX$NAME
103}
104
105parse_start_args() {
106 # TODO: When we have a nice structured way of parsing flags with getopt,
107 # implement the following flags:
108 # --fetch, to set FLAG_FETCH=1
109 # --no-fetch, to set FLAG_FETCH=0
Vincent Driessenb866b012010-01-28 01:01:53 +0100110 NAME="$1"
111 BASE="${2:-$DEVELOP_BRANCH}"
112 if [ "$NAME" = "" ]; then
113 echo "Missing argument <name>."
114 usage
115 exit 1
116 fi
Vincent Driessenb866b012010-01-28 01:01:53 +0100117 BRANCH=$PREFIX$NAME
118}
119
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100120cmd_start() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100121 parse_start_args "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100122
123 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100124 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100125 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100126
127 # update the local repo with remote changes, if asked
128 if [ $FLAG_FETCH -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100129 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100130 fi
131
Vincent Driessene034e4a2010-01-29 12:10:44 +0100132 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
133
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100134 # create branch
135 git checkout -b $BRANCH $BASE
136
137 echo
138 echo "Summary of actions:"
139 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
140 echo "- You are now on branch '$BRANCH'"
141 echo ""
142 echo "Now, start committing on your feature. When done, use:"
143 echo ""
144 echo " git flow finish feature $NAME"
145 echo
146}
147
148cmd_finish() {
149 parse_args "$@"
150
151 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100152 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100153
Vincent Driessen49c7d022010-01-28 12:40:33 +0100154 # detect if we're restoring from a merge conflict
155 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
156 #
157 # TODO: detect that we're working on the correct branch here!
158 # The user need not necessarily have given the same $NAME twice here
159 # (although he/she should).
160 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100161
162 # TODO: gitflow_test_clean_working_tree() should provide an alternative
163 # exit code for "unmerged changes in working tree", which we should
164 # actually be testing for here
165 if gitflow_test_clean_working_tree; then
166 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
167
168 # Since the working tree is now clean, either the user did a
169 # succesfull merge manually, or the merge was cancelled.
170 # We detect this using gitflow_is_branch_merged_into()
171 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
172 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
173 helper_finish_cleanup
174 exit 0
175 else
176 # If the user cancelled the merge and decided to wait until later,
177 # that's fine. But we have to acknowledge this by removing the
178 # MERGE_BASE file and continuing normal execution of the finish
179 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
180 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100181 else
182 echo
183 echo "Merge conflicts not resolved yet, use:"
184 echo " git mergetool"
185 echo " git commit"
186 echo
187 echo "You can then complete the finish by running it again:"
188 echo " git flow feature finish $NAME"
189 echo
190 exit 1
191 fi
192 fi
193
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100194 # sanity checks
195 gitflow_require_clean_working_tree
196
Vincent Driessene034e4a2010-01-29 12:10:44 +0100197 # update local repo with remote changes first, if asked
198 if [ $FLAG_FETCH -eq 1 ]; then
199 git fetch -q $ORIGIN $BRANCH
200 fi
201
Benedikt Böhm350e7152010-01-26 13:05:05 +0100202 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
203 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100204 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100205 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100206 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100207 fi
208
209 # merge into BASE
210 git checkout $BASE
211 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
212 git merge --ff $BRANCH
213 else
214 git merge --no-ff $BRANCH
215 fi
216
Vincent Driessen49c7d022010-01-28 12:40:33 +0100217 if [ $? -ne 0 ]; then
218 # oops.. we have a merge conflict!
219 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100220 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100221 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
222 echo
223 echo "There were merge conflicts. To resolve the merge conflict manually, 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
233 # when no merge conflict is detected, just clean up the feature branch
234 helper_finish_cleanup
235}
236
237helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100238 # sanity checks
239 gitflow_require_branch $BRANCH
240 gitflow_check_clean_working_tree
241
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100242 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100243 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100244 git branch -d $BRANCH
245
246 echo
247 echo "Summary of actions:"
248 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
249 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
250 echo "- Feature branch '$BRANCH' has been removed"
251 echo "- You are now on branch '$BASE'"
252 echo
253}
254
255cmd_publish() {
256 parse_args "$@"
257
258 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100259 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100260 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100261 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100262 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100263
264 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100265 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100266 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100267
268 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100269 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100270 git config branch.$BRANCH.merge refs/heads/$BRANCH
271 git checkout $BRANCH
272
273 echo
274 echo "Summary of actions:"
275 echo "- A new remote branch '$BRANCH' was created"
276 echo "- The local branch '$BRANCH' was configured to track the remote branch"
277 echo "- You are now on branch '$BRANCH'"
278 echo
279}
280
281cmd_track() {
282 parse_args "$@"
283
284 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100285 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100286 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100287 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100288 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100289
290 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100291 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100292
293 echo
294 echo "Summary of actions:"
295 echo "- A new remote tracking branch '$BRANCH' was created"
296 echo "- You are now on branch '$BRANCH'"
297 echo
298}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100299
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100300cmd_diff() {
301 parse_args "$@"
302 # TODO: if this feature has been based on a non-develop branch, we really
303 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100304 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100305}