blob: fc1ecc42c4cf3b57f88ca124408a633e560c1b1a [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 Driessenb866b012010-01-28 01:01:53 +010043cmd_list() {
Vincent Driessen1b819232010-02-01 15:51:43 +010044 DEFINE_boolean verbose 0 'verbose (more) output' v
45 parse_args "$@"
46
Vincent Driessen186d2b52010-01-27 23:48:39 +010047 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
48 if [ -z "$FEATURE_BRANCHES" ]; then
49 warn "No feature branches exist."
50 exit 0
51 fi
Vincent Driessenf2536f42010-02-01 15:57:48 +010052
53 SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
54 if [ $FLAGS_verbose -eq 0 ]; then
55 echo "$SHORT_NAMES"
56 else
57 for branch in $SHORT_NAMES; do
58 fullname="$PREFIX$branch"
59 base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
60 develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
61 branch_sha=$(git rev-parse "$fullname")
62 printf "%-40s" "$branch"
63 if [ "$branch_sha" = "$develop_sha" ]; then
64 printf "(no commits yet)"
65 elif [ "$base" = "$branch_sha" ]; then
66 printf "(is behind develop, may ff)"
67 elif [ "$base" = "$develop_sha" ]; then
68 printf "(based on latest develop)"
69 else
70 printf "(may be rebased)"
71 fi
72 echo
73 done
74 fi
Vincent Driessen186d2b52010-01-27 23:48:39 +010075}
76
Benedikt Böhm00ccea62010-01-26 12:39:36 +010077cmd_help() {
78 usage
79 exit 0
80}
81
Vincent Driessen22ef21a2010-01-29 16:43:37 +010082resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010083 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010084 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
85 echo "$1"
86 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010087 fi
88
89 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
90 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +010091 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +010092 # no prefix match, so take it literally
93 echo "$1"
94 else
Vincent Driessen1b819232010-02-01 15:51:43 +010095 if [ $NUM_MATCHES -eq 1 ]; then
96 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
97 # so this one is safe
98 echo "$MATCHES" | sed "s $PREFIX g"
99 else
100 # multiple matches, cannot decide
101 warn "Multiple branches match for prefix '$1':"
102 for match in $MATCHES; do
103 warn "- $match"
104 done
105 die "Aborting. Use an unambiguous prefix."
106 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100107 fi
108}
109
Vincent Driessen1b819232010-02-01 15:51:43 +0100110require_name() {
111 if [ "$NAME" = "" ]; then
112 echo "Missing argument <name>"
113 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100114 exit 1
115 fi
116}
117
Vincent Driessen1b819232010-02-01 15:51:43 +0100118expand_name_arg_prefix() {
119 require_name
120 NAME=$(resolve_name_by_prefix "$NAME")
121 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100122 exit 1
123 fi
124 BRANCH=$PREFIX$NAME
125}
126
Vincent Driessenea608952010-01-29 16:56:29 +0100127parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100128 # parse options
129 FLAGS "$@" || exit $?
130 eval set -- "${FLAGS_ARGV}"
131
132 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100133 NAME="$1"
Vincent Driessen1b819232010-02-01 15:51:43 +0100134 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessenb866b012010-01-28 01:01:53 +0100135}
136
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100137cmd_start() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100138 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100139 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100140 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100141
142 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100143 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100144 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100145
146 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100147 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100148 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100149 fi
150
Vincent Driessene034e4a2010-01-29 12:10:44 +0100151 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
152
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100153 # create branch
154 git checkout -b $BRANCH $BASE
155
156 echo
157 echo "Summary of actions:"
158 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
159 echo "- You are now on branch '$BRANCH'"
160 echo ""
161 echo "Now, start committing on your feature. When done, use:"
162 echo ""
163 echo " git flow finish feature $NAME"
164 echo
165}
166
167cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100168 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
169 DEFINE_boolean rebase 0 'rebase instead of merge' r
170 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
171 parse_args "$@"
172 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100173
174 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100175 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100176
Vincent Driessen49c7d022010-01-28 12:40:33 +0100177 # detect if we're restoring from a merge conflict
178 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
179 #
180 # TODO: detect that we're working on the correct branch here!
181 # The user need not necessarily have given the same $NAME twice here
182 # (although he/she should).
183 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100184
185 # TODO: gitflow_test_clean_working_tree() should provide an alternative
186 # exit code for "unmerged changes in working tree", which we should
187 # actually be testing for here
188 if gitflow_test_clean_working_tree; then
189 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
190
191 # Since the working tree is now clean, either the user did a
192 # succesfull merge manually, or the merge was cancelled.
193 # We detect this using gitflow_is_branch_merged_into()
194 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
195 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
196 helper_finish_cleanup
197 exit 0
198 else
199 # If the user cancelled the merge and decided to wait until later,
200 # that's fine. But we have to acknowledge this by removing the
201 # MERGE_BASE file and continuing normal execution of the finish
202 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
203 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100204 else
205 echo
206 echo "Merge conflicts not resolved yet, use:"
207 echo " git mergetool"
208 echo " git commit"
209 echo
210 echo "You can then complete the finish by running it again:"
211 echo " git flow feature finish $NAME"
212 echo
213 exit 1
214 fi
215 fi
216
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100217 # sanity checks
218 gitflow_require_clean_working_tree
219
Vincent Driessene034e4a2010-01-29 12:10:44 +0100220 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100221 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100222 git fetch -q $ORIGIN $BRANCH
223 fi
224
Benedikt Böhm350e7152010-01-26 13:05:05 +0100225 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
226 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100227 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100228 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100229 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100230 fi
231
232 # merge into BASE
233 git checkout $BASE
234 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
235 git merge --ff $BRANCH
236 else
237 git merge --no-ff $BRANCH
238 fi
239
Vincent Driessen49c7d022010-01-28 12:40:33 +0100240 if [ $? -ne 0 ]; then
241 # oops.. we have a merge conflict!
242 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100243 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100244 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
245 echo
246 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
247 echo " git mergetool"
248 echo " git commit"
249 echo
250 echo "You can then complete the finish by running it again:"
251 echo " git flow feature finish $NAME"
252 echo
253 exit 1
254 fi
255
256 # when no merge conflict is detected, just clean up the feature branch
257 helper_finish_cleanup
258}
259
260helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100261 # sanity checks
262 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100263 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100264
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100265 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100266 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100267 git branch -d $BRANCH
268
269 echo
270 echo "Summary of actions:"
271 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
272 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
273 echo "- Feature branch '$BRANCH' has been removed"
274 echo "- You are now on branch '$BASE'"
275 echo
276}
277
278cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100279 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100280 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100281
282 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100283 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100284 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100285 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100286 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100287
288 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100289 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100290 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100291
292 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100293 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100294 git config branch.$BRANCH.merge refs/heads/$BRANCH
295 git checkout $BRANCH
296
297 echo
298 echo "Summary of actions:"
299 echo "- A new remote branch '$BRANCH' was created"
300 echo "- The local branch '$BRANCH' was configured to track the remote branch"
301 echo "- You are now on branch '$BRANCH'"
302 echo
303}
304
305cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100306 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100307 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100308
309 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100310 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100311 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100312 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100313 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100314
315 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100316 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100317
318 echo
319 echo "Summary of actions:"
320 echo "- A new remote tracking branch '$BRANCH' was created"
321 echo "- You are now on branch '$BRANCH'"
322 echo
323}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100324
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100325cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100326 parse_args "$@"
327 expand_name_arg_prefix
328
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100329 # TODO: if this feature has been based on a non-develop branch, we really
330 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100331 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100332}