blob: e34cb14436d90922cb735ca83dfa2b11a01a098c [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() {
Vincent Driessena2e41162010-02-24 01:37:07 +010016 echo "usage: git flow init [-f]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010017}
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
Vincent Driessenb1033aa2010-03-23 21:10:13 +010064 default_suggestion=$(git config --get gitflow.branch.master || echo master)
Vincent Driessen131c2982010-02-20 14:30:16 +010065 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=
Vincent Driessenb1033aa2010-03-23 21:10:13 +010072 for guess in $(git config --get gitflow.branch.master) \
73 'production' 'main' 'master'; do
Vincent Driessen7832d6e2010-02-21 21:31:03 +010074 if git_local_branch_exists "$guess"; then
Vincent Driessen131c2982010-02-20 14:30:16 +010075 default_suggestion="$guess"
76 break
77 fi
78 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010079 fi
Vincent Driessen131c2982010-02-20 14:30:16 +010080
Vincent Driessenf6228ed2010-03-23 21:19:54 +010081 printf "Branch name for production releases: [$default_suggestion] "
Vincent Driessen131c2982010-02-20 14:30:16 +010082 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 Driessen7832d6e2010-02-21 21:31:03 +010087 git_local_branch_exists "$master_branch" || \
Vincent Driessen131c2982010-02-20 14:30:16 +010088 die "Local branch '$master_branch' does not exist."
89 fi
Vincent Driessen0161de52010-02-18 12:07:34 +010090
Vincent Driessen61882062010-02-18 12:32:20 +010091 # store the name of the master branch
92 git config gitflow.branch.master "$master_branch"
Vincent Driessen186d2b52010-01-27 23:48:39 +010093 fi
94
Vincent Driessen0161de52010-02-18 12:07:34 +010095 # add a develop branch if no such branch exists yet
96 local develop_branch
Vincent Driessen131c2982010-02-20 14:30:16 +010097 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 Driessen7832d6e2010-02-21 21:31:03 +0100104 branch_count=$(git_local_branches | grep -v "^${master_branch}\$" | wc -l)
Vincent Driessen131c2982010-02-20 14:30:16 +0100105 if [ "$branch_count" -eq 0 ]; then
106 should_check_existence=NO
Vincent Driessenb1033aa2010-03-23 21:10:13 +0100107 default_suggestion=$(git config --get gitflow.branch.develop || echo develop)
Vincent Driessen131c2982010-02-20 14:30:16 +0100108 else
109 echo
110 echo "Which branch should be used for integration of the \"next release\"?"
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100111 git_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
Vincent Driessen131c2982010-02-20 14:30:16 +0100112
113 should_check_existence=YES
114 default_suggestion=
Vincent Driessenb1033aa2010-03-23 21:10:13 +0100115 for guess in $(git config --get gitflow.branch.develop) \
116 'develop' 'int' 'integration' 'master'; do
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100117 if git_local_branch_exists "$guess"; then
Vincent Driessen131c2982010-02-20 14:30:16 +0100118 default_suggestion="$guess"
119 break
120 fi
121 done
Vincent Driessen0161de52010-02-18 12:07:34 +0100122 fi
Vincent Driessen131c2982010-02-20 14:30:16 +0100123
Vincent Driessenf6228ed2010-03-23 21:19:54 +0100124 printf "Branch name for \"next release\" development: [$default_suggestion] "
Vincent Driessen131c2982010-02-20 14:30:16 +0100125 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 Driessen7832d6e2010-02-21 21:31:03 +0100134 git_local_branch_exists "$develop_branch" || \
Vincent Driessen131c2982010-02-20 14:30:16 +0100135 die "Local branch '$develop_branch' does not exist."
136 fi
Vincent Driessen0161de52010-02-18 12:07:34 +0100137
Vincent Driessen61882062010-02-18 12:32:20 +0100138 # store the name of the develop branch
139 git config gitflow.branch.develop "$develop_branch"
Vincent Driessen0161de52010-02-18 12:07:34 +0100140 fi
141
Vincent Driessen131c2982010-02-20 14:30:16 +0100142 # 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 Driessen3227d802010-02-20 16:13:23 +0100146 local created_gitflow_branch=0
Vincent Driessen0161de52010-02-18 12:07:34 +0100147 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +0100148 git symbolic-ref HEAD "refs/heads/$master_branch"
Vincent Driessen61882062010-02-18 12:32:20 +0100149 git commit --allow-empty --quiet -m "Initial commit"
Vincent Driessen3227d802010-02-20 16:13:23 +0100150 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100151 fi
152
Vincent Driessen131c2982010-02-20 14:30:16 +0100153 # 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 Driessen7832d6e2010-02-21 21:31:03 +0100165 if ! git_local_branch_exists "$develop_branch"; then
Vincent Driessen3176f742010-03-25 15:30:58 +0100166 git branch --no-track "$develop_branch" "$master_branch"
Vincent Driessen3227d802010-02-20 16:13:23 +0100167 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100168 fi
169
Vincent Driessen131c2982010-02-20 14:30:16 +0100170 # assert the gitflow repo has been correctly initialized
171 gitflow_is_initialized
Vincent Driessen186d2b52010-01-27 23:48:39 +0100172
Vincent Driessen3227d802010-02-20 16:13:23 +0100173 # 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 Driessen61882062010-02-18 12:32:20 +0100177
Vincent Driessenf476d262010-02-20 16:23:47 +0100178 # finally, ask the user for naming conventions (branch and tag prefixes)
Vincent Driessenb1033aa2010-03-23 21:10:13 +0100179 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 Driessenf476d262010-02-20 16:23:47 +0100188
189 local prefix
190
191 # Feature branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100192 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 Driessenf6228ed2010-03-23 21:19:54 +0100194 printf "Feature branches? [$default_suggestion] "
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100195 read answer
196 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
197 git config gitflow.prefix.feature "$prefix"
198 fi
Vincent Driessenf476d262010-02-20 16:23:47 +0100199
200 # Release branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100201 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 Driessenf6228ed2010-03-23 21:19:54 +0100203 printf "Release branches? [$default_suggestion] "
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100204 read answer
205 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
206 git config gitflow.prefix.release "$prefix"
207 fi
208
Vincent Driessenf476d262010-02-20 16:23:47 +0100209
210 # Hotfix branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100211 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 Driessenf6228ed2010-03-23 21:19:54 +0100213 printf "Hotfix branches? [$default_suggestion] "
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100214 read answer
215 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
216 git config gitflow.prefix.hotfix "$prefix"
217 fi
218
Vincent Driessenf476d262010-02-20 16:23:47 +0100219
220 # Support branches
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100221 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 Driessenf6228ed2010-03-23 21:19:54 +0100223 printf "Support branches? [$default_suggestion] "
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100224 read answer
225 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
226 git config gitflow.prefix.support "$prefix"
227 fi
228
Vincent Driessenf476d262010-02-20 16:23:47 +0100229
230 # Version tag prefix
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100231 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 Driessenf6228ed2010-03-23 21:19:54 +0100233 printf "Version tag prefix? [$default_suggestion] "
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100234 read answer
235 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
236 git config gitflow.prefix.versiontag "$prefix"
237 fi
238
Vincent Driessen61882062010-02-18 12:32:20 +0100239
240 # TODO: what to do with origin?
Vincent Driessen186d2b52010-01-27 23:48:39 +0100241}
242
Vincent Driessenb866b012010-01-28 01:01:53 +0100243cmd_help() {
244 usage
245 exit 0
246}