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