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