blob: 3ee35efc2ce172e2dac1e4469f22c995cda62903 [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:
Vincent Driessenddb350b2010-07-09 09:42:09 +02006# http://nvie.com/git-model
Benedikt Böhm00ccea62010-01-26 12:39:36 +01007#
8# Feel free to contribute to this project at:
9# http://github.com/nvie/gitflow
10#
Vincent Driessend72acba2010-04-04 16:10:17 +020011# Copyright 2010 Vincent Driessen. All rights reserved.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are met:
15#
16# 1. Redistributions of source code must retain the above copyright notice,
17# this list of conditions and the following disclaimer.
18#
19# 2. Redistributions in binary form must reproduce the above copyright
20# notice, this list of conditions and the following disclaimer in the
21# documentation and/or other materials provided with the distribution.
22#
23# THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26# EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
30# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34# The views and conclusions contained in the software and documentation are
35# those of the authors and should not be interpreted as representing official
36# policies, either expressed or implied, of Vincent Driessen.
Benedikt Böhm00ccea62010-01-26 12:39:36 +010037#
38
pccrdc902ed2012-04-21 00:23:55 -040039init() {
40 require_git_repo
41 require_gitflow_initialized
42 gitflow_load_settings
43 PREFIX=$(git config --get gitflow.prefix.feature)
44}
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010045
Benedikt Böhm00ccea62010-01-26 12:39:36 +010046usage() {
Vincent Driessenb03cf962010-02-01 22:00:39 +010047 echo "usage: git flow feature [list] [-v]"
Vincent Driessen2b829ce2010-07-09 09:36:20 +020048 echo " git flow feature start [-F] <name> [<base>]"
Myke Hines75fbdd72012-04-19 11:14:22 -070049 echo " git flow feature finish [-rFkDS] [<name|nameprefix>]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010050 echo " git flow feature publish <name>"
51 echo " git flow feature track <name>"
Vincent Driessena2e41162010-02-24 01:37:07 +010052 echo " git flow feature diff [<name|nameprefix>]"
53 echo " git flow feature rebase [-i] [<name|nameprefix>]"
Vincent Driessend43ab842010-05-27 11:26:01 -070054 echo " git flow feature checkout [<name|nameprefix>]"
Vedang Manerikar56bff9a2012-01-30 18:38:46 +053055 echo " git flow feature pull [-r] <remote> [<name>]"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010056}
57
Vincent Driessen186d2b52010-01-27 23:48:39 +010058cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010059 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010060}
61
Vincent Driessenb866b012010-01-28 01:01:53 +010062cmd_list() {
Vincent Driessen44174922010-02-02 23:53:21 +010063 DEFINE_boolean verbose false 'verbose (more) output' v
Vincent Driessen1b819232010-02-01 15:51:43 +010064 parse_args "$@"
65
Vincent Driessenf46e2902010-02-15 23:01:52 +010066 local feature_branches
67 local current_branch
68 local short_names
Vincent Driessen7832d6e2010-02-21 21:31:03 +010069 feature_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010070 if [ -z "$feature_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010071 warn "No feature branches exist."
Randy Merrillb681b452010-06-26 13:14:15 +080072 warn ""
73 warn "You can start a new feature branch:"
74 warn ""
75 warn " git flow feature start <name> [<base>]"
Randy Merrill4de01f22010-06-26 13:19:06 +080076 warn ""
Vincent Driessen186d2b52010-01-27 23:48:39 +010077 exit 0
78 fi
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010079 current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen27592dd2010-02-06 14:45:39 +010080 short_names=$(echo "$feature_branches" | sed "s ^$PREFIX g")
Vincent Driessenf2536f42010-02-01 15:57:48 +010081
Vincent Driessenaa6d0162010-02-01 19:43:46 +010082 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010083 local width=0
84 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010085 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010086 local len=${#branch}
Vincent Driessenaa6d0162010-02-01 19:43:46 +010087 width=$(max $width $len)
88 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010089 width=$(($width+3))
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010090
Vincent Driessenf46e2902010-02-15 23:01:52 +010091 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010092 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010093 local fullname=$PREFIX$branch
94 local base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
95 local develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
96 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010097 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessenaa6d0162010-02-01 19:43:46 +010098 printf "* "
99 else
100 printf " "
101 fi
Vincent Driessen44174922010-02-02 23:53:21 +0100102 if flag verbose; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +0100103 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +0100104 if [ "$branch_sha" = "$develop_sha" ]; then
105 printf "(no commits yet)"
106 elif [ "$base" = "$branch_sha" ]; then
107 printf "(is behind develop, may ff)"
108 elif [ "$base" = "$develop_sha" ]; then
109 printf "(based on latest develop)"
110 else
111 printf "(may be rebased)"
112 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +0100113 else
114 printf "%s" "$branch"
115 fi
116 echo
117 done
Vincent Driessen186d2b52010-01-27 23:48:39 +0100118}
119
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100120cmd_help() {
121 usage
122 exit 0
123}
124
Vincent Driessend0991262010-02-06 21:19:07 +0100125require_name_arg() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100126 if [ "$NAME" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100127 warn "Missing argument <name>"
Vincent Driessen1b819232010-02-01 15:51:43 +0100128 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100129 exit 1
130 fi
131}
132
Vincent Driessend0991262010-02-06 21:19:07 +0100133expand_nameprefix_arg() {
134 require_name_arg
135
Vincent Driessenf46e2902010-02-15 23:01:52 +0100136 local expanded_name
137 local exitcode
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100138 expanded_name=$(gitflow_resolve_nameprefix "$NAME" "$PREFIX")
Vincent Driessend0991262010-02-06 21:19:07 +0100139 exitcode=$?
140 case $exitcode in
141 0) NAME=$expanded_name
142 BRANCH=$PREFIX$NAME
143 ;;
144 *) exit 1 ;;
145 esac
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100146}
147
Vincent Driessenf68d4052010-07-09 12:34:18 +0200148use_current_feature_branch_name() {
149 local current_branch=$(git_current_branch)
150 if startswith "$current_branch" "$PREFIX"; then
151 BRANCH=$current_branch
152 NAME=${BRANCH#$PREFIX}
153 else
154 warn "The current HEAD is no feature branch."
155 warn "Please specify a <name> argument."
156 exit 1
157 fi
158}
159
Vincent Driessend0991262010-02-06 21:19:07 +0100160expand_nameprefix_arg_or_current() {
Vincent Driessenc62633f2010-02-02 10:48:50 +0100161 if [ "$NAME" != "" ]; then
Vincent Driessend0991262010-02-06 21:19:07 +0100162 expand_nameprefix_arg
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100163 require_branch "$PREFIX$NAME"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100164 else
Vincent Driessenf68d4052010-07-09 12:34:18 +0200165 use_current_feature_branch_name
Vincent Driessenc62633f2010-02-02 10:48:50 +0100166 fi
167}
168
Vincent Driessenf68d4052010-07-09 12:34:18 +0200169name_or_current() {
170 if [ -z "$NAME" ]; then
171 use_current_feature_branch_name
172 fi
173}
174
Vincent Driessen68e53aa2011-02-03 16:29:50 +0100175parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100176 # parse options
177 FLAGS "$@" || exit $?
178 eval set -- "${FLAGS_ARGV}"
179
180 # read arguments into global variables
Vincent Driessenf1eaa4e2011-02-03 16:11:25 +0100181 NAME=$1
Vincent Driessen11560922010-02-01 21:58:37 +0100182 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100183}
184
Vincent Driessenf68d4052010-07-09 12:34:18 +0200185parse_remote_name() {
Vincent Driessen68e53aa2011-02-03 16:29:50 +0100186 # parse options
187 FLAGS "$@" || exit $?
188 eval set -- "${FLAGS_ARGV}"
Vincent Driessenf68d4052010-07-09 12:34:18 +0200189
190 # read arguments into global variables
191 REMOTE=$1
192 NAME=$2
193 BRANCH=$PREFIX$NAME
194}
195
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100196cmd_start() {
Vincent Driessen44174922010-02-02 23:53:21 +0100197 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100198 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100199 BASE=${2:-$DEVELOP_BRANCH}
Vincent Driessend0991262010-02-06 21:19:07 +0100200 require_name_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100201
202 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100203 require_branch_absent "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100204
205 # update the local repo with remote changes, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100206 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100207 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100208 fi
209
Vincent Driessenc118a852010-04-04 16:32:04 +0200210 # if the origin branch counterpart exists, assert that the local branch
211 # isn't behind it (to avoid unnecessary rebasing)
212 if git_branch_exists "$ORIGIN/$DEVELOP_BRANCH"; then
213 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
214 fi
Vincent Driessene034e4a2010-01-29 12:10:44 +0100215
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100216 # create branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100217 if ! git checkout -b "$BRANCH" "$BASE"; then
Vincent Driessen5455a6f2010-02-03 00:14:05 +0100218 die "Could not create feature branch '$BRANCH'"
219 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100220
221 echo
222 echo "Summary of actions:"
223 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
224 echo "- You are now on branch '$BRANCH'"
225 echo ""
226 echo "Now, start committing on your feature. When done, use:"
227 echo ""
gmallard192ff522010-04-01 19:31:09 -0400228 echo " git flow feature finish $NAME"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100229 echo
230}
231
232cmd_finish() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100233 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200234 DEFINE_boolean rebase false "rebase instead of merge" r
235 DEFINE_boolean keep false "keep branch after performing finish" k
Vincent Driessen4895c152011-11-23 20:36:16 +0100236 DEFINE_boolean force_delete false "force delete feature branch after finish" D
Vincent Driessen0c4d0bf2012-07-09 14:23:59 +0200237 DEFINE_boolean squash false "squash feature during merge" S
Vincent Driessen1b819232010-02-01 15:51:43 +0100238 parse_args "$@"
Vincent Driessencf5b2a52011-05-16 10:37:16 +0200239 expand_nameprefix_arg_or_current
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100240
241 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100242 require_branch "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100243
Vincent Driessen49c7d022010-01-28 12:40:33 +0100244 # detect if we're restoring from a merge conflict
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100245 if [ -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE" ]; then
Vincent Driessen49c7d022010-01-28 12:40:33 +0100246 #
247 # TODO: detect that we're working on the correct branch here!
248 # The user need not necessarily have given the same $NAME twice here
249 # (although he/she should).
250 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100251
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100252 # TODO: git_is_clean_working_tree() should provide an alternative
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100253 # exit code for "unmerged changes in working tree", which we should
254 # actually be testing for here
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100255 if git_is_clean_working_tree; then
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100256 FINISH_BASE=$(cat "$DOT_GIT_DIR/.gitflow/MERGE_BASE")
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100257
258 # Since the working tree is now clean, either the user did a
259 # succesfull merge manually, or the merge was cancelled.
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100260 # We detect this using git_is_branch_merged_into()
261 if git_is_branch_merged_into "$BRANCH" "$FINISH_BASE"; then
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100262 rm -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100263 helper_finish_cleanup
264 exit 0
265 else
266 # If the user cancelled the merge and decided to wait until later,
267 # that's fine. But we have to acknowledge this by removing the
268 # MERGE_BASE file and continuing normal execution of the finish
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100269 rm -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100270 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100271 else
272 echo
273 echo "Merge conflicts not resolved yet, use:"
274 echo " git mergetool"
275 echo " git commit"
276 echo
277 echo "You can then complete the finish by running it again:"
278 echo " git flow feature finish $NAME"
279 echo
280 exit 1
281 fi
282 fi
283
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100284 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100285 require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100286
Vincent Driessene034e4a2010-01-29 12:10:44 +0100287 # update local repo with remote changes first, if asked
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300288 if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
Vincent Driessencb654b12010-07-21 13:28:54 +0200289 if flag fetch; then
290 git fetch -q "$ORIGIN" "$BRANCH"
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300291 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Vincent Driessencb654b12010-07-21 13:28:54 +0200292 fi
Vincent Driessene034e4a2010-01-29 12:10:44 +0100293 fi
294
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300295 if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100296 require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100297 fi
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300298 if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
Vincent Driessencb654b12010-07-21 13:28:54 +0200299 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
300 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100301
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100302 # if the user wants to rebase, do that first
Vincent Driessen44174922010-02-02 23:53:21 +0100303 if flag rebase; then
Vincent Driessen010252a2010-02-04 10:31:29 +0100304 if ! git flow feature rebase "$NAME" "$DEVELOP_BRANCH"; then
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100305 warn "Finish was aborted due to conflicts during rebase."
306 warn "Please finish the rebase manually now."
307 warn "When finished, re-run:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100308 warn " git flow feature finish '$NAME' '$DEVELOP_BRANCH'"
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100309 exit 1
310 fi
311 fi
312
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100313 # merge into BASE
Vincent Driessena4dd2232010-02-10 00:34:59 +0100314 git checkout "$DEVELOP_BRANCH"
315 if [ "$(git rev-list -n2 "$DEVELOP_BRANCH..$BRANCH" | wc -l)" -eq 1 ]; then
316 git merge --ff "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100317 else
Vincent Driessen0c4d0bf2012-07-09 14:23:59 +0200318 if noflag squash; then
Myke Hines75fbdd72012-04-19 11:14:22 -0700319 git merge --no-ff "$BRANCH"
Vincent Driessen0c4d0bf2012-07-09 14:23:59 +0200320 else
321 git merge --squash "$BRANCH"
322 git commit
323 git merge "$BRANCH"
324 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100325 fi
326
Vincent Driessen49c7d022010-01-28 12:40:33 +0100327 if [ $? -ne 0 ]; then
328 # oops.. we have a merge conflict!
Vincent Driessen010252a2010-02-04 10:31:29 +0100329 # write the given $DEVELOP_BRANCH to a temporary file (we need it later)
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100330 mkdir -p "$DOT_GIT_DIR/.gitflow"
331 echo "$DEVELOP_BRANCH" > "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100332 echo
333 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
334 echo " git mergetool"
335 echo " git commit"
336 echo
337 echo "You can then complete the finish by running it again:"
338 echo " git flow feature finish $NAME"
339 echo
340 exit 1
341 fi
342
343 # when no merge conflict is detected, just clean up the feature branch
344 helper_finish_cleanup
345}
346
347helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100348 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100349 require_branch "$BRANCH"
350 require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100351
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100352 # delete branch
Vincent Driessen44174922010-02-02 23:53:21 +0100353 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100354 git push "$ORIGIN" ":refs/heads/$BRANCH"
Vincent Driesseneec73c62010-02-02 16:14:16 +0100355 fi
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200356
357
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200358 if noflag keep; then
Vincent Driessen4895c152011-11-23 20:36:16 +0100359 if flag force_delete; then
Chad Walker9c5c8062011-10-25 15:42:32 -0700360 git branch -D "$BRANCH"
361 else
362 git branch -d "$BRANCH"
363 fi
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200364 fi
Vincent Driessen0c4d0bf2012-07-09 14:23:59 +0200365
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100366 echo
367 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100368 echo "- The feature branch '$BRANCH' was merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100369 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200370 if flag keep; then
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200371 echo "- Feature branch '$BRANCH' is still available"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200372 else
373 echo "- Feature branch '$BRANCH' has been removed"
374 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100375 echo "- You are now on branch '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100376 echo
377}
378
379cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100380 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100381 expand_nameprefix_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100382
383 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100384 require_clean_working_tree
385 require_branch "$BRANCH"
Vincent Driessena4dd2232010-02-10 00:34:59 +0100386 git fetch -q "$ORIGIN"
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100387 require_branch_absent "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100388
389 # create remote branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100390 git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
391 git fetch -q "$ORIGIN"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100392
393 # configure remote tracking
Vincent Driessena4dd2232010-02-10 00:34:59 +0100394 git config "branch.$BRANCH.remote" "$ORIGIN"
395 git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
396 git checkout "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100397
398 echo
399 echo "Summary of actions:"
400 echo "- A new remote branch '$BRANCH' was created"
401 echo "- The local branch '$BRANCH' was configured to track the remote branch"
402 echo "- You are now on branch '$BRANCH'"
403 echo
404}
405
406cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100407 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100408 require_name_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100409
410 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100411 require_clean_working_tree
412 require_branch_absent "$BRANCH"
Vincent Driessena4dd2232010-02-10 00:34:59 +0100413 git fetch -q "$ORIGIN"
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100414 require_branch "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100415
416 # create tracking branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100417 git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100418
419 echo
420 echo "Summary of actions:"
421 echo "- A new remote tracking branch '$BRANCH' was created"
422 echo "- You are now on branch '$BRANCH'"
423 echo
424}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100425
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100426cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100427 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100428
Vincent Driessen5474e462010-02-04 15:50:06 +0100429 if [ "$NAME" != "" ]; then
Vincent Driessend0991262010-02-06 21:19:07 +0100430 expand_nameprefix_arg
Vincent Driessena4dd2232010-02-10 00:34:59 +0100431 BASE=$(git merge-base "$DEVELOP_BRANCH" "$BRANCH")
432 git diff "$BASE..$BRANCH"
Vincent Driessen5474e462010-02-04 15:50:06 +0100433 else
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100434 if ! git_current_branch | grep -q "^$PREFIX"; then
Vincent Driessen5474e462010-02-04 15:50:06 +0100435 die "Not on a feature branch. Name one explicitly."
436 fi
437
Vincent Driessena4dd2232010-02-10 00:34:59 +0100438 BASE=$(git merge-base "$DEVELOP_BRANCH" HEAD)
439 git diff "$BASE"
Vincent Driessen5474e462010-02-04 15:50:06 +0100440 fi
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100441}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100442
Vincent Driessend43ab842010-05-27 11:26:01 -0700443cmd_checkout() {
444 parse_args "$@"
445
446 if [ "$NAME" != "" ]; then
447 expand_nameprefix_arg
448 git checkout "$BRANCH"
449 else
450 die "Name a feature branch explicitly."
451 fi
452}
453
Vincent Driessena2baef92010-05-27 11:52:31 -0700454cmd_co() {
455 # Alias for checkout
456 cmd_checkout "$@"
457}
458
Vincent Driessenc62633f2010-02-02 10:48:50 +0100459cmd_rebase() {
Vincent Driessen44174922010-02-02 23:53:21 +0100460 DEFINE_boolean interactive false 'do an interactive rebase' i
Vincent Driessenc62633f2010-02-02 10:48:50 +0100461 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100462 expand_nameprefix_arg_or_current
Vincent Driessenc62633f2010-02-02 10:48:50 +0100463 warn "Will try to rebase '$NAME'..."
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100464 require_clean_working_tree
465 require_branch "$BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100466
467 git checkout -q "$BRANCH"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100468 local OPTS=
Vincent Driessen44174922010-02-02 23:53:21 +0100469 if flag interactive; then
Vincent Driessenc62633f2010-02-02 10:48:50 +0100470 OPTS="$OPTS -i"
471 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100472 git rebase $OPTS "$DEVELOP_BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100473}
Vincent Driessenf68d4052010-07-09 12:34:18 +0200474
475avoid_accidental_cross_branch_action() {
476 local current_branch=$(git_current_branch)
477 if [ "$BRANCH" != "$current_branch" ]; then
478 warn "Trying to pull from '$BRANCH' while currently on branch '$current_branch'."
479 warn "To avoid unintended merges, git-flow aborted."
480 return 1
481 fi
482 return 0
483}
484
485cmd_pull() {
486 #DEFINE_string prefix false 'alternative remote feature branch name prefix' p
Vincent Driessen73840522012-02-13 20:43:12 +0100487 DEFINE_boolean rebase false "pull with rebase" r
Vincent Driessenf68d4052010-07-09 12:34:18 +0200488 parse_remote_name "$@"
489
490 if [ -z "$REMOTE" ]; then
491 die "Name a remote explicitly."
492 fi
493 name_or_current
494
495 # To avoid accidentally merging different feature branches into each other,
496 # die if the current feature branch differs from the requested $NAME
497 # argument.
498 local current_branch=$(git_current_branch)
499 if startswith "$current_branch" "$PREFIX"; then
500 # we are on a local feature branch already, so $BRANCH must be equal to
501 # the current branch
502 avoid_accidental_cross_branch_action || die
503 fi
504
505 require_clean_working_tree
506
507 if git_branch_exists "$BRANCH"; then
508 # Again, avoid accidental merges
509 avoid_accidental_cross_branch_action || die
510
511 # we already have a local branch called like this, so simply pull the
512 # remote changes in
Vincent Driessen73840522012-02-13 20:43:12 +0100513 if flag rebase; then
514 if ! git pull --rebase -q "$REMOTE" "$BRANCH"; then
515 warn "Pull was aborted. There might be conflicts during rebase or '$REMOTE' might be inaccessible."
516 exit 1
517 fi
518 else
memleak12c2a9c2012-06-01 13:45:31 +0300519 git pull -q "$REMOTE" "$BRANCH" || die "Failed to pull from remote '$REMOTE'."
Vincent Driessen73840522012-02-13 20:43:12 +0100520 fi
Vedang Manerikar56bff9a2012-01-30 18:38:46 +0530521
Vincent Driessenf68d4052010-07-09 12:34:18 +0200522 echo "Pulled $REMOTE's changes into $BRANCH."
523 else
524 # setup the local branch clone for the first time
525 git fetch -q "$REMOTE" "$BRANCH" || die "Fetch failed." # stores in FETCH_HEAD
526 git branch --no-track "$BRANCH" FETCH_HEAD || die "Branch failed."
527 git checkout -q "$BRANCH" || die "Checking out new local branch failed."
528 echo "Created local branch $BRANCH based on $REMOTE's $BRANCH."
529 fi
530}