Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 1 | # |
Vincent Driessen | 6c2d30b | 2010-01-26 22:18:36 +0100 | [diff] [blame] | 2 | # git-flow -- A collection of Git extensions to provide high-level |
| 3 | # repository operations for Vincent Driessen's branching model. |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 4 | # |
| 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öhm | 49dd62b | 2010-01-28 00:51:15 +0100 | [diff] [blame] | 15 | PREFIX=$(git config --get gitflow.prefix.feature || echo feature/) |
Vincent Driessen | e034e4a | 2010-01-29 12:10:44 +0100 | [diff] [blame] | 16 | FLAG_FETCH=0 |
Benedikt Böhm | 49dd62b | 2010-01-28 00:51:15 +0100 | [diff] [blame] | 17 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 18 | usage() { |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 19 | echo "usage: git flow feature [list]" |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 20 | 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öhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 25 | # 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 Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 40 | cmd_default() { |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 41 | cmd_list "$@" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 44 | cmd_list() { |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 45 | 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öhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 53 | cmd_help() { |
| 54 | usage |
| 55 | exit 0 |
| 56 | } |
| 57 | |
Vincent Driessen | 2e1856b | 2010-01-29 15:18:13 +0100 | [diff] [blame^] | 58 | parse_feature_rev() { |
| 59 | # first, check if there is a perfect match |
| 60 | if echo "$LOCAL_BRANCHES" | grep -q "^$PREFIX/$1\$"; then |
| 61 | echo "$PREFIX/$1" |
| 62 | fi |
| 63 | |
| 64 | MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")" |
| 65 | NUM_MATCHES=$(echo "$MATCHES" | wc -l) |
| 66 | if [ $NUM_MATCHES -eq 1 ]; then |
| 67 | echo "$MATCHES" |
| 68 | elif [ $NUM_MATCHES -eq 0 ]; then |
| 69 | # no prefix match, so take it literally |
| 70 | echo "$1" |
| 71 | else |
| 72 | # multiple matches, cannot decide |
| 73 | warn "Multiple branches match for prefix '$1':" |
| 74 | for match in $MATCHES; do |
| 75 | warn "- $match" |
| 76 | done |
| 77 | die "Aborting. Use an unambiguous prefix." |
| 78 | fi |
| 79 | } |
| 80 | |
| 81 | get_name_from_arg() { |
| 82 | NAME=$(parse_feature_rev "$1") |
| 83 | if [ -z "$NAME" ]; then |
| 84 | exit 1 |
| 85 | fi |
| 86 | } |
| 87 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 88 | parse_args() { |
Vincent Driessen | e034e4a | 2010-01-29 12:10:44 +0100 | [diff] [blame] | 89 | # TODO: When we have a nice structured way of parsing flags with getopt, |
Vincent Driessen | 2acfffd | 2010-01-29 12:37:22 +0100 | [diff] [blame] | 90 | # implement the following flags: |
| 91 | # --fetch, to set FLAG_FETCH=1 |
| 92 | # --no-fetch, to set FLAG_FETCH=0 |
Vincent Driessen | 2e1856b | 2010-01-29 15:18:13 +0100 | [diff] [blame^] | 93 | get_name_from_arg "$1" |
| 94 | echo $NAME |
| 95 | #exit 1 # debug! |
| 96 | BASE="${2:-$DEVELOP_BRANCH}" |
| 97 | if [ "$NAME" = "" ]; then |
| 98 | echo "Missing argument <name>." |
| 99 | usage |
| 100 | exit 1 |
| 101 | fi |
| 102 | BRANCH=$PREFIX$NAME |
| 103 | } |
| 104 | |
| 105 | parse_start_args() { |
| 106 | # TODO: When we have a nice structured way of parsing flags with getopt, |
| 107 | # implement the following flags: |
| 108 | # --fetch, to set FLAG_FETCH=1 |
| 109 | # --no-fetch, to set FLAG_FETCH=0 |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 110 | NAME="$1" |
| 111 | BASE="${2:-$DEVELOP_BRANCH}" |
| 112 | if [ "$NAME" = "" ]; then |
| 113 | echo "Missing argument <name>." |
| 114 | usage |
| 115 | exit 1 |
| 116 | fi |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 117 | BRANCH=$PREFIX$NAME |
| 118 | } |
| 119 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 120 | cmd_start() { |
Vincent Driessen | 2e1856b | 2010-01-29 15:18:13 +0100 | [diff] [blame^] | 121 | parse_start_args "$@" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 122 | |
| 123 | # sanity checks |
Vincent Driessen | 4838644 | 2010-01-29 10:30:40 +0100 | [diff] [blame] | 124 | gitflow_require_clean_working_tree |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 125 | gitflow_require_branch_absent $BRANCH |
Vincent Driessen | e034e4a | 2010-01-29 12:10:44 +0100 | [diff] [blame] | 126 | |
| 127 | # update the local repo with remote changes, if asked |
| 128 | if [ $FLAG_FETCH -eq 1 ]; then |
Benedikt Böhm | 4d22227 | 2010-01-26 14:46:56 +0100 | [diff] [blame] | 129 | git fetch -q $ORIGIN $DEVELOP_BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 130 | fi |
| 131 | |
Vincent Driessen | e034e4a | 2010-01-29 12:10:44 +0100 | [diff] [blame] | 132 | gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH |
| 133 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 134 | # create branch |
| 135 | git checkout -b $BRANCH $BASE |
| 136 | |
| 137 | echo |
| 138 | echo "Summary of actions:" |
| 139 | echo "- A new branch '$BRANCH' was created, based on '$BASE'" |
| 140 | echo "- You are now on branch '$BRANCH'" |
| 141 | echo "" |
| 142 | echo "Now, start committing on your feature. When done, use:" |
| 143 | echo "" |
| 144 | echo " git flow finish feature $NAME" |
| 145 | echo |
| 146 | } |
| 147 | |
| 148 | cmd_finish() { |
| 149 | parse_args "$@" |
| 150 | |
| 151 | # sanity checks |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 152 | gitflow_require_branch $BRANCH |
Vincent Driessen | e034e4a | 2010-01-29 12:10:44 +0100 | [diff] [blame] | 153 | |
Vincent Driessen | 49c7d02 | 2010-01-28 12:40:33 +0100 | [diff] [blame] | 154 | # detect if we're restoring from a merge conflict |
| 155 | if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then |
| 156 | # |
| 157 | # TODO: detect that we're working on the correct branch here! |
| 158 | # The user need not necessarily have given the same $NAME twice here |
| 159 | # (although he/she should). |
| 160 | # |
Vincent Driessen | f6f152f | 2010-01-29 10:28:08 +0100 | [diff] [blame] | 161 | |
| 162 | # TODO: gitflow_test_clean_working_tree() should provide an alternative |
| 163 | # exit code for "unmerged changes in working tree", which we should |
| 164 | # actually be testing for here |
| 165 | if gitflow_test_clean_working_tree; then |
| 166 | FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")" |
| 167 | |
| 168 | # Since the working tree is now clean, either the user did a |
| 169 | # succesfull merge manually, or the merge was cancelled. |
| 170 | # We detect this using gitflow_is_branch_merged_into() |
| 171 | if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then |
| 172 | rm -f "$GIT_DIR/.gitflow/MERGE_BASE" |
| 173 | helper_finish_cleanup |
| 174 | exit 0 |
| 175 | else |
| 176 | # If the user cancelled the merge and decided to wait until later, |
| 177 | # that's fine. But we have to acknowledge this by removing the |
| 178 | # MERGE_BASE file and continuing normal execution of the finish |
| 179 | rm -f "$GIT_DIR/.gitflow/MERGE_BASE" |
| 180 | fi |
Vincent Driessen | 49c7d02 | 2010-01-28 12:40:33 +0100 | [diff] [blame] | 181 | else |
| 182 | echo |
| 183 | echo "Merge conflicts not resolved yet, use:" |
| 184 | echo " git mergetool" |
| 185 | echo " git commit" |
| 186 | echo |
| 187 | echo "You can then complete the finish by running it again:" |
| 188 | echo " git flow feature finish $NAME" |
| 189 | echo |
| 190 | exit 1 |
| 191 | fi |
| 192 | fi |
| 193 | |
Vincent Driessen | f6f152f | 2010-01-29 10:28:08 +0100 | [diff] [blame] | 194 | # sanity checks |
| 195 | gitflow_require_clean_working_tree |
| 196 | |
Vincent Driessen | e034e4a | 2010-01-29 12:10:44 +0100 | [diff] [blame] | 197 | # update local repo with remote changes first, if asked |
| 198 | if [ $FLAG_FETCH -eq 1 ]; then |
| 199 | git fetch -q $ORIGIN $BRANCH |
| 200 | fi |
| 201 | |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 202 | if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then |
| 203 | gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 204 | fi |
Benedikt Böhm | 4a864fb | 2010-01-26 12:59:27 +0100 | [diff] [blame] | 205 | if [ "$BASE" = "$DEVELOP_BRANCH" ]; then |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 206 | gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 207 | fi |
| 208 | |
| 209 | # merge into BASE |
| 210 | git checkout $BASE |
| 211 | if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then |
| 212 | git merge --ff $BRANCH |
| 213 | else |
| 214 | git merge --no-ff $BRANCH |
| 215 | fi |
| 216 | |
Vincent Driessen | 49c7d02 | 2010-01-28 12:40:33 +0100 | [diff] [blame] | 217 | if [ $? -ne 0 ]; then |
| 218 | # oops.. we have a merge conflict! |
| 219 | # write the given $BASE to a temporary file (we need it later) |
Vincent Driessen | f6f152f | 2010-01-29 10:28:08 +0100 | [diff] [blame] | 220 | mkdir -p "$GIT_DIR/.gitflow" |
Vincent Driessen | 49c7d02 | 2010-01-28 12:40:33 +0100 | [diff] [blame] | 221 | echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE" |
| 222 | echo |
| 223 | echo "There were merge conflicts. To resolve the merge conflict manually, use:" |
| 224 | echo " git mergetool" |
| 225 | echo " git commit" |
| 226 | echo |
| 227 | echo "You can then complete the finish by running it again:" |
| 228 | echo " git flow feature finish $NAME" |
| 229 | echo |
| 230 | exit 1 |
| 231 | fi |
| 232 | |
| 233 | # when no merge conflict is detected, just clean up the feature branch |
| 234 | helper_finish_cleanup |
| 235 | } |
| 236 | |
| 237 | helper_finish_cleanup() { |
Vincent Driessen | f6f152f | 2010-01-29 10:28:08 +0100 | [diff] [blame] | 238 | # sanity checks |
| 239 | gitflow_require_branch $BRANCH |
| 240 | gitflow_check_clean_working_tree |
| 241 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 242 | # delete branch |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 243 | git push $ORIGIN :refs/heads/$BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 244 | git branch -d $BRANCH |
| 245 | |
| 246 | echo |
| 247 | echo "Summary of actions:" |
| 248 | echo "- The feature branch '$BRANCH' was merged into '$BASE'" |
| 249 | #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported |
| 250 | echo "- Feature branch '$BRANCH' has been removed" |
| 251 | echo "- You are now on branch '$BASE'" |
| 252 | echo |
| 253 | } |
| 254 | |
| 255 | cmd_publish() { |
| 256 | parse_args "$@" |
| 257 | |
| 258 | # sanity checks |
Vincent Driessen | 4838644 | 2010-01-29 10:30:40 +0100 | [diff] [blame] | 259 | gitflow_require_clean_working_tree |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 260 | gitflow_require_branch $BRANCH |
Benedikt Böhm | 4d22227 | 2010-01-26 14:46:56 +0100 | [diff] [blame] | 261 | git fetch -q $ORIGIN |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 262 | gitflow_require_branch_absent $ORIGIN/$BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 263 | |
| 264 | # create remote branch |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 265 | git push $ORIGIN $BRANCH:refs/heads/$BRANCH |
Benedikt Böhm | 4d22227 | 2010-01-26 14:46:56 +0100 | [diff] [blame] | 266 | git fetch -q $ORIGIN |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 267 | |
| 268 | # configure remote tracking |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 269 | git config branch.$BRANCH.remote $ORIGIN |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 270 | git config branch.$BRANCH.merge refs/heads/$BRANCH |
| 271 | git checkout $BRANCH |
| 272 | |
| 273 | echo |
| 274 | echo "Summary of actions:" |
| 275 | echo "- A new remote branch '$BRANCH' was created" |
| 276 | echo "- The local branch '$BRANCH' was configured to track the remote branch" |
| 277 | echo "- You are now on branch '$BRANCH'" |
| 278 | echo |
| 279 | } |
| 280 | |
| 281 | cmd_track() { |
| 282 | parse_args "$@" |
| 283 | |
| 284 | # sanity checks |
Vincent Driessen | 4838644 | 2010-01-29 10:30:40 +0100 | [diff] [blame] | 285 | gitflow_require_clean_working_tree |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 286 | gitflow_require_branch_absent $BRANCH |
Benedikt Böhm | 4d22227 | 2010-01-26 14:46:56 +0100 | [diff] [blame] | 287 | git fetch -q $ORIGIN |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 288 | gitflow_require_branch $ORIGIN/$BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 289 | |
| 290 | # create tracking branch |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 291 | git checkout -b $BRANCH $ORIGIN/$BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 292 | |
| 293 | echo |
| 294 | echo "Summary of actions:" |
| 295 | echo "- A new remote tracking branch '$BRANCH' was created" |
| 296 | echo "- You are now on branch '$BRANCH'" |
| 297 | echo |
| 298 | } |
Vincent Driessen | bd4f095 | 2010-01-27 13:57:15 +0100 | [diff] [blame] | 299 | |
Vincent Driessen | bd4f095 | 2010-01-27 13:57:15 +0100 | [diff] [blame] | 300 | cmd_diff() { |
| 301 | parse_args "$@" |
| 302 | # TODO: if this feature has been based on a non-develop branch, we really |
| 303 | # should not be comparing to $DEVELOP. How to deal with this? |
Vincent Driessen | 278884b | 2010-01-28 16:29:34 +0100 | [diff] [blame] | 304 | git diff $DEVELOP_BRANCH..$BRANCH |
Vincent Driessen | bd4f095 | 2010-01-27 13:57:15 +0100 | [diff] [blame] | 305 | } |