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