blob: 3994729440d9da2dc3b2880457b98a872d2bd9cb [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.
Vincent Driessen31e637d2012-01-02 08:26:46 +010012#
Vincent Driessend72acba2010-04-04 16:10:17 +020013# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are met:
Vincent Driessen31e637d2012-01-02 08:26:46 +010015#
Vincent Driessend72acba2010-04-04 16:10:17 +020016# 1. Redistributions of source code must retain the above copyright notice,
17# this list of conditions and the following disclaimer.
Vincent Driessen31e637d2012-01-02 08:26:46 +010018#
Vincent Driessend72acba2010-04-04 16:10:17 +020019# 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.
Vincent Driessen31e637d2012-01-02 08:26:46 +010022#
Vincent Driessend72acba2010-04-04 16:10:17 +020023# 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.
Vincent Driessen31e637d2012-01-02 08:26:46 +010033#
Vincent Driessend72acba2010-04-04 16:10:17 +020034# 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
Vincent Driessen7832d6e2010-02-21 21:31:03 +010039require_git_repo
40require_gitflow_initialized
Vincent Driessend72e4ac2010-02-16 21:33:51 +010041gitflow_load_settings
Vincent Driessenc1598bf2010-02-20 16:52:48 +010042PREFIX=$(git config --get gitflow.prefix.feature)
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010043
Benedikt Böhm00ccea62010-01-26 12:39:36 +010044usage() {
Vincent Driessenb03cf962010-02-01 22:00:39 +010045 echo "usage: git flow feature [list] [-v]"
Vincent Driessen2b829ce2010-07-09 09:36:20 +020046 echo " git flow feature start [-F] <name> [<base>]"
Chad Walker9c5c8062011-10-25 15:42:32 -070047 echo " git flow feature finish [-rFkD] [<name|nameprefix>]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010048 echo " git flow feature publish <name>"
49 echo " git flow feature track <name>"
Vincent Driessena2e41162010-02-24 01:37:07 +010050 echo " git flow feature diff [<name|nameprefix>]"
51 echo " git flow feature rebase [-i] [<name|nameprefix>]"
Vincent Driessend43ab842010-05-27 11:26:01 -070052 echo " git flow feature checkout [<name|nameprefix>]"
Vincent Driessenf68d4052010-07-09 12:34:18 +020053 echo " git flow feature pull <remote> [<name>]"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010054}
55
Vincent Driessen186d2b52010-01-27 23:48:39 +010056cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010057 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010058}
59
Vincent Driessenb866b012010-01-28 01:01:53 +010060cmd_list() {
Vincent Driessen44174922010-02-02 23:53:21 +010061 DEFINE_boolean verbose false 'verbose (more) output' v
Vincent Driessen1b819232010-02-01 15:51:43 +010062 parse_args "$@"
63
Vincent Driessenf46e2902010-02-15 23:01:52 +010064 local feature_branches
65 local current_branch
66 local short_names
Vincent Driessen7832d6e2010-02-21 21:31:03 +010067 feature_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
Vincent Driessen27592dd2010-02-06 14:45:39 +010068 if [ -z "$feature_branches" ]; then
Vincent Driessen186d2b52010-01-27 23:48:39 +010069 warn "No feature branches exist."
Randy Merrillb681b452010-06-26 13:14:15 +080070 warn ""
71 warn "You can start a new feature branch:"
72 warn ""
73 warn " git flow feature start <name> [<base>]"
Randy Merrill4de01f22010-06-26 13:19:06 +080074 warn ""
Vincent Driessen186d2b52010-01-27 23:48:39 +010075 exit 0
76 fi
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010077 current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
Vincent Driessen27592dd2010-02-06 14:45:39 +010078 short_names=$(echo "$feature_branches" | sed "s ^$PREFIX g")
Vincent Driessenf2536f42010-02-01 15:57:48 +010079
Vincent Driessenaa6d0162010-02-01 19:43:46 +010080 # determine column width first
Vincent Driessenf46e2902010-02-15 23:01:52 +010081 local width=0
82 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010083 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010084 local len=${#branch}
Vincent Driessenaa6d0162010-02-01 19:43:46 +010085 width=$(max $width $len)
86 done
Vincent Driessenf46e2902010-02-15 23:01:52 +010087 width=$(($width+3))
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010088
Vincent Driessenf46e2902010-02-15 23:01:52 +010089 local branch
Vincent Driessen27592dd2010-02-06 14:45:39 +010090 for branch in $short_names; do
Vincent Driessenf46e2902010-02-15 23:01:52 +010091 local fullname=$PREFIX$branch
92 local base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
93 local develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
94 local branch_sha=$(git rev-parse "$fullname")
Vincent Driessen27592dd2010-02-06 14:45:39 +010095 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessenaa6d0162010-02-01 19:43:46 +010096 printf "* "
97 else
98 printf " "
99 fi
Vincent Driessen44174922010-02-02 23:53:21 +0100100 if flag verbose; then
Vincent Driessen1adbc3e2010-01-30 16:28:20 +0100101 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +0100102 if [ "$branch_sha" = "$develop_sha" ]; then
103 printf "(no commits yet)"
104 elif [ "$base" = "$branch_sha" ]; then
105 printf "(is behind develop, may ff)"
106 elif [ "$base" = "$develop_sha" ]; then
107 printf "(based on latest develop)"
108 else
109 printf "(may be rebased)"
110 fi
Vincent Driessenaa6d0162010-02-01 19:43:46 +0100111 else
112 printf "%s" "$branch"
113 fi
114 echo
115 done
Vincent Driessen186d2b52010-01-27 23:48:39 +0100116}
117
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100118cmd_help() {
119 usage
120 exit 0
121}
122
Vincent Driessend0991262010-02-06 21:19:07 +0100123require_name_arg() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100124 if [ "$NAME" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100125 warn "Missing argument <name>"
Vincent Driessen1b819232010-02-01 15:51:43 +0100126 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100127 exit 1
128 fi
129}
130
Vincent Driessend0991262010-02-06 21:19:07 +0100131expand_nameprefix_arg() {
132 require_name_arg
133
Vincent Driessenf46e2902010-02-15 23:01:52 +0100134 local expanded_name
135 local exitcode
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100136 expanded_name=$(gitflow_resolve_nameprefix "$NAME" "$PREFIX")
Vincent Driessend0991262010-02-06 21:19:07 +0100137 exitcode=$?
138 case $exitcode in
139 0) NAME=$expanded_name
140 BRANCH=$PREFIX$NAME
141 ;;
142 *) exit 1 ;;
143 esac
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100144}
145
Vincent Driessenf68d4052010-07-09 12:34:18 +0200146use_current_feature_branch_name() {
147 local current_branch=$(git_current_branch)
148 if startswith "$current_branch" "$PREFIX"; then
149 BRANCH=$current_branch
150 NAME=${BRANCH#$PREFIX}
151 else
152 warn "The current HEAD is no feature branch."
153 warn "Please specify a <name> argument."
154 exit 1
155 fi
156}
157
Vincent Driessend0991262010-02-06 21:19:07 +0100158expand_nameprefix_arg_or_current() {
Vincent Driessenc62633f2010-02-02 10:48:50 +0100159 if [ "$NAME" != "" ]; then
Vincent Driessend0991262010-02-06 21:19:07 +0100160 expand_nameprefix_arg
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100161 require_branch "$PREFIX$NAME"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100162 else
Vincent Driessenf68d4052010-07-09 12:34:18 +0200163 use_current_feature_branch_name
Vincent Driessenc62633f2010-02-02 10:48:50 +0100164 fi
165}
166
Vincent Driessenf68d4052010-07-09 12:34:18 +0200167name_or_current() {
168 if [ -z "$NAME" ]; then
169 use_current_feature_branch_name
170 fi
171}
172
Vincent Driessen68e53aa2011-02-03 16:29:50 +0100173parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100174 # parse options
175 FLAGS "$@" || exit $?
176 eval set -- "${FLAGS_ARGV}"
177
178 # read arguments into global variables
Vincent Driessenf1eaa4e2011-02-03 16:11:25 +0100179 NAME=$1
Vincent Driessen11560922010-02-01 21:58:37 +0100180 BRANCH=$PREFIX$NAME
Vincent Driessenb866b012010-01-28 01:01:53 +0100181}
182
Vincent Driessenf68d4052010-07-09 12:34:18 +0200183parse_remote_name() {
Vincent Driessen68e53aa2011-02-03 16:29:50 +0100184 # parse options
185 FLAGS "$@" || exit $?
186 eval set -- "${FLAGS_ARGV}"
Vincent Driessenf68d4052010-07-09 12:34:18 +0200187
188 # read arguments into global variables
189 REMOTE=$1
190 NAME=$2
191 BRANCH=$PREFIX$NAME
192}
193
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100194cmd_start() {
Vincent Driessen44174922010-02-02 23:53:21 +0100195 DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100196 parse_args "$@"
Vincent Driessenc5fcc012010-02-10 00:18:08 +0100197 BASE=${2:-$DEVELOP_BRANCH}
Vincent Driessend0991262010-02-06 21:19:07 +0100198 require_name_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100199
200 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100201 require_branch_absent "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100202
203 # update the local repo with remote changes, if asked
Vincent Driessen44174922010-02-02 23:53:21 +0100204 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100205 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100206 fi
207
Vincent Driessenc118a852010-04-04 16:32:04 +0200208 # if the origin branch counterpart exists, assert that the local branch
209 # isn't behind it (to avoid unnecessary rebasing)
210 if git_branch_exists "$ORIGIN/$DEVELOP_BRANCH"; then
211 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
212 fi
Vincent Driessene034e4a2010-01-29 12:10:44 +0100213
Vincent Driessen9283eae2012-01-02 10:46:33 +0100214 run_pre_hook "$NAME" "$ORIGIN" "$BRANCH" "$BASE"
215
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
Vincent Driessen9283eae2012-01-02 10:46:33 +0100221 run_post_hook "$NAME" "$ORIGIN" "$BRANCH" "$BASE"
222
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100223 echo
224 echo "Summary of actions:"
225 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
226 echo "- You are now on branch '$BRANCH'"
227 echo ""
228 echo "Now, start committing on your feature. When done, use:"
229 echo ""
gmallard192ff522010-04-01 19:31:09 -0400230 echo " git flow feature finish $NAME"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100231 echo
232}
233
234cmd_finish() {
Vincent Driessenca73caf2010-02-07 19:46:38 +0100235 DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200236 DEFINE_boolean rebase false "rebase instead of merge" r
237 DEFINE_boolean keep false "keep branch after performing finish" k
Vincent Driessen4895c152011-11-23 20:36:16 +0100238 DEFINE_boolean force_delete false "force delete feature branch after finish" D
Vincent Driessen1b819232010-02-01 15:51:43 +0100239 parse_args "$@"
Vincent Driessencf5b2a52011-05-16 10:37:16 +0200240 expand_nameprefix_arg_or_current
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100241
242 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100243 require_branch "$BRANCH"
Vincent Driessene034e4a2010-01-29 12:10:44 +0100244
Vincent Driessen49c7d022010-01-28 12:40:33 +0100245 # detect if we're restoring from a merge conflict
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100246 if [ -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE" ]; then
Vincent Driessen49c7d022010-01-28 12:40:33 +0100247 #
248 # TODO: detect that we're working on the correct branch here!
249 # The user need not necessarily have given the same $NAME twice here
250 # (although he/she should).
Vincent Driessen31e637d2012-01-02 08:26:46 +0100251 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100252
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100253 # TODO: git_is_clean_working_tree() should provide an alternative
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100254 # exit code for "unmerged changes in working tree", which we should
255 # actually be testing for here
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100256 if git_is_clean_working_tree; then
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100257 FINISH_BASE=$(cat "$DOT_GIT_DIR/.gitflow/MERGE_BASE")
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100258
259 # Since the working tree is now clean, either the user did a
260 # succesfull merge manually, or the merge was cancelled.
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100261 # We detect this using git_is_branch_merged_into()
262 if git_is_branch_merged_into "$BRANCH" "$FINISH_BASE"; then
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100263 rm -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100264 helper_finish_cleanup
265 exit 0
266 else
267 # If the user cancelled the merge and decided to wait until later,
268 # that's fine. But we have to acknowledge this by removing the
269 # MERGE_BASE file and continuing normal execution of the finish
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100270 rm -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100271 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100272 else
273 echo
274 echo "Merge conflicts not resolved yet, use:"
275 echo " git mergetool"
276 echo " git commit"
Vincent Driessen31e637d2012-01-02 08:26:46 +0100277 echo
Vincent Driessen49c7d022010-01-28 12:40:33 +0100278 echo "You can then complete the finish by running it again:"
279 echo " git flow feature finish $NAME"
280 echo
281 exit 1
282 fi
283 fi
284
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100285 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100286 require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100287
Vincent Driessene034e4a2010-01-29 12:10:44 +0100288 # update local repo with remote changes first, if asked
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300289 if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
Vincent Driessencb654b12010-07-21 13:28:54 +0200290 if flag fetch; then
291 git fetch -q "$ORIGIN" "$BRANCH"
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300292 git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
Vincent Driessencb654b12010-07-21 13:28:54 +0200293 fi
Vincent Driessene034e4a2010-01-29 12:10:44 +0100294 fi
295
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300296 if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100297 require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100298 fi
Konstantin Tjuterevf6fcc4e2011-04-14 19:20:19 +0300299 if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
Vincent Driessencb654b12010-07-21 13:28:54 +0200300 require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
301 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100302
Vincent Driessen9283eae2012-01-02 10:46:33 +0100303 run_pre_hook "$NAME" "$ORIGIN" "$BRANCH"
304
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100305 # if the user wants to rebase, do that first
Vincent Driessen44174922010-02-02 23:53:21 +0100306 if flag rebase; then
Vincent Driessen010252a2010-02-04 10:31:29 +0100307 if ! git flow feature rebase "$NAME" "$DEVELOP_BRANCH"; then
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100308 warn "Finish was aborted due to conflicts during rebase."
309 warn "Please finish the rebase manually now."
310 warn "When finished, re-run:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100311 warn " git flow feature finish '$NAME' '$DEVELOP_BRANCH'"
Vincent Driessen95bf82c2010-02-02 11:58:37 +0100312 exit 1
313 fi
314 fi
315
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100316 # merge into BASE
Vincent Driessena4dd2232010-02-10 00:34:59 +0100317 git checkout "$DEVELOP_BRANCH"
318 if [ "$(git rev-list -n2 "$DEVELOP_BRANCH..$BRANCH" | wc -l)" -eq 1 ]; then
319 git merge --ff "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100320 else
Vincent Driessena4dd2232010-02-10 00:34:59 +0100321 git merge --no-ff "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100322 fi
323
Vincent Driessen49c7d022010-01-28 12:40:33 +0100324 if [ $? -ne 0 ]; then
325 # oops.. we have a merge conflict!
Vincent Driessen010252a2010-02-04 10:31:29 +0100326 # write the given $DEVELOP_BRANCH to a temporary file (we need it later)
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100327 mkdir -p "$DOT_GIT_DIR/.gitflow"
328 echo "$DEVELOP_BRANCH" > "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100329 echo
330 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
331 echo " git mergetool"
332 echo " git commit"
Vincent Driessen31e637d2012-01-02 08:26:46 +0100333 echo
Vincent Driessen49c7d022010-01-28 12:40:33 +0100334 echo "You can then complete the finish by running it again:"
335 echo " git flow feature finish $NAME"
336 echo
337 exit 1
338 fi
339
Vincent Driessen9283eae2012-01-02 10:46:33 +0100340 run_post_hook "$NAME" "$ORIGIN" "$BRANCH"
341
Vincent Driessen49c7d022010-01-28 12:40:33 +0100342 # when no merge conflict is detected, just clean up the feature branch
343 helper_finish_cleanup
344}
345
346helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100347 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100348 require_branch "$BRANCH"
349 require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100350
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100351 # delete branch
Vincent Driessen44174922010-02-02 23:53:21 +0100352 if flag fetch; then
Vincent Driessena4dd2232010-02-10 00:34:59 +0100353 git push "$ORIGIN" ":refs/heads/$BRANCH"
Vincent Driesseneec73c62010-02-02 16:14:16 +0100354 fi
Vincent Driessen31e637d2012-01-02 08:26:46 +0100355
356
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200357 if noflag keep; then
Vincent Driessen4895c152011-11-23 20:36:16 +0100358 if flag force_delete; then
Chad Walker9c5c8062011-10-25 15:42:32 -0700359 git branch -D "$BRANCH"
360 else
361 git branch -d "$BRANCH"
362 fi
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200363 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100364
365 echo
366 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100367 echo "- The feature branch '$BRANCH' was merged into '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100368 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200369 if flag keep; then
Vincent Driessenddd9dfe2010-10-05 12:34:38 +0200370 echo "- Feature branch '$BRANCH' is still available"
Guillaume-Jean Herbiet8fee0c22010-10-04 11:35:10 +0200371 else
372 echo "- Feature branch '$BRANCH' has been removed"
373 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100374 echo "- You are now on branch '$DEVELOP_BRANCH'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100375 echo
376}
377
378cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100379 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100380 expand_nameprefix_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100381
382 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100383 require_clean_working_tree
384 require_branch "$BRANCH"
Vincent Driessena4dd2232010-02-10 00:34:59 +0100385 git fetch -q "$ORIGIN"
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100386 require_branch_absent "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100387
Vincent Driessen9283eae2012-01-02 10:46:33 +0100388 run_pre_hook "$NAME" "$ORIGIN" "$BRANCH"
389
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100390 # create remote branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100391 git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
392 git fetch -q "$ORIGIN"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100393
394 # configure remote tracking
Vincent Driessena4dd2232010-02-10 00:34:59 +0100395 git config "branch.$BRANCH.remote" "$ORIGIN"
396 git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
397 git checkout "$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100398
Vincent Driessen9283eae2012-01-02 10:46:33 +0100399 run_post_hook "$NAME" "$ORIGIN" "$BRANCH"
400
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100401 echo
402 echo "Summary of actions:"
403 echo "- A new remote branch '$BRANCH' was created"
404 echo "- The local branch '$BRANCH' was configured to track the remote branch"
405 echo "- You are now on branch '$BRANCH'"
406 echo
407}
408
409cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100410 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100411 require_name_arg
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100412
413 # sanity checks
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100414 require_clean_working_tree
415 require_branch_absent "$BRANCH"
Vincent Driessen9283eae2012-01-02 10:46:33 +0100416
417 run_pre_hook "$NAME" "$ORIGIN" "$BRANCH"
418
Vincent Driessena4dd2232010-02-10 00:34:59 +0100419 git fetch -q "$ORIGIN"
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100420 require_branch "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100421
422 # create tracking branch
Vincent Driessena4dd2232010-02-10 00:34:59 +0100423 git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100424
Vincent Driessen9283eae2012-01-02 10:46:33 +0100425 run_post_hook "$NAME" "$ORIGIN" "$BRANCH"
426
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100427 echo
428 echo "Summary of actions:"
429 echo "- A new remote tracking branch '$BRANCH' was created"
430 echo "- You are now on branch '$BRANCH'"
431 echo
432}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100433
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100434cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100435 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100436
Vincent Driessen5474e462010-02-04 15:50:06 +0100437 if [ "$NAME" != "" ]; then
Vincent Driessend0991262010-02-06 21:19:07 +0100438 expand_nameprefix_arg
Vincent Driessena4dd2232010-02-10 00:34:59 +0100439 BASE=$(git merge-base "$DEVELOP_BRANCH" "$BRANCH")
440 git diff "$BASE..$BRANCH"
Vincent Driessen5474e462010-02-04 15:50:06 +0100441 else
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100442 if ! git_current_branch | grep -q "^$PREFIX"; then
Vincent Driessen5474e462010-02-04 15:50:06 +0100443 die "Not on a feature branch. Name one explicitly."
444 fi
445
Vincent Driessena4dd2232010-02-10 00:34:59 +0100446 BASE=$(git merge-base "$DEVELOP_BRANCH" HEAD)
447 git diff "$BASE"
Vincent Driessen5474e462010-02-04 15:50:06 +0100448 fi
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100449}
Vincent Driessenc62633f2010-02-02 10:48:50 +0100450
Vincent Driessend43ab842010-05-27 11:26:01 -0700451cmd_checkout() {
452 parse_args "$@"
453
454 if [ "$NAME" != "" ]; then
455 expand_nameprefix_arg
456 git checkout "$BRANCH"
457 else
458 die "Name a feature branch explicitly."
459 fi
460}
461
Vincent Driessena2baef92010-05-27 11:52:31 -0700462cmd_co() {
463 # Alias for checkout
464 cmd_checkout "$@"
465}
466
Vincent Driessenc62633f2010-02-02 10:48:50 +0100467cmd_rebase() {
Vincent Driessen44174922010-02-02 23:53:21 +0100468 DEFINE_boolean interactive false 'do an interactive rebase' i
Vincent Driessenc62633f2010-02-02 10:48:50 +0100469 parse_args "$@"
Vincent Driessend0991262010-02-06 21:19:07 +0100470 expand_nameprefix_arg_or_current
Vincent Driessenc62633f2010-02-02 10:48:50 +0100471 warn "Will try to rebase '$NAME'..."
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100472 require_clean_working_tree
473 require_branch "$BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100474
475 git checkout -q "$BRANCH"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100476 local OPTS=
Vincent Driessen44174922010-02-02 23:53:21 +0100477 if flag interactive; then
Vincent Driessenc62633f2010-02-02 10:48:50 +0100478 OPTS="$OPTS -i"
479 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100480 git rebase $OPTS "$DEVELOP_BRANCH"
Vincent Driessenc62633f2010-02-02 10:48:50 +0100481}
Vincent Driessenf68d4052010-07-09 12:34:18 +0200482
483avoid_accidental_cross_branch_action() {
484 local current_branch=$(git_current_branch)
485 if [ "$BRANCH" != "$current_branch" ]; then
486 warn "Trying to pull from '$BRANCH' while currently on branch '$current_branch'."
487 warn "To avoid unintended merges, git-flow aborted."
488 return 1
489 fi
490 return 0
491}
492
493cmd_pull() {
494 #DEFINE_string prefix false 'alternative remote feature branch name prefix' p
495 parse_remote_name "$@"
496
497 if [ -z "$REMOTE" ]; then
498 die "Name a remote explicitly."
499 fi
500 name_or_current
501
502 # To avoid accidentally merging different feature branches into each other,
503 # die if the current feature branch differs from the requested $NAME
504 # argument.
505 local current_branch=$(git_current_branch)
506 if startswith "$current_branch" "$PREFIX"; then
507 # we are on a local feature branch already, so $BRANCH must be equal to
508 # the current branch
509 avoid_accidental_cross_branch_action || die
510 fi
511
512 require_clean_working_tree
513
Vincent Driessen359db202012-01-02 09:22:43 +0100514 run_pre_hook "$NAME" "$REMOTE" "$BRANCH"
515
Vincent Driessenf68d4052010-07-09 12:34:18 +0200516 if git_branch_exists "$BRANCH"; then
517 # Again, avoid accidental merges
518 avoid_accidental_cross_branch_action || die
519
520 # we already have a local branch called like this, so simply pull the
521 # remote changes in
522 git pull -q "$REMOTE" "$BRANCH" || die "Failed to pull from remote '$REMOTE'."
523 echo "Pulled $REMOTE's changes into $BRANCH."
524 else
525 # setup the local branch clone for the first time
526 git fetch -q "$REMOTE" "$BRANCH" || die "Fetch failed." # stores in FETCH_HEAD
527 git branch --no-track "$BRANCH" FETCH_HEAD || die "Branch failed."
528 git checkout -q "$BRANCH" || die "Checking out new local branch failed."
529 echo "Created local branch $BRANCH based on $REMOTE's $BRANCH."
530 fi
Vincent Driessen359db202012-01-02 09:22:43 +0100531
532 run_post_hook "$NAME" "$REMOTE" "$BRANCH"
Vincent Driessenf68d4052010-07-09 12:34:18 +0200533}