blob: 18467a6fec49cd3d43551daeb4e70b4d0dab23fd [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
Vincent Driessen48386442010-01-29 10:30:40 +0100105 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100106 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100107
Vincent Driessen49c7d022010-01-28 12:40:33 +0100108 # detect if we're restoring from a merge conflict
109 if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
110 #
111 # TODO: detect that we're working on the correct branch here!
112 # The user need not necessarily have given the same $NAME twice here
113 # (although he/she should).
114 #
115 FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
116 if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
117 rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
118 helper_finish_cleanup
119 exit 0
120 else
121 echo
122 echo "Merge conflicts not resolved yet, use:"
123 echo " git mergetool"
124 echo " git commit"
125 echo
126 echo "You can then complete the finish by running it again:"
127 echo " git flow feature finish $NAME"
128 echo
129 exit 1
130 fi
131 fi
132
Vincent Driessene034e4a2010-01-29 12:10:44 +0100133 # update local repo with remote changes first, if asked
134 if [ $FLAG_FETCH -eq 1 ]; then
135 git fetch -q $ORIGIN $BRANCH
136 fi
137
Benedikt Böhm350e7152010-01-26 13:05:05 +0100138 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
139 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100140 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100141 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100142 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100143 fi
144
145 # merge into BASE
146 git checkout $BASE
147 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
148 git merge --ff $BRANCH
149 else
150 git merge --no-ff $BRANCH
151 fi
152
Vincent Driessen49c7d022010-01-28 12:40:33 +0100153 if [ $? -ne 0 ]; then
154 # oops.. we have a merge conflict!
155 # write the given $BASE to a temporary file (we need it later)
156 mkdir -p "$GIT_DIR/.gitflow"
157 echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
158 echo
159 echo "There were merge conflicts. To resolve the merge conflict manually, use:"
160 echo " git mergetool"
161 echo " git commit"
162 echo
163 echo "You can then complete the finish by running it again:"
164 echo " git flow feature finish $NAME"
165 echo
166 exit 1
167 fi
168
169 # when no merge conflict is detected, just clean up the feature branch
170 helper_finish_cleanup
171}
172
173helper_finish_cleanup() {
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100174 # delete branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100175 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100176 git branch -d $BRANCH
177
178 echo
179 echo "Summary of actions:"
180 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
181 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
182 echo "- Feature branch '$BRANCH' has been removed"
183 echo "- You are now on branch '$BASE'"
184 echo
185}
186
187cmd_publish() {
188 parse_args "$@"
189
190 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100191 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100192 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100193 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100194 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100195
196 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100197 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100198 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100199
200 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100201 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100202 git config branch.$BRANCH.merge refs/heads/$BRANCH
203 git checkout $BRANCH
204
205 echo
206 echo "Summary of actions:"
207 echo "- A new remote branch '$BRANCH' was created"
208 echo "- The local branch '$BRANCH' was configured to track the remote branch"
209 echo "- You are now on branch '$BRANCH'"
210 echo
211}
212
213cmd_track() {
214 parse_args "$@"
215
216 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100217 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100218 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100219 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100220 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100221
222 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100223 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100224
225 echo
226 echo "Summary of actions:"
227 echo "- A new remote tracking branch '$BRANCH' was created"
228 echo "- You are now on branch '$BRANCH'"
229 echo
230}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100231
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100232cmd_diff() {
233 parse_args "$@"
234 # TODO: if this feature has been based on a non-develop branch, we really
235 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100236 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100237}