blob: 482b2985786bc87b897d9207165b1369d82c3b89 [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 Driessenb866b012010-01-28 01:01:53 +010058parse_args() {
Vincent Driessene034e4a2010-01-29 12:10:44 +010059 # TODO: When we have a nice structured way of parsing flags with getopt,
Vincent Driessen2acfffd2010-01-29 12:37:22 +010060 # implement the following flags:
61 # --fetch, to set FLAG_FETCH=1
62 # --no-fetch, to set FLAG_FETCH=0
Vincent Driessenb866b012010-01-28 01:01:53 +010063 NAME="$1"
64 BASE="${2:-$DEVELOP_BRANCH}"
65 if [ "$NAME" = "" ]; then
66 echo "Missing argument <name>."
67 usage
68 exit 1
69 fi
Vincent Driessenb866b012010-01-28 01:01:53 +010070 BRANCH=$PREFIX$NAME
71}
72
Benedikt Böhm00ccea62010-01-26 12:39:36 +010073cmd_start() {
74 parse_args "$@"
75
76 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +010077 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +010078 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +010079
80 # update the local repo with remote changes, if asked
81 if [ $FLAG_FETCH -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +010082 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +010083 fi
84
Vincent Driessene034e4a2010-01-29 12:10:44 +010085 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
86
Benedikt Böhm00ccea62010-01-26 12:39:36 +010087 # create branch
88 git checkout -b $BRANCH $BASE
89
90 echo
91 echo "Summary of actions:"
92 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
93 echo "- You are now on branch '$BRANCH'"
94 echo ""
95 echo "Now, start committing on your feature. When done, use:"
96 echo ""
97 echo " git flow finish feature $NAME"
98 echo
99}
100
101cmd_finish() {
102 parse_args "$@"
103
104 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100105 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100106
Vincent Driessen49c7d022010-01-28 12:40:33 +0100107 # detect if we're restoring from a merge conflict
108 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
109 #
110 # TODO: detect that we're working on the correct branch here!
111 # The user need not necessarily have given the same $NAME twice here
112 # (although he/she should).
113 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100114
115 # TODO: gitflow_test_clean_working_tree() should provide an alternative
116 # exit code for "unmerged changes in working tree", which we should
117 # actually be testing for here
118 if gitflow_test_clean_working_tree; then
119 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
120
121 # Since the working tree is now clean, either the user did a
122 # succesfull merge manually, or the merge was cancelled.
123 # We detect this using gitflow_is_branch_merged_into()
124 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
125 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
126 helper_finish_cleanup
127 exit 0
128 else
129 # If the user cancelled the merge and decided to wait until later,
130 # that's fine. But we have to acknowledge this by removing the
131 # MERGE_BASE file and continuing normal execution of the finish
132 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
133 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100134 else
135 echo
136 echo "Merge conflicts not resolved yet, use:"
137 echo " git mergetool"
138 echo " git commit"
139 echo
140 echo "You can then complete the finish by running it again:"
141 echo " git flow feature finish $NAME"
142 echo
143 exit 1
144 fi
145 fi
146
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100147 # sanity checks
148 gitflow_require_clean_working_tree
149
Vincent Driessene034e4a2010-01-29 12:10:44 +0100150 # update local repo with remote changes first, if asked
151 if [ $FLAG_FETCH -eq 1 ]; then
152 git fetch -q $ORIGIN $BRANCH
153 fi
154
Benedikt Böhm350e7152010-01-26 13:05:05 +0100155 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
156 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100157 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100158 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100159 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160 fi
161
162 # merge into BASE
163 git checkout $BASE
164 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
165 git merge --ff $BRANCH
166 else
167 git merge --no-ff $BRANCH
168 fi
169
Vincent Driessen49c7d022010-01-28 12:40:33 +0100170 if [ $? -ne 0 ]; then
171 # oops.. we have a merge conflict!
172 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100173 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100174 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
175 echo
176 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
177 echo " git mergetool"
178 echo " git commit"
179 echo
180 echo "You can then complete the finish by running it again:"
181 echo " git flow feature finish $NAME"
182 echo
183 exit 1
184 fi
185
186 # when no merge conflict is detected, just clean up the feature branch
187 helper_finish_cleanup
188}
189
190helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100191 # sanity checks
192 gitflow_require_branch $BRANCH
193 gitflow_check_clean_working_tree
194
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100195 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100196 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100197 git branch -d $BRANCH
198
199 echo
200 echo "Summary of actions:"
201 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
202 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
203 echo "- Feature branch '$BRANCH' has been removed"
204 echo "- You are now on branch '$BASE'"
205 echo
206}
207
208cmd_publish() {
209 parse_args "$@"
210
211 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100212 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100213 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100214 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100215 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100216
217 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100218 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100219 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100220
221 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100222 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100223 git config branch.$BRANCH.merge refs/heads/$BRANCH
224 git checkout $BRANCH
225
226 echo
227 echo "Summary of actions:"
228 echo "- A new remote branch '$BRANCH' was created"
229 echo "- The local branch '$BRANCH' was configured to track the remote branch"
230 echo "- You are now on branch '$BRANCH'"
231 echo
232}
233
234cmd_track() {
235 parse_args "$@"
236
237 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100238 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100239 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100240 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100241 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100242
243 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100244 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100245
246 echo
247 echo "Summary of actions:"
248 echo "- A new remote tracking branch '$BRANCH' was created"
249 echo "- You are now on branch '$BRANCH'"
250 echo
251}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100252
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100253cmd_diff() {
254 parse_args "$@"
255 # TODO: if this feature has been based on a non-develop branch, we really
256 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100257 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100258}