blob: 890724ff7bbfc44832cc31bc0fdaa12b883ff471 [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 Driessen22ef21a2010-01-29 16:43:37 +010058resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010059 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010060 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
61 echo "$1"
62 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010063 fi
64
65 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
66 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
67 if [ $NUM_MATCHES -eq 1 ]; then
Vincent Driessen22ef21a2010-01-29 16:43:37 +010068 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
69 # so this one is safe
70 echo "$MATCHES" | sed "s $PREFIX g"
Vincent Driessen2e1856b2010-01-29 15:18:13 +010071 elif [ $NUM_MATCHES -eq 0 ]; then
72 # no prefix match, so take it literally
73 echo "$1"
74 else
75 # multiple matches, cannot decide
76 warn "Multiple branches match for prefix '$1':"
77 for match in $MATCHES; do
78 warn "- $match"
79 done
80 die "Aborting. Use an unambiguous prefix."
81 fi
82}
83
Vincent Driessen22ef21a2010-01-29 16:43:37 +010084get_name_by_prefix() {
85 NAME=$(resolve_name_by_prefix "$1")
Vincent Driessen2e1856b2010-01-29 15:18:13 +010086 if [ -z "$NAME" ]; then
87 exit 1
88 fi
89}
90
Vincent Driessen22ef21a2010-01-29 16:43:37 +010091parse_args_common() {
Vincent Driessene034e4a2010-01-29 12:10:44 +010092 # TODO: When we have a nice structured way of parsing flags with getopt,
Vincent Driessen2acfffd2010-01-29 12:37:22 +010093 # implement the following flags:
94 # --fetch, to set FLAG_FETCH=1
95 # --no-fetch, to set FLAG_FETCH=0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010096 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
Vincent Driessen22ef21a2010-01-29 16:43:37 +0100105parse_args() {
106 get_name_by_prefix "$1"
107 parse_args_common
108}
109
Vincent Driessen8392ed32010-01-29 16:54:39 +0100110parse_args_with_name_prefix() {
Vincent Driessenb866b012010-01-28 01:01:53 +0100111 NAME="$1"
Vincent Driessen22ef21a2010-01-29 16:43:37 +0100112 parse_args_common
Vincent Driessenb866b012010-01-28 01:01:53 +0100113}
114
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100115cmd_start() {
Vincent Driessen8392ed32010-01-29 16:54:39 +0100116 parse_args_with_name_prefix "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100117
118 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100119 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100120 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100121
122 # update the local repo with remote changes, if asked
123 if [ $FLAG_FETCH -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100124 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100125 fi
126
Vincent Driessene034e4a2010-01-29 12:10:44 +0100127 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
128
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100129 # create branch
130 git checkout -b $BRANCH $BASE
131
132 echo
133 echo "Summary of actions:"
134 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
135 echo "- You are now on branch '$BRANCH'"
136 echo ""
137 echo "Now, start committing on your feature. When done, use:"
138 echo ""
139 echo " git flow finish feature $NAME"
140 echo
141}
142
143cmd_finish() {
144 parse_args "$@"
145
146 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100147 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100148
Vincent Driessen49c7d022010-01-28 12:40:33 +0100149 # detect if we're restoring from a merge conflict
150 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
151 #
152 # TODO: detect that we're working on the correct branch here!
153 # The user need not necessarily have given the same $NAME twice here
154 # (although he/she should).
155 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100156
157 # TODO: gitflow_test_clean_working_tree() should provide an alternative
158 # exit code for "unmerged changes in working tree", which we should
159 # actually be testing for here
160 if gitflow_test_clean_working_tree; then
161 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
162
163 # Since the working tree is now clean, either the user did a
164 # succesfull merge manually, or the merge was cancelled.
165 # We detect this using gitflow_is_branch_merged_into()
166 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
167 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
168 helper_finish_cleanup
169 exit 0
170 else
171 # If the user cancelled the merge and decided to wait until later,
172 # that's fine. But we have to acknowledge this by removing the
173 # MERGE_BASE file and continuing normal execution of the finish
174 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
175 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100176 else
177 echo
178 echo "Merge conflicts not resolved yet, use:"
179 echo " git mergetool"
180 echo " git commit"
181 echo
182 echo "You can then complete the finish by running it again:"
183 echo " git flow feature finish $NAME"
184 echo
185 exit 1
186 fi
187 fi
188
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100189 # sanity checks
190 gitflow_require_clean_working_tree
191
Vincent Driessene034e4a2010-01-29 12:10:44 +0100192 # update local repo with remote changes first, if asked
193 if [ $FLAG_FETCH -eq 1 ]; then
194 git fetch -q $ORIGIN $BRANCH
195 fi
196
Benedikt Böhm350e7152010-01-26 13:05:05 +0100197 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
198 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100199 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100200 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100201 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100202 fi
203
204 # merge into BASE
205 git checkout $BASE
206 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
207 git merge --ff $BRANCH
208 else
209 git merge --no-ff $BRANCH
210 fi
211
Vincent Driessen49c7d022010-01-28 12:40:33 +0100212 if [ $? -ne 0 ]; then
213 # oops.. we have a merge conflict!
214 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100215 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100216 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
217 echo
218 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
219 echo " git mergetool"
220 echo " git commit"
221 echo
222 echo "You can then complete the finish by running it again:"
223 echo " git flow feature finish $NAME"
224 echo
225 exit 1
226 fi
227
228 # when no merge conflict is detected, just clean up the feature branch
229 helper_finish_cleanup
230}
231
232helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100233 # sanity checks
234 gitflow_require_branch $BRANCH
235 gitflow_check_clean_working_tree
236
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100237 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100238 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100239 git branch -d $BRANCH
240
241 echo
242 echo "Summary of actions:"
243 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
244 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
245 echo "- Feature branch '$BRANCH' has been removed"
246 echo "- You are now on branch '$BASE'"
247 echo
248}
249
250cmd_publish() {
Vincent Driessen8392ed32010-01-29 16:54:39 +0100251 parse_args_with_name_prefix "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100252
253 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100254 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100255 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100256 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100257 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100258
259 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100260 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100261 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100262
263 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100264 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100265 git config branch.$BRANCH.merge refs/heads/$BRANCH
266 git checkout $BRANCH
267
268 echo
269 echo "Summary of actions:"
270 echo "- A new remote branch '$BRANCH' was created"
271 echo "- The local branch '$BRANCH' was configured to track the remote branch"
272 echo "- You are now on branch '$BRANCH'"
273 echo
274}
275
276cmd_track() {
Vincent Driessen8392ed32010-01-29 16:54:39 +0100277 parse_args_with_name_prefix "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100278
279 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100280 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100281 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100282 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100283 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100284
285 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100286 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100287
288 echo
289 echo "Summary of actions:"
290 echo "- A new remote tracking branch '$BRANCH' was created"
291 echo "- You are now on branch '$BRANCH'"
292 echo
293}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100294
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100295cmd_diff() {
296 parse_args "$@"
297 # TODO: if this feature has been based on a non-develop branch, we really
298 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100299 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100300}