blob: 819906de8b4b0b343e666c44f7c44c456743788e [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
55 SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
56 if [ $FLAGS_verbose -eq 0 ]; then
57 echo "$SHORT_NAMES"
58 else
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010059 # determine column width first
60 width=0
61 for branch in $SHORT_NAMES; do
62 len=$(($(echo "$branch" | wc -c) - 1))
63 width=$(max $width $len)
64 done
65 width=$(($width + 3))
66
Vincent Driessenf2536f42010-02-01 15:57:48 +010067 for branch in $SHORT_NAMES; do
68 fullname="$PREFIX$branch"
69 base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
70 develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
71 branch_sha=$(git rev-parse "$fullname")
Vincent Driessen1adbc3e2010-01-30 16:28:20 +010072 printf "%-${width}s" "$branch"
Vincent Driessenf2536f42010-02-01 15:57:48 +010073 if [ "$branch_sha" = "$develop_sha" ]; then
74 printf "(no commits yet)"
75 elif [ "$base" = "$branch_sha" ]; then
76 printf "(is behind develop, may ff)"
77 elif [ "$base" = "$develop_sha" ]; then
78 printf "(based on latest develop)"
79 else
80 printf "(may be rebased)"
81 fi
82 echo
83 done
84 fi
Vincent Driessen186d2b52010-01-27 23:48:39 +010085}
86
Benedikt Böhm00ccea62010-01-26 12:39:36 +010087cmd_help() {
88 usage
89 exit 0
90}
91
Vincent Driessen22ef21a2010-01-29 16:43:37 +010092resolve_name_by_prefix() {
Vincent Driessen2e1856b2010-01-29 15:18:13 +010093 # first, check if there is a perfect match
Vincent Driessen22ef21a2010-01-29 16:43:37 +010094 if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
95 echo "$1"
96 return 0
Vincent Driessen2e1856b2010-01-29 15:18:13 +010097 fi
98
99 MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
100 NUM_MATCHES=$(echo "$MATCHES" | wc -l)
Vincent Driessen1b819232010-02-01 15:51:43 +0100101 if [ -z "$MATCHES" ]; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100102 # no prefix match, so take it literally
103 echo "$1"
104 else
Vincent Driessen1b819232010-02-01 15:51:43 +0100105 if [ $NUM_MATCHES -eq 1 ]; then
106 # sed arg looks a bit weird, but $PREFIX should not contain spaces,
107 # so this one is safe
108 echo "$MATCHES" | sed "s $PREFIX g"
109 else
110 # multiple matches, cannot decide
111 warn "Multiple branches match for prefix '$1':"
112 for match in $MATCHES; do
113 warn "- $match"
114 done
115 die "Aborting. Use an unambiguous prefix."
116 fi
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100117 fi
118}
119
Vincent Driessen1b819232010-02-01 15:51:43 +0100120require_name() {
121 if [ "$NAME" = "" ]; then
122 echo "Missing argument <name>"
123 usage
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100124 exit 1
125 fi
126}
127
Vincent Driessen1b819232010-02-01 15:51:43 +0100128expand_name_arg_prefix() {
129 require_name
130 NAME=$(resolve_name_by_prefix "$NAME")
131 if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
Vincent Driessen2e1856b2010-01-29 15:18:13 +0100132 exit 1
133 fi
134 BRANCH=$PREFIX$NAME
135}
136
Vincent Driessenea608952010-01-29 16:56:29 +0100137parse_args() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100138 # parse options
139 FLAGS "$@" || exit $?
140 eval set -- "${FLAGS_ARGV}"
141
142 # read arguments into global variables
Vincent Driessenb866b012010-01-28 01:01:53 +0100143 NAME="$1"
Vincent Driessen1b819232010-02-01 15:51:43 +0100144 BASE="${2:-$DEVELOP_BRANCH}"
Vincent Driessenb866b012010-01-28 01:01:53 +0100145}
146
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100147cmd_start() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100148 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
Vincent Driessenea608952010-01-29 16:56:29 +0100149 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100150 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100151
152 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100153 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100154 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100155
156 # update the local repo with remote changes, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100157 if [ $FLAGS_fetch -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +0100158 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100159 fi
160
Vincent Driessene034e4a2010-01-29 12:10:44 +0100161 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
162
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100163 # create branch
164 git checkout -b $BRANCH $BASE
165
166 echo
167 echo "Summary of actions:"
168 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
169 echo "- You are now on branch '$BRANCH'"
170 echo ""
171 echo "Now, start committing on your feature. When done, use:"
172 echo ""
173 echo " git flow finish feature $NAME"
174 echo
175}
176
177cmd_finish() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100178 DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
179 DEFINE_boolean rebase 0 'rebase instead of merge' r
180 DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
181 parse_args "$@"
182 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100183
184 # sanity checks
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100185 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100186
Vincent Driessen49c7d022010-01-28 12:40:33 +0100187 # detect if we're restoring from a merge conflict
188 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
189 #
190 # TODO: detect that we're working on the correct branch here!
191 # The user need not necessarily have given the same $NAME twice here
192 # (although he/she should).
193 #
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100194
195 # TODO: gitflow_test_clean_working_tree() should provide an alternative
196 # exit code for "unmerged changes in working tree", which we should
197 # actually be testing for here
198 if gitflow_test_clean_working_tree; then
199 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
200
201 # Since the working tree is now clean, either the user did a
202 # succesfull merge manually, or the merge was cancelled.
203 # We detect this using gitflow_is_branch_merged_into()
204 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
205 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
206 helper_finish_cleanup
207 exit 0
208 else
209 # If the user cancelled the merge and decided to wait until later,
210 # that's fine. But we have to acknowledge this by removing the
211 # MERGE_BASE file and continuing normal execution of the finish
212 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
213 fi
Vincent Driessen49c7d022010-01-28 12:40:33 +0100214 else
215 echo
216 echo "Merge conflicts not resolved yet, use:"
217 echo " git mergetool"
218 echo " git commit"
219 echo
220 echo "You can then complete the finish by running it again:"
221 echo " git flow feature finish $NAME"
222 echo
223 exit 1
224 fi
225 fi
226
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100227 # sanity checks
228 gitflow_require_clean_working_tree
229
Vincent Driessene034e4a2010-01-29 12:10:44 +0100230 # update local repo with remote changes first, if asked
Vincent Driessen1b819232010-02-01 15:51:43 +0100231 if [ $FLAGS_fetch -eq 1 ]; then
Vincent Driessene034e4a2010-01-29 12:10:44 +0100232 git fetch -q $ORIGIN $BRANCH
233 fi
234
Benedikt Böhm350e7152010-01-26 13:05:05 +0100235 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
236 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100237 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100238 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100239 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100240 fi
241
242 # merge into BASE
243 git checkout $BASE
244 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
245 git merge --ff $BRANCH
246 else
247 git merge --no-ff $BRANCH
248 fi
249
Vincent Driessen49c7d022010-01-28 12:40:33 +0100250 if [ $? -ne 0 ]; then
251 # oops.. we have a merge conflict!
252 # write the given $BASE to a temporary file (we need it later)
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100253 mkdir -p "$GIT_DIR/.gitflow"
Vincent Driessen49c7d022010-01-28 12:40:33 +0100254 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
255 echo
256 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
257 echo " git mergetool"
258 echo " git commit"
259 echo
260 echo "You can then complete the finish by running it again:"
261 echo " git flow feature finish $NAME"
262 echo
263 exit 1
264 fi
265
266 # when no merge conflict is detected, just clean up the feature branch
267 helper_finish_cleanup
268}
269
270helper_finish_cleanup() {
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100271 # sanity checks
272 gitflow_require_branch $BRANCH
Vincent Driessen1ee37e72010-01-29 17:36:21 +0100273 gitflow_require_clean_working_tree
Vincent Driessenf6f152f2010-01-29 10:28:08 +0100274
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100275 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100276 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100277 git branch -d $BRANCH
278
279 echo
280 echo "Summary of actions:"
281 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
282 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
283 echo "- Feature branch '$BRANCH' has been removed"
284 echo "- You are now on branch '$BASE'"
285 echo
286}
287
288cmd_publish() {
Vincent Driessenea608952010-01-29 16:56:29 +0100289 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100290 expand_name_arg_prefix
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100291
292 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100293 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100294 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100295 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100296 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100297
298 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100299 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100300 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100301
302 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100303 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100304 git config branch.$BRANCH.merge refs/heads/$BRANCH
305 git checkout $BRANCH
306
307 echo
308 echo "Summary of actions:"
309 echo "- A new remote branch '$BRANCH' was created"
310 echo "- The local branch '$BRANCH' was configured to track the remote branch"
311 echo "- You are now on branch '$BRANCH'"
312 echo
313}
314
315cmd_track() {
Vincent Driessenea608952010-01-29 16:56:29 +0100316 parse_args "$@"
Vincent Driessen1b819232010-02-01 15:51:43 +0100317 require_name
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100318
319 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100320 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100321 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100322 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100323 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100324
325 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100326 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100327
328 echo
329 echo "Summary of actions:"
330 echo "- A new remote tracking branch '$BRANCH' was created"
331 echo "- You are now on branch '$BRANCH'"
332 echo
333}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100334
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100335cmd_diff() {
Vincent Driessen1b819232010-02-01 15:51:43 +0100336 parse_args "$@"
337 expand_name_arg_prefix
338
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100339 # TODO: if this feature has been based on a non-develop branch, we really
340 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100341 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100342}