blob: 2c1c4f33b188f256ee2fb5a057b91a535f6a5aa7 [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 Driessenb866b012010-01-28 01:01:53 +0100151}
152
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100153cmd_start() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100154 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100155 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100156 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100157
158 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100159 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100160 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100161
162 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100163 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100164 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100165 fi
166
Vincent Driessene034e4a2010-01-29 12:10:44 +0100167 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
168
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100169 # create branch
170 git checkout -b $BRANCH $BASE
171
172 echo
173 echo "Summary of actions:"
174 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
175 echo "- You are now on branch '$BRANCH'"
176 echo ""
177 echo "Now, start committing on your feature. When done, use:"
178 echo ""
179 echo " git flow finish feature $NAME"
180 echo
181}
182
183cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100184 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
185 DEFINE_boolean rebase 0 'rebase instead of merge' r
186 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
187 parse_args "$@"
188 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100189
190 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100191 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100192
Vincent Driessen49c7d022010-01-28 12:40:33 +0100193 # detect if we're restoring from a merge conflict
194 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
195 #
196 # TODO: detect that we're working on the correct branch here!
197 # The user need not necessarily have given the same $NAME twice here
198 # (although he/she should).
199 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100200
201 # TODO: gitflow_test_clean_working_tree() should provide an alternative
202 # exit code for "unmerged changes in working tree", which we should
203 # actually be testing for here
204 if gitflow_test_clean_working_tree; then
205 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
206
207 # Since the working tree is now clean, either the user did a
208 # succesfull merge manually, or the merge was cancelled.
209 # We detect this using gitflow_is_branch_merged_into()
210 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
211 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
212 helper_finish_cleanup
213 exit 0
214 else
215 # If the user cancelled the merge and decided to wait until later,
216 # that's fine. But we have to acknowledge this by removing the
217 # MERGE_BASE file and continuing normal execution of the finish
218 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
219 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100220 else
221 echo
222 echo "Merge conflicts not resolved yet, use:"
223 echo " git mergetool"
224 echo " git commit"
225 echo
226 echo "You can then complete the finish by running it again:"
227 echo " git flow feature finish $NAME"
228 echo
229 exit 1
230 fi
231 fi
232
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100233 # sanity checks
234 gitflow_require_clean_working_tree
235
Vincent Driessene034e4a2010-01-29 12:10:44 +0100236 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100237 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100238 git fetch -q $ORIGIN $BRANCH
239 fi
240
Benedikt Böhm350e7152010-01-26 13:05:05 +0100241 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
242 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100243 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100244 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100245 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100246 fi
247
248 # merge into BASE
249 git checkout $BASE
250 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
251 git merge --ff $BRANCH
252 else
253 git merge --no-ff $BRANCH
254 fi
255
Vincent Driessen49c7d022010-01-28 12:40:33 +0100256 if [ $? -ne 0 ]; then
257 # oops.. we have a merge conflict!
258 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100259 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100260 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
261 echo
262 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
263 echo " git mergetool"
264 echo " git commit"
265 echo
266 echo "You can then complete the finish by running it again:"
267 echo " git flow feature finish $NAME"
268 echo
269 exit 1
270 fi
271
272 # when no merge conflict is detected, just clean up the feature branch
273 helper_finish_cleanup
274}
275
276helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100277 # sanity checks
278 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100279 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100280
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100281 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100282 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100283 git branch -d $BRANCH
284
285 echo
286 echo "Summary of actions:"
287 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
288 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
289 echo "- Feature branch '$BRANCH' has been removed"
290 echo "- You are now on branch '$BASE'"
291 echo
292}
293
294cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100295 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100296 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100297
298 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100299 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100300 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100301 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100302 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100303
304 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100305 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100306 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100307
308 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100309 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100310 git config branch.$BRANCH.merge refs/heads/$BRANCH
311 git checkout $BRANCH
312
313 echo
314 echo "Summary of actions:"
315 echo "- A new remote branch '$BRANCH' was created"
316 echo "- The local branch '$BRANCH' was configured to track the remote branch"
317 echo "- You are now on branch '$BRANCH'"
318 echo
319}
320
321cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100322 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100323 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100324
325 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100326 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100327 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100328 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100329 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100330
331 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100332 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100333
334 echo
335 echo "Summary of actions:"
336 echo "- A new remote tracking branch '$BRANCH' was created"
337 echo "- You are now on branch '$BRANCH'"
338 echo
339}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100340
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100341cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100342 parse_args "$@"
343 expand_name_arg_prefix
344
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100345 # TODO: if this feature has been based on a non-develop branch, we really
346 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100347 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100348}