blob: d533934ea15b43e84643adfe566acf74169415cf [file] [log] [blame]
Vincent Driessen186d2b52010-01-27 23:48:39 +01001#
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
15usage() {
16 echo "usage: git flow init"
17}
18
Vincent Driessen131c2982010-02-20 14:30:16 +010019parse_args() {
20 # parse options
21 FLAGS "$@" || exit $?
22 eval set -- "${FLAGS_ARGV}"
23}
24
Vincent Driessen186d2b52010-01-27 23:48:39 +010025# Default entry when no SUBACTION is given
26cmd_default() {
Vincent Driessen131c2982010-02-20 14:30:16 +010027 DEFINE_boolean force false 'force setting of gitflow branches, even if already configured' f
28 parse_args "$@"
29
Vincent Driessen283b0f72010-02-15 23:23:14 +010030 if ! git rev-parse --git-dir >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +010031 git init
32 else
Vincent Driessen131c2982010-02-20 14:30:16 +010033 # assure that we are not working in a repo with local changes
Vincent Driessen7832d6e2010-02-21 21:31:03 +010034 git_repo_is_headless || require_clean_working_tree
Vincent Driessen131c2982010-02-20 14:30:16 +010035 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 Driessen186d2b52010-01-27 23:48:39 +010042 fi
43
Vincent Driessen0161de52010-02-18 12:07:34 +010044 local branch_count
Vincent Driessenf476d262010-02-20 16:23:47 +010045 local answer
Vincent Driessen0161de52010-02-18 12:07:34 +010046
47 # add a master branch if no such branch exists yet
48 local master_branch
Vincent Driessen131c2982010-02-20 14:30:16 +010049 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 Driessen7832d6e2010-02-21 21:31:03 +010060 branch_count=$(git_local_branches | wc -l)
Vincent Driessen131c2982010-02-20 14:30:16 +010061 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 Driessen7832d6e2010-02-21 21:31:03 +010068 git_local_branches | sed 's/^.*$/ - &/g'
Vincent Driessen131c2982010-02-20 14:30:16 +010069
70 should_check_existence=YES
71 default_suggestion=
72 for guess in 'production' 'main' 'master'; do
Vincent Driessen7832d6e2010-02-21 21:31:03 +010073 if git_local_branch_exists "$guess"; then
Vincent Driessen131c2982010-02-20 14:30:16 +010074 default_suggestion="$guess"
75 break
76 fi
77 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010078 fi
Vincent Driessen131c2982010-02-20 14:30:16 +010079
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 Driessen7832d6e2010-02-21 21:31:03 +010086 git_local_branch_exists "$master_branch" || \
Vincent Driessen131c2982010-02-20 14:30:16 +010087 die "Local branch '$master_branch' does not exist."
88 fi
Vincent Driessen0161de52010-02-18 12:07:34 +010089
Vincent Driessen61882062010-02-18 12:32:20 +010090 # store the name of the master branch
91 git config gitflow.branch.master "$master_branch"
Vincent Driessen186d2b52010-01-27 23:48:39 +010092 fi
93
Vincent Driessen0161de52010-02-18 12:07:34 +010094 # add a develop branch if no such branch exists yet
95 local develop_branch
Vincent Driessen131c2982010-02-20 14:30:16 +010096 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 Driessen7832d6e2010-02-21 21:31:03 +0100103 branch_count=$(git_local_branches | grep -v "^${master_branch}\$" | wc -l)
Vincent Driessen131c2982010-02-20 14:30:16 +0100104 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 Driessen7832d6e2010-02-21 21:31:03 +0100110 git_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
Vincent Driessen131c2982010-02-20 14:30:16 +0100111
112 should_check_existence=YES
113 default_suggestion=
114 for guess in 'develop' 'int' 'integration' 'master'; do
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100115 if git_local_branch_exists "$guess"; then
Vincent Driessen131c2982010-02-20 14:30:16 +0100116 default_suggestion="$guess"
117 break
118 fi
119 done
Vincent Driessen0161de52010-02-18 12:07:34 +0100120 fi
Vincent Driessen131c2982010-02-20 14:30:16 +0100121
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 Driessen7832d6e2010-02-21 21:31:03 +0100132 git_local_branch_exists "$develop_branch" || \
Vincent Driessen131c2982010-02-20 14:30:16 +0100133 die "Local branch '$develop_branch' does not exist."
134 fi
Vincent Driessen0161de52010-02-18 12:07:34 +0100135
Vincent Driessen61882062010-02-18 12:32:20 +0100136 # store the name of the develop branch
137 git config gitflow.branch.develop "$develop_branch"
Vincent Driessen0161de52010-02-18 12:07:34 +0100138 fi
139
Vincent Driessen131c2982010-02-20 14:30:16 +0100140 # 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 Driessen3227d802010-02-20 16:13:23 +0100144 local created_gitflow_branch=0
Vincent Driessen0161de52010-02-18 12:07:34 +0100145 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +0100146 git symbolic-ref HEAD "refs/heads/$master_branch"
Vincent Driessen61882062010-02-18 12:32:20 +0100147 git commit --allow-empty --quiet -m "Initial commit"
Vincent Driessen3227d802010-02-20 16:13:23 +0100148 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100149 fi
150
Vincent Driessen131c2982010-02-20 14:30:16 +0100151 # 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 Driessen7832d6e2010-02-21 21:31:03 +0100163 if ! git_local_branch_exists "$develop_branch"; then
Vincent Driessen131c2982010-02-20 14:30:16 +0100164 git branch "$develop_branch" "$master_branch"
Vincent Driessen3227d802010-02-20 16:13:23 +0100165 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100166 fi
167
Vincent Driessen131c2982010-02-20 14:30:16 +0100168 # assert the gitflow repo has been correctly initialized
169 gitflow_is_initialized
Vincent Driessen186d2b52010-01-27 23:48:39 +0100170
Vincent Driessen3227d802010-02-20 16:13:23 +0100171 # 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 Driessen61882062010-02-18 12:32:20 +0100175
Vincent Driessenf476d262010-02-20 16:23:47 +0100176 # 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 Driessen1d8bb0d2010-02-20 16:46:38 +0100183 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 Driessenf476d262010-02-20 16:23:47 +0100190
191 # Release branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100192 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 Driessenf476d262010-02-20 16:23:47 +0100200
201 # Hotfix branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100202 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 Driessenf476d262010-02-20 16:23:47 +0100210
211 # Support branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100212 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 Driessenf476d262010-02-20 16:23:47 +0100220
221 # Version tag prefix
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100222 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 Driessen61882062010-02-18 12:32:20 +0100230
231 # TODO: what to do with origin?
Vincent Driessen186d2b52010-01-27 23:48:39 +0100232}
233
Vincent Driessenb866b012010-01-28 01:01:53 +0100234cmd_help() {
235 usage
236 exit 0
237}