Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +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 | 186d2b5 | 2010-01-27 23:48:39 +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 | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 37 | # |
| 38 | |
| 39 | usage() { |
Vincent Driessen | a2e4116 | 2010-02-24 01:37:07 +0100 | [diff] [blame] | 40 | echo "usage: git flow init [-f]" |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 43 | parse_args() { |
| 44 | # parse options |
| 45 | FLAGS "$@" || exit $? |
| 46 | eval set -- "${FLAGS_ARGV}" |
| 47 | } |
| 48 | |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 49 | # Default entry when no SUBACTION is given |
| 50 | cmd_default() { |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 51 | DEFINE_boolean force false 'force setting of gitflow branches, even if already configured' f |
| 52 | parse_args "$@" |
| 53 | |
Vincent Driessen | 283b0f7 | 2010-02-15 23:23:14 +0100 | [diff] [blame] | 54 | if ! git rev-parse --git-dir >/dev/null 2>&1; then |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 55 | git init |
| 56 | else |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 57 | # assure that we are not working in a repo with local changes |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 58 | git_repo_is_headless || require_clean_working_tree |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 59 | fi |
| 60 | |
| 61 | # running git flow init on an already initialized repo is fine |
| 62 | if gitflow_is_initialized && ! flag force; then |
| 63 | warn "Already initialized for gitflow." |
| 64 | warn "To force reinitialization, use: git flow init -f" |
| 65 | exit 0 |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 66 | fi |
| 67 | |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 68 | local branch_count |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 69 | local answer |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 70 | |
| 71 | # add a master branch if no such branch exists yet |
| 72 | local master_branch |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 73 | if gitflow_has_master_configured && ! flag force; then |
| 74 | master_branch=$(git config --get gitflow.branch.master) |
| 75 | else |
| 76 | # Two cases are distinguished: |
| 77 | # 1. A fresh git repo (without any branches) |
| 78 | # We will create a new master/develop branch for the user |
| 79 | # 2. Some branches do already exist |
| 80 | # We will disallow creation of new master/develop branches and |
| 81 | # rather allow to use existing branches for git-flow. |
| 82 | local default_suggestion |
| 83 | local should_check_existence |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 84 | branch_count=$(git_local_branches | wc -l) |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 85 | if [ "$branch_count" -eq 0 ]; then |
| 86 | echo "No branches exist yet. Base branches must be created now." |
| 87 | should_check_existence=NO |
Vincent Driessen | b1033aa | 2010-03-23 21:10:13 +0100 | [diff] [blame] | 88 | default_suggestion=$(git config --get gitflow.branch.master || echo master) |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 89 | else |
| 90 | echo |
| 91 | echo "Which branch should be used for bringing forth production releases?" |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 92 | git_local_branches | sed 's/^.*$/ - &/g' |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 93 | |
| 94 | should_check_existence=YES |
| 95 | default_suggestion= |
Vincent Driessen | b1033aa | 2010-03-23 21:10:13 +0100 | [diff] [blame] | 96 | for guess in $(git config --get gitflow.branch.master) \ |
| 97 | 'production' 'main' 'master'; do |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 98 | if git_local_branch_exists "$guess"; then |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 99 | default_suggestion="$guess" |
| 100 | break |
| 101 | fi |
| 102 | done |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 103 | fi |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 104 | |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 105 | printf "Branch name for production releases: [$default_suggestion] " |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 106 | read answer |
| 107 | master_branch=${answer:-$default_suggestion} |
| 108 | |
| 109 | # check existence in case of an already existing repo |
| 110 | if [ "$should_check_existence" = "YES" ]; then |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 111 | git_local_branch_exists "$master_branch" || \ |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 112 | die "Local branch '$master_branch' does not exist." |
| 113 | fi |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 114 | |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 115 | # store the name of the master branch |
| 116 | git config gitflow.branch.master "$master_branch" |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 117 | fi |
| 118 | |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 119 | # add a develop branch if no such branch exists yet |
| 120 | local develop_branch |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 121 | if gitflow_has_develop_configured && ! flag force; then |
| 122 | develop_branch=$(git config --get gitflow.branch.develop) |
| 123 | else |
| 124 | # Again, the same two cases as with the master selection are |
| 125 | # considered (fresh repo or repo that contains branches) |
| 126 | local default_suggestion |
| 127 | local should_check_existence |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 128 | branch_count=$(git_local_branches | grep -v "^${master_branch}\$" | wc -l) |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 129 | if [ "$branch_count" -eq 0 ]; then |
| 130 | should_check_existence=NO |
Vincent Driessen | b1033aa | 2010-03-23 21:10:13 +0100 | [diff] [blame] | 131 | default_suggestion=$(git config --get gitflow.branch.develop || echo develop) |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 132 | else |
| 133 | echo |
| 134 | echo "Which branch should be used for integration of the \"next release\"?" |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 135 | git_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g' |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 136 | |
| 137 | should_check_existence=YES |
| 138 | default_suggestion= |
Vincent Driessen | b1033aa | 2010-03-23 21:10:13 +0100 | [diff] [blame] | 139 | for guess in $(git config --get gitflow.branch.develop) \ |
| 140 | 'develop' 'int' 'integration' 'master'; do |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 141 | if git_local_branch_exists "$guess"; then |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 142 | default_suggestion="$guess" |
| 143 | break |
| 144 | fi |
| 145 | done |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 146 | fi |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 147 | |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 148 | printf "Branch name for \"next release\" development: [$default_suggestion] " |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 149 | read answer |
| 150 | develop_branch=${answer:-$default_suggestion} |
| 151 | |
| 152 | if [ "$master_branch" = "$develop_branch" ]; then |
| 153 | die "Production and integration branches should differ." |
| 154 | fi |
| 155 | |
| 156 | # check existence in case of an already existing repo |
| 157 | if [ "$should_check_existence" = "YES" ]; then |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 158 | git_local_branch_exists "$develop_branch" || \ |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 159 | die "Local branch '$develop_branch' does not exist." |
| 160 | fi |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 161 | |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 162 | # store the name of the develop branch |
| 163 | git config gitflow.branch.develop "$develop_branch" |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 164 | fi |
| 165 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 166 | # Creation of HEAD |
| 167 | # ---------------- |
| 168 | # We create a HEAD now, if it does not exist yet (in a fresh repo). We need |
| 169 | # it to be able to create new branches. |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame] | 170 | local created_gitflow_branch=0 |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 171 | if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 172 | git symbolic-ref HEAD "refs/heads/$master_branch" |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 173 | git commit --allow-empty --quiet -m "Initial commit" |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame] | 174 | created_gitflow_branch=1 |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 175 | fi |
| 176 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 177 | # Creation of master |
| 178 | # ------------------ |
| 179 | # At this point, there always is a master branch: either it existed already |
| 180 | # (and was picked interactively as the production branch) or it has just |
| 181 | # been created in a fresh repo |
| 182 | |
| 183 | # Creation of develop |
| 184 | # ------------------- |
| 185 | # The develop branch possibly does not exist yet. This is the case when, |
| 186 | # in a git init'ed repo with one or more commits, master was picked as the |
| 187 | # default production branch and develop was "created". We should create |
| 188 | # the develop branch now in that case (we base it on master, of course) |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 189 | if ! git_local_branch_exists "$develop_branch"; then |
Vincent Driessen | 3176f74 | 2010-03-25 15:30:58 +0100 | [diff] [blame] | 190 | git branch --no-track "$develop_branch" "$master_branch" |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame] | 191 | created_gitflow_branch=1 |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 192 | fi |
| 193 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 194 | # assert the gitflow repo has been correctly initialized |
| 195 | gitflow_is_initialized |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 196 | |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame] | 197 | # switch to develop branch if its newly created |
| 198 | if [ $created_gitflow_branch -eq 1 ]; then |
| 199 | git checkout -q "$develop_branch" |
| 200 | fi |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 201 | |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 202 | # finally, ask the user for naming conventions (branch and tag prefixes) |
Vincent Driessen | b1033aa | 2010-03-23 21:10:13 +0100 | [diff] [blame] | 203 | if flag force || \ |
| 204 | ! git config --get gitflow.prefix.feature >/dev/null 2>&1 || |
| 205 | ! git config --get gitflow.prefix.release >/dev/null 2>&1 || |
| 206 | ! git config --get gitflow.prefix.hotfix >/dev/null 2>&1 || |
| 207 | ! git config --get gitflow.prefix.support >/dev/null 2>&1 || |
| 208 | ! git config --get gitflow.prefix.versiontag >/dev/null 2>&1; then |
| 209 | echo |
| 210 | echo "How to name your supporting branch prefixes?" |
| 211 | fi |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 212 | |
| 213 | local prefix |
| 214 | |
| 215 | # Feature branches |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 216 | if ! git config --get gitflow.prefix.feature >/dev/null 2>&1 || flag force; then |
| 217 | default_suggestion=$(git config --get gitflow.prefix.feature || echo feature/) |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 218 | printf "Feature branches? [$default_suggestion] " |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 219 | read answer |
| 220 | [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} |
| 221 | git config gitflow.prefix.feature "$prefix" |
| 222 | fi |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 223 | |
| 224 | # Release branches |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 225 | if ! git config --get gitflow.prefix.release >/dev/null 2>&1 || flag force; then |
| 226 | default_suggestion=$(git config --get gitflow.prefix.release || echo release/) |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 227 | printf "Release branches? [$default_suggestion] " |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 228 | read answer |
| 229 | [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} |
| 230 | git config gitflow.prefix.release "$prefix" |
| 231 | fi |
| 232 | |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 233 | |
| 234 | # Hotfix branches |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 235 | if ! git config --get gitflow.prefix.hotfix >/dev/null 2>&1 || flag force; then |
| 236 | default_suggestion=$(git config --get gitflow.prefix.hotfix || echo hotfix/) |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 237 | printf "Hotfix branches? [$default_suggestion] " |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 238 | read answer |
| 239 | [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} |
| 240 | git config gitflow.prefix.hotfix "$prefix" |
| 241 | fi |
| 242 | |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 243 | |
| 244 | # Support branches |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 245 | if ! git config --get gitflow.prefix.support >/dev/null 2>&1 || flag force; then |
| 246 | default_suggestion=$(git config --get gitflow.prefix.support || echo support/) |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 247 | printf "Support branches? [$default_suggestion] " |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 248 | read answer |
| 249 | [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} |
| 250 | git config gitflow.prefix.support "$prefix" |
| 251 | fi |
| 252 | |
Vincent Driessen | f476d26 | 2010-02-20 16:23:47 +0100 | [diff] [blame] | 253 | |
| 254 | # Version tag prefix |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 255 | if ! git config --get gitflow.prefix.versiontag >/dev/null 2>&1 || flag force; then |
| 256 | default_suggestion=$(git config --get gitflow.prefix.versiontag || echo "") |
Vincent Driessen | f6228ed | 2010-03-23 21:19:54 +0100 | [diff] [blame] | 257 | printf "Version tag prefix? [$default_suggestion] " |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 258 | read answer |
| 259 | [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} |
| 260 | git config gitflow.prefix.versiontag "$prefix" |
| 261 | fi |
| 262 | |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 263 | |
| 264 | # TODO: what to do with origin? |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 267 | cmd_help() { |
| 268 | usage |
| 269 | exit 0 |
| 270 | } |