Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 1 | # |
| 2 | # git-flow -- A collection of Git extensions to provide high-level |
| 3 | # repository operations for Vincent Driessen's branching model. |
| 4 | # |
| 5 | # Original blog post presenting this model is found at: |
Vincent Driessen | ddb350b | 2010-07-09 09:42:09 +0200 | [diff] [blame] | 6 | # http://nvie.com/git-model |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 7 | # |
| 8 | # Feel free to contribute to this project at: |
| 9 | # http://github.com/nvie/gitflow |
| 10 | # |
Vincent Driessen | d72acba | 2010-04-04 16:10:17 +0200 | [diff] [blame] | 11 | # Copyright 2010 Vincent Driessen. All rights reserved. |
| 12 | # |
| 13 | # Redistribution and use in source and binary forms, with or without |
| 14 | # modification, are permitted provided that the following conditions are met: |
| 15 | # |
| 16 | # 1. Redistributions of source code must retain the above copyright notice, |
| 17 | # this list of conditions and the following disclaimer. |
| 18 | # |
| 19 | # 2. Redistributions in binary form must reproduce the above copyright |
| 20 | # notice, this list of conditions and the following disclaimer in the |
| 21 | # documentation and/or other materials provided with the distribution. |
| 22 | # |
| 23 | # THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR |
| 24 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 26 | # EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 27 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 30 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 32 | # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | # |
| 34 | # The views and conclusions contained in the software and documentation are |
| 35 | # those of the authors and should not be interpreted as representing official |
| 36 | # policies, either expressed or implied, of Vincent Driessen. |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 37 | # |
| 38 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 39 | # |
| 40 | # Common functionality |
| 41 | # |
| 42 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 43 | # shell output |
| 44 | warn() { echo "$@" >&2; } |
| 45 | die() { warn "$@"; exit 1; } |
| 46 | |
Vincent Driessen | 1b471a6 | 2011-02-04 08:25:38 +0100 | [diff] [blame] | 47 | escape() { |
| 48 | echo "$1" | sed 's/\([\.\+\$\*]\)/\\\1/g' |
| 49 | } |
| 50 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 51 | # set logic |
| 52 | has() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 53 | local item=$1; shift |
Vincent Driessen | 1b471a6 | 2011-02-04 08:25:38 +0100 | [diff] [blame] | 54 | echo " $@ " | grep -q " $(escape $item) " |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | # basic math |
| 58 | min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; } |
| 59 | max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; } |
| 60 | |
| 61 | # basic string matching |
| 62 | startswith() { [ "$1" != "${1#$2}" ]; } |
Vincent Driessen | e0d8af3 | 2010-02-22 12:21:36 +0100 | [diff] [blame] | 63 | endswith() { [ "$1" != "${1%$2}" ]; } |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 64 | |
| 65 | # convenience functions for checking shFlags flags |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 66 | flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; } |
| 67 | noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; } |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 68 | |
| 69 | # |
| 70 | # Git specific common functionality |
| 71 | # |
| 72 | |
Adam Gibbins | cf3da5a | 2010-08-22 19:13:34 +0100 | [diff] [blame] | 73 | git_local_branches() { git branch --no-color | sed 's/^[* ] //'; } |
| 74 | git_remote_branches() { git branch -r --no-color | sed 's/^[* ] //'; } |
| 75 | git_all_branches() { ( git branch --no-color; git branch -r --no-color) | sed 's/^[* ] //'; } |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 76 | git_all_tags() { git tag; } |
| 77 | |
Vincent Driessen | 55c1553 | 2010-02-22 07:28:27 +0100 | [diff] [blame] | 78 | git_current_branch() { |
Adam Gibbins | cf3da5a | 2010-08-22 19:13:34 +0100 | [diff] [blame] | 79 | git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g' |
Vincent Driessen | 55c1553 | 2010-02-22 07:28:27 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | git_is_clean_working_tree() { |
| 83 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then |
| 84 | return 1 |
| 85 | elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then |
| 86 | return 2 |
| 87 | else |
| 88 | return 0 |
| 89 | fi |
| 90 | } |
| 91 | |
| 92 | git_repo_is_headless() { |
| 93 | ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1 |
| 94 | } |
| 95 | |
| 96 | git_local_branch_exists() { |
| 97 | has $1 $(git_local_branches) |
| 98 | } |
| 99 | |
| 100 | git_branch_exists() { |
| 101 | has $1 $(git_all_branches) |
| 102 | } |
| 103 | |
| 104 | git_tag_exists() { |
| 105 | has $1 $(git_all_tags) |
| 106 | } |
| 107 | |
| 108 | # |
| 109 | # git_compare_branches() |
| 110 | # |
| 111 | # Tests whether branches and their "origin" counterparts have diverged and need |
| 112 | # merging first. It returns error codes to provide more detail, like so: |
| 113 | # |
| 114 | # 0 Branch heads point to the same commit |
| 115 | # 1 First given branch needs fast-forwarding |
| 116 | # 2 Second given branch needs fast-forwarding |
| 117 | # 3 Branch needs a real merge |
| 118 | # 4 There is no merge base, i.e. the branches have no common ancestors |
| 119 | # |
| 120 | git_compare_branches() { |
| 121 | local commit1=$(git rev-parse "$1") |
| 122 | local commit2=$(git rev-parse "$2") |
| 123 | if [ "$commit1" != "$commit2" ]; then |
| 124 | local base=$(git merge-base "$commit1" "$commit2") |
| 125 | if [ $? -ne 0 ]; then |
| 126 | return 4 |
| 127 | elif [ "$commit1" = "$base" ]; then |
| 128 | return 1 |
| 129 | elif [ "$commit2" = "$base" ]; then |
| 130 | return 2 |
| 131 | else |
| 132 | return 3 |
| 133 | fi |
| 134 | else |
| 135 | return 0 |
| 136 | fi |
| 137 | } |
| 138 | |
| 139 | # |
| 140 | # git_is_branch_merged_into() |
| 141 | # |
| 142 | # Checks whether branch $1 is succesfully merged into $2 |
| 143 | # |
| 144 | git_is_branch_merged_into() { |
| 145 | local subject=$1 |
| 146 | local base=$2 |
Brian St. Pierre | 5ff0b47 | 2010-11-03 16:40:58 -0400 | [diff] [blame] | 147 | local all_merges="$(git branch --no-color --contains $subject | sed 's/^[* ] //')" |
Vincent Driessen | 55c1553 | 2010-02-22 07:28:27 +0100 | [diff] [blame] | 148 | has $base $all_merges |
| 149 | } |
| 150 | |
| 151 | # |
| 152 | # gitflow specific common functionality |
| 153 | # |
| 154 | |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 155 | # check if this repo has been inited for gitflow |
| 156 | gitflow_has_master_configured() { |
| 157 | local master=$(git config --get gitflow.branch.master) |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 158 | [ "$master" != "" ] && git_local_branch_exists "$master" |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | gitflow_has_develop_configured() { |
| 162 | local develop=$(git config --get gitflow.branch.develop) |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 163 | [ "$develop" != "" ] && git_local_branch_exists "$develop" |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 166 | gitflow_has_prefixes_configured() { |
| 167 | git config --get gitflow.prefix.feature >/dev/null 2>&1 && \ |
| 168 | git config --get gitflow.prefix.release >/dev/null 2>&1 && \ |
| 169 | git config --get gitflow.prefix.hotfix >/dev/null 2>&1 && \ |
| 170 | git config --get gitflow.prefix.support >/dev/null 2>&1 && \ |
| 171 | git config --get gitflow.prefix.versiontag >/dev/null 2>&1 |
| 172 | } |
| 173 | |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 174 | gitflow_is_initialized() { |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 175 | gitflow_has_master_configured && \ |
| 176 | gitflow_has_develop_configured && \ |
| 177 | [ "$(git config --get gitflow.branch.master)" != \ |
| 178 | "$(git config --get gitflow.branch.develop)" ] && \ |
| 179 | gitflow_has_prefixes_configured |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 182 | # loading settings that can be overridden using git config |
| 183 | gitflow_load_settings() { |
Vincent Driessen | a7a89cd | 2011-04-17 08:41:36 +0200 | [diff] [blame] | 184 | export DOT_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) |
Vincent Driessen | c1598bf | 2010-02-20 16:52:48 +0100 | [diff] [blame] | 185 | export MASTER_BRANCH=$(git config --get gitflow.branch.master) |
| 186 | export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop) |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 187 | export ORIGIN=$(git config --get gitflow.origin || echo origin) |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 190 | # |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 191 | # gitflow_resolve_nameprefix |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 192 | # |
| 193 | # Inputs: |
| 194 | # $1 = name prefix to resolve |
| 195 | # $2 = branch prefix to use |
| 196 | # |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 197 | # Searches branch names from git_local_branches() to look for a unique |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 198 | # branch name whose name starts with the given name prefix. |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 199 | # |
| 200 | # There are multiple exit codes possible: |
| 201 | # 0: The unambiguous full name of the branch is written to stdout |
| 202 | # (success) |
| 203 | # 1: No match is found. |
| 204 | # 2: Multiple matches found. These matches are written to stderr |
| 205 | # |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 206 | gitflow_resolve_nameprefix() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 207 | local name=$1 |
| 208 | local prefix=$2 |
| 209 | local matches |
| 210 | local num_matches |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 211 | |
| 212 | # first, check if there is a perfect match |
Vincent Driessen | 5b05ad7 | 2010-05-27 11:51:50 -0700 | [diff] [blame] | 213 | if git_local_branch_exists "$prefix$name"; then |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 214 | echo "$name" |
| 215 | return 0 |
| 216 | fi |
| 217 | |
Vincent Driessen | 1b471a6 | 2011-02-04 08:25:38 +0100 | [diff] [blame] | 218 | matches=$(echo "$(git_local_branches)" | grep "^$(escape "$prefix$name")") |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 219 | num_matches=$(echo "$matches" | wc -l) |
| 220 | if [ -z "$matches" ]; then |
| 221 | # no prefix match, so take it literally |
| 222 | warn "No branch matches prefix '$name'" |
| 223 | return 1 |
| 224 | else |
| 225 | if [ $num_matches -eq 1 ]; then |
| 226 | echo "${matches#$prefix}" |
| 227 | return 0 |
| 228 | else |
| 229 | # multiple matches, cannot decide |
| 230 | warn "Multiple branches match prefix '$name':" |
| 231 | for match in $matches; do |
| 232 | warn "- $match" |
| 233 | done |
| 234 | return 2 |
| 235 | fi |
| 236 | fi |
| 237 | } |
| 238 | |
Vincent Driessen | 55c1553 | 2010-02-22 07:28:27 +0100 | [diff] [blame] | 239 | # |
| 240 | # Assertions for use in git-flow subcommands |
| 241 | # |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 242 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 243 | require_git_repo() { |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 244 | if ! git rev-parse --git-dir >/dev/null 2>&1; then |
Vincent Driessen | c1598bf | 2010-02-20 16:52:48 +0100 | [diff] [blame] | 245 | die "fatal: Not a git repository" |
| 246 | fi |
| 247 | } |
| 248 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 249 | require_gitflow_initialized() { |
Vincent Driessen | c1598bf | 2010-02-20 16:52:48 +0100 | [diff] [blame] | 250 | if ! gitflow_is_initialized; then |
| 251 | die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first." |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 252 | fi |
| 253 | } |
| 254 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 255 | require_clean_working_tree() { |
| 256 | git_is_clean_working_tree |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 257 | local result=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 258 | if [ $result -eq 1 ]; then |
| 259 | die "fatal: Working tree contains unstaged changes. Aborting." |
| 260 | fi |
| 261 | if [ $result -eq 2 ]; then |
| 262 | die "fatal: Index contains uncommited changes. Aborting." |
| 263 | fi |
| 264 | } |
| 265 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 266 | require_local_branch() { |
Vincent Driessen | 6ee6223 | 2010-02-22 07:45:24 +0100 | [diff] [blame] | 267 | if ! git_local_branch_exists $1; then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 268 | die "fatal: Local branch '$1' does not exist and is required." |
| 269 | fi |
| 270 | } |
| 271 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 272 | require_remote_branch() { |
| 273 | if ! has $1 $(git_remote_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 274 | die "Remote branch '$1' does not exist and is required." |
| 275 | fi |
| 276 | } |
| 277 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 278 | require_branch() { |
| 279 | if ! has $1 $(git_all_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 280 | die "Branch '$1' does not exist and is required." |
| 281 | fi |
| 282 | } |
| 283 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 284 | require_branch_absent() { |
| 285 | if has $1 $(git_all_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 286 | die "Branch '$1' already exists. Pick another name." |
| 287 | fi |
| 288 | } |
| 289 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 290 | require_tag_absent() { |
Jannis Leidel | 8f280e0 | 2011-03-28 18:48:39 +0200 | [diff] [blame] | 291 | for tag in $(git_all_tags); do |
| 292 | if [ "$1" = "$tag" ]; then |
| 293 | die "Tag '$1' already exists. Pick another name." |
| 294 | fi |
| 295 | done |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 298 | require_branches_equal() { |
| 299 | require_local_branch "$1" |
| 300 | require_remote_branch "$2" |
| 301 | git_compare_branches "$1" "$2" |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 302 | local status=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 303 | if [ $status -gt 0 ]; then |
| 304 | warn "Branches '$1' and '$2' have diverged." |
| 305 | if [ $status -eq 1 ]; then |
| 306 | die "And branch '$1' may be fast-forwarded." |
| 307 | elif [ $status -eq 2 ]; then |
| 308 | # Warn here, since there is no harm in being ahead |
| 309 | warn "And local branch '$1' is ahead of '$2'." |
| 310 | else |
| 311 | die "Branches need merging first." |
| 312 | fi |
| 313 | fi |
| 314 | } |