blob: 71d577d60d86447321003bf34bd50e0717e87372 [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 # 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 Driessen186d2b52010-01-27 23:48:39 +010046 fi
47
Vincent Driessen0161de52010-02-18 12:07:34 +010048 local branch_count
Vincent Driessenf476d262010-02-20 16:23:47 +010049 local answer
Vincent Driessen0161de52010-02-18 12:07:34 +010050
51 # add a master branch if no such branch exists yet
52 local master_branch
Vincent Driessen131c2982010-02-20 14:30:16 +010053 if gitflow_has_master_configured && ! flag force; then
54 master_branch=$(git config --get gitflow.branch.master)
55 else
56 # Two cases are distinguished:
57 # 1. A fresh git repo (without any branches)
58 # We will create a new master/develop branch for the user
59 # 2. Some branches do already exist
60 # We will disallow creation of new master/develop branches and
61 # rather allow to use existing branches for git-flow.
62 local default_suggestion
63 local should_check_existence
64 branch_count=$(gitflow_local_branches | wc -l)
65 if [ "$branch_count" -eq 0 ]; then
66 echo "No branches exist yet. Base branches must be created now."
67 should_check_existence=NO
68 default_suggestion=master
69 else
70 echo
71 echo "Which branch should be used for bringing forth production releases?"
72 gitflow_local_branches | sed 's/^.*$/ - &/g'
73
74 should_check_existence=YES
75 default_suggestion=
76 for guess in 'production' 'main' 'master'; do
77 if gitflow_local_branch_exists "$guess"; then
78 default_suggestion="$guess"
79 break
80 fi
81 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010082 fi
Vincent Driessen131c2982010-02-20 14:30:16 +010083
84 echo "Branch name for production releases: [$default_suggestion] \c"
85 read answer
86 master_branch=${answer:-$default_suggestion}
87
88 # check existence in case of an already existing repo
89 if [ "$should_check_existence" = "YES" ]; then
90 gitflow_local_branch_exists "$master_branch" || \
91 die "Local branch '$master_branch' does not exist."
92 fi
Vincent Driessen0161de52010-02-18 12:07:34 +010093
Vincent Driessen61882062010-02-18 12:32:20 +010094 # store the name of the master branch
95 git config gitflow.branch.master "$master_branch"
Vincent Driessen186d2b52010-01-27 23:48:39 +010096 fi
97
Vincent Driessen0161de52010-02-18 12:07:34 +010098 # add a develop branch if no such branch exists yet
99 local develop_branch
Vincent Driessen131c2982010-02-20 14:30:16 +0100100 if gitflow_has_develop_configured && ! flag force; then
101 develop_branch=$(git config --get gitflow.branch.develop)
102 else
103 # Again, the same two cases as with the master selection are
104 # considered (fresh repo or repo that contains branches)
105 local default_suggestion
106 local should_check_existence
107 branch_count=$(gitflow_local_branches | grep -v "^${master_branch}\$" | wc -l)
108 if [ "$branch_count" -eq 0 ]; then
109 should_check_existence=NO
110 default_suggestion=develop
111 else
112 echo
113 echo "Which branch should be used for integration of the \"next release\"?"
114 gitflow_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
115
116 should_check_existence=YES
117 default_suggestion=
118 for guess in 'develop' 'int' 'integration' 'master'; do
119 if gitflow_local_branch_exists "$guess"; then
120 default_suggestion="$guess"
121 break
122 fi
123 done
Vincent Driessen0161de52010-02-18 12:07:34 +0100124 fi
Vincent Driessen131c2982010-02-20 14:30:16 +0100125
126 echo "Branch name for \"next release\" development: [$default_suggestion] \c"
127 read answer
128 develop_branch=${answer:-$default_suggestion}
129
130 if [ "$master_branch" = "$develop_branch" ]; then
131 die "Production and integration branches should differ."
132 fi
133
134 # check existence in case of an already existing repo
135 if [ "$should_check_existence" = "YES" ]; then
136 gitflow_local_branch_exists "$develop_branch" || \
137 die "Local branch '$develop_branch' does not exist."
138 fi
Vincent Driessen0161de52010-02-18 12:07:34 +0100139
Vincent Driessen61882062010-02-18 12:32:20 +0100140 # store the name of the develop branch
141 git config gitflow.branch.develop "$develop_branch"
Vincent Driessen0161de52010-02-18 12:07:34 +0100142 fi
143
Vincent Driessen131c2982010-02-20 14:30:16 +0100144 # Creation of HEAD
145 # ----------------
146 # We create a HEAD now, if it does not exist yet (in a fresh repo). We need
147 # it to be able to create new branches.
Vincent Driessen3227d802010-02-20 16:13:23 +0100148 local created_gitflow_branch=0
Vincent Driessen0161de52010-02-18 12:07:34 +0100149 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +0100150 git symbolic-ref HEAD "refs/heads/$master_branch"
Vincent Driessen61882062010-02-18 12:32:20 +0100151 git commit --allow-empty --quiet -m "Initial commit"
Vincent Driessen3227d802010-02-20 16:13:23 +0100152 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100153 fi
154
Vincent Driessen131c2982010-02-20 14:30:16 +0100155 # Creation of master
156 # ------------------
157 # At this point, there always is a master branch: either it existed already
158 # (and was picked interactively as the production branch) or it has just
159 # been created in a fresh repo
160
161 # Creation of develop
162 # -------------------
163 # The develop branch possibly does not exist yet. This is the case when,
164 # in a git init'ed repo with one or more commits, master was picked as the
165 # default production branch and develop was "created". We should create
166 # the develop branch now in that case (we base it on master, of course)
167 if ! gitflow_local_branch_exists "$develop_branch"; then
168 git branch "$develop_branch" "$master_branch"
Vincent Driessen3227d802010-02-20 16:13:23 +0100169 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100170 fi
171
Vincent Driessen131c2982010-02-20 14:30:16 +0100172 # assert the gitflow repo has been correctly initialized
173 gitflow_is_initialized
Vincent Driessen186d2b52010-01-27 23:48:39 +0100174
Vincent Driessen3227d802010-02-20 16:13:23 +0100175 # switch to develop branch if its newly created
176 if [ $created_gitflow_branch -eq 1 ]; then
177 git checkout -q "$develop_branch"
178 fi
Vincent Driessen61882062010-02-18 12:32:20 +0100179
Vincent Driessenf476d262010-02-20 16:23:47 +0100180 # finally, ask the user for naming conventions (branch and tag prefixes)
181 echo
182 echo "How to name your supporting branch prefixes?"
183
184 local prefix
185
186 # Feature branches
187 default_suggestion=$(git config --get gitflow.prefix.feature || echo feature/)
188 echo "Feature branches? [$default_suggestion] \c"
189 read answer
190 prefix=${answer:-$default_suggestion}
191 git config gitflow.prefix.feature "$prefix"
192
193 # Release branches
194 default_suggestion=$(git config --get gitflow.prefix.release || echo release/)
195 echo "Release branches? [$default_suggestion] \c"
196 read answer
197 prefix=${answer:-$default_suggestion}
198 git config gitflow.prefix.release "$prefix"
199
200 # Hotfix branches
201 default_suggestion=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
202 echo "Hotfix branches? [$default_suggestion] \c"
203 read answer
204 prefix=${answer:-$default_suggestion}
205 git config gitflow.prefix.hotfix "$prefix"
206
207 # Support branches
208 default_suggestion=$(git config --get gitflow.prefix.support || echo support/)
209 echo "Support branches? [$default_suggestion] \c"
210 read answer
211 prefix=${answer:-$default_suggestion}
212 git config gitflow.prefix.support "$prefix"
213
214 # Version tag prefix
215 default_suggestion=$(git config --get gitflow.prefix.versiontag || echo "")
216 echo "Version tag prefix? [$default_suggestion] \c"
217 read answer
218 prefix=${answer:-$default_suggestion}
219 git config gitflow.prefix.versiontag "$prefix"
Vincent Driessen61882062010-02-18 12:32:20 +0100220
221 # TODO: what to do with origin?
Vincent Driessen186d2b52010-01-27 23:48:39 +0100222}
223
Vincent Driessenb866b012010-01-28 01:01:53 +0100224cmd_help() {
225 usage
226 exit 0
227}