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