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 |
| 49 | |
| 50 | # add a master branch if no such branch exists yet |
| 51 | local master_branch |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 52 | if gitflow_has_master_configured && ! flag force; then |
| 53 | master_branch=$(git config --get gitflow.branch.master) |
| 54 | else |
| 55 | # Two cases are distinguished: |
| 56 | # 1. A fresh git repo (without any branches) |
| 57 | # We will create a new master/develop branch for the user |
| 58 | # 2. Some branches do already exist |
| 59 | # We will disallow creation of new master/develop branches and |
| 60 | # rather allow to use existing branches for git-flow. |
| 61 | local default_suggestion |
| 62 | local should_check_existence |
| 63 | branch_count=$(gitflow_local_branches | wc -l) |
| 64 | if [ "$branch_count" -eq 0 ]; then |
| 65 | echo "No branches exist yet. Base branches must be created now." |
| 66 | should_check_existence=NO |
| 67 | default_suggestion=master |
| 68 | else |
| 69 | echo |
| 70 | echo "Which branch should be used for bringing forth production releases?" |
| 71 | gitflow_local_branches | sed 's/^.*$/ - &/g' |
| 72 | |
| 73 | should_check_existence=YES |
| 74 | default_suggestion= |
| 75 | for guess in 'production' 'main' 'master'; do |
| 76 | if gitflow_local_branch_exists "$guess"; then |
| 77 | default_suggestion="$guess" |
| 78 | break |
| 79 | fi |
| 80 | done |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 81 | fi |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 82 | |
| 83 | echo "Branch name for production releases: [$default_suggestion] \c" |
| 84 | read answer |
| 85 | master_branch=${answer:-$default_suggestion} |
| 86 | |
| 87 | # check existence in case of an already existing repo |
| 88 | if [ "$should_check_existence" = "YES" ]; then |
| 89 | gitflow_local_branch_exists "$master_branch" || \ |
| 90 | die "Local branch '$master_branch' does not exist." |
| 91 | fi |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 92 | |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 93 | # store the name of the master branch |
| 94 | git config gitflow.branch.master "$master_branch" |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 95 | fi |
| 96 | |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 97 | # add a develop branch if no such branch exists yet |
| 98 | local develop_branch |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 99 | if gitflow_has_develop_configured && ! flag force; then |
| 100 | develop_branch=$(git config --get gitflow.branch.develop) |
| 101 | else |
| 102 | # Again, the same two cases as with the master selection are |
| 103 | # considered (fresh repo or repo that contains branches) |
| 104 | local default_suggestion |
| 105 | local should_check_existence |
| 106 | branch_count=$(gitflow_local_branches | grep -v "^${master_branch}\$" | wc -l) |
| 107 | if [ "$branch_count" -eq 0 ]; then |
| 108 | should_check_existence=NO |
| 109 | default_suggestion=develop |
| 110 | else |
| 111 | echo |
| 112 | echo "Which branch should be used for integration of the \"next release\"?" |
| 113 | gitflow_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g' |
| 114 | |
| 115 | should_check_existence=YES |
| 116 | default_suggestion= |
| 117 | for guess in 'develop' 'int' 'integration' 'master'; do |
| 118 | if gitflow_local_branch_exists "$guess"; then |
| 119 | default_suggestion="$guess" |
| 120 | break |
| 121 | fi |
| 122 | done |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 123 | fi |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 124 | |
| 125 | echo "Branch name for \"next release\" development: [$default_suggestion] \c" |
| 126 | read answer |
| 127 | develop_branch=${answer:-$default_suggestion} |
| 128 | |
| 129 | if [ "$master_branch" = "$develop_branch" ]; then |
| 130 | die "Production and integration branches should differ." |
| 131 | fi |
| 132 | |
| 133 | # check existence in case of an already existing repo |
| 134 | if [ "$should_check_existence" = "YES" ]; then |
| 135 | gitflow_local_branch_exists "$develop_branch" || \ |
| 136 | die "Local branch '$develop_branch' does not exist." |
| 137 | fi |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 138 | |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 139 | # store the name of the develop branch |
| 140 | git config gitflow.branch.develop "$develop_branch" |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 141 | fi |
| 142 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 143 | # Creation of HEAD |
| 144 | # ---------------- |
| 145 | # We create a HEAD now, if it does not exist yet (in a fresh repo). We need |
| 146 | # it to be able to create new branches. |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame^] | 147 | local created_gitflow_branch=0 |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 148 | if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 149 | git symbolic-ref HEAD "refs/heads/$master_branch" |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 150 | git commit --allow-empty --quiet -m "Initial commit" |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame^] | 151 | created_gitflow_branch=1 |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 152 | fi |
| 153 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 154 | # Creation of master |
| 155 | # ------------------ |
| 156 | # At this point, there always is a master branch: either it existed already |
| 157 | # (and was picked interactively as the production branch) or it has just |
| 158 | # been created in a fresh repo |
| 159 | |
| 160 | # Creation of develop |
| 161 | # ------------------- |
| 162 | # The develop branch possibly does not exist yet. This is the case when, |
| 163 | # in a git init'ed repo with one or more commits, master was picked as the |
| 164 | # default production branch and develop was "created". We should create |
| 165 | # the develop branch now in that case (we base it on master, of course) |
| 166 | if ! gitflow_local_branch_exists "$develop_branch"; then |
| 167 | git branch "$develop_branch" "$master_branch" |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame^] | 168 | created_gitflow_branch=1 |
Vincent Driessen | 0161de5 | 2010-02-18 12:07:34 +0100 | [diff] [blame] | 169 | fi |
| 170 | |
Vincent Driessen | 131c298 | 2010-02-20 14:30:16 +0100 | [diff] [blame] | 171 | # assert the gitflow repo has been correctly initialized |
| 172 | gitflow_is_initialized |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 173 | |
Vincent Driessen | 3227d80 | 2010-02-20 16:13:23 +0100 | [diff] [blame^] | 174 | # switch to develop branch if its newly created |
| 175 | if [ $created_gitflow_branch -eq 1 ]; then |
| 176 | git checkout -q "$develop_branch" |
| 177 | fi |
Vincent Driessen | 6188206 | 2010-02-18 12:32:20 +0100 | [diff] [blame] | 178 | |
| 179 | # TODO: finally, ask the user for naming convention preferences |
| 180 | # i.e. tag prefixes, prefixes for supporting branches, etc. |
| 181 | |
| 182 | # TODO: what to do with origin? |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 185 | cmd_help() { |
| 186 | usage |
| 187 | exit 0 |
| 188 | } |