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