Skip to content
This repository
Newer
Older
100644 307 lines (271 sloc) 10.401 kb
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
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:
ddb350b3 »
2010-07-09 Change the URL of the original blog post.
6 # http://nvie.com/git-model
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
7 #
8 # Feel free to contribute to this project at:
9 # http://github.com/nvie/gitflow
10 #
d72acbaf »
2010-04-04 Added inline license terms to all source files.
11 # Copyright 2010 Vincent Driessen. All rights reserved.
12 #
13 # Redistribution and use in source and binary forms, with or without
14 # modification, are permitted provided that the following conditions are met:
15 #
16 # 1. Redistributions of source code must retain the above copyright notice,
17 # this list of conditions and the following disclaimer.
18 #
19 # 2. Redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution.
22 #
23 # THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR
24 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26 # EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
30 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #
34 # The views and conclusions contained in the software and documentation are
35 # those of the authors and should not be interpreted as representing official
36 # policies, either expressed or implied, of Vincent Driessen.
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
37 #
38
39 usage() {
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
40 echo "usage: git flow init [-fd]"
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
41 }
42
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
43 parse_args() {
44 # parse options
45 FLAGS "$@" || exit $?
46 eval set -- "${FLAGS_ARGV}"
47 }
48
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
49 # Default entry when no SUBACTION is given
50 cmd_default() {
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
51 DEFINE_boolean force false 'force setting of gitflow branches, even if already configured' f
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
52 DEFINE_boolean defaults false 'use default branch naming conventions' d
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
53 parse_args "$@"
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
54
283b0f70 »
2010-02-15 Fixed a problem with redirection of stdout/stderr. The specifier '2>&1'
55 if ! git rev-parse --git-dir >/dev/null 2>&1; then
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
56 git init
57 else
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
58 # assure that we are not working in a repo with local changes
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
59 git_repo_is_headless || require_clean_working_tree
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
60 fi
61
62 # running git flow init on an already initialized repo is fine
63 if gitflow_is_initialized && ! flag force; then
64 warn "Already initialized for gitflow."
65 warn "To force reinitialization, use: git flow init -f"
66 exit 0
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
67 fi
68
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
69 local branch_count
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
70 local answer
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
71
f78b6604 »
2011-02-14 Move up the notification that default branch names are being used.
72 if flag defaults; then
73 warn "Using default branch names."
74 fi
75
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
76 # add a master branch if no such branch exists yet
77 local master_branch
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
78 if gitflow_has_master_configured && ! flag force; then
79 master_branch=$(git config --get gitflow.branch.master)
80 else
81 # Two cases are distinguished:
82 # 1. A fresh git repo (without any branches)
83 # We will create a new master/develop branch for the user
84 # 2. Some branches do already exist
85 # We will disallow creation of new master/develop branches and
86 # rather allow to use existing branches for git-flow.
87 local default_suggestion
88 local should_check_existence
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
89 branch_count=$(git_local_branches | wc -l)
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
90 if [ "$branch_count" -eq 0 ]; then
91 echo "No branches exist yet. Base branches must be created now."
92 should_check_existence=NO
b1033aa3 »
2010-03-23 gitflow-init honors global gitflow configuration that may exist before a
93 default_suggestion=$(git config --get gitflow.branch.master || echo master)
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
94 else
95 echo
96 echo "Which branch should be used for bringing forth production releases?"
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
97 git_local_branches | sed 's/^.*$/ - &/g'
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
98
99 should_check_existence=YES
100 default_suggestion=
b1033aa3 »
2010-03-23 gitflow-init honors global gitflow configuration that may exist before a
101 for guess in $(git config --get gitflow.branch.master) \
102 'production' 'main' 'master'; do
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
103 if git_local_branch_exists "$guess"; then
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
104 default_suggestion="$guess"
105 break
106 fi
107 done
108 fi
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
109
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
110 printf "Branch name for production releases: [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
111 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
112 read answer
113 else
114 printf "\n"
115 fi
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
116 master_branch=${answer:-$default_suggestion}
117
118 # check existence in case of an already existing repo
119 if [ "$should_check_existence" = "YES" ]; then
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
120 git_local_branch_exists "$master_branch" || \
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
121 die "Local branch '$master_branch' does not exist."
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
122 fi
61882067 »
2010-02-18 Always set the gitflow.branch.master and gitflow.branch.develop prope…
123
124 # store the name of the master branch
125 git config gitflow.branch.master "$master_branch"
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
126 fi
127
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
128 # add a develop branch if no such branch exists yet
129 local develop_branch
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
130 if gitflow_has_develop_configured && ! flag force; then
131 develop_branch=$(git config --get gitflow.branch.develop)
132 else
133 # Again, the same two cases as with the master selection are
134 # considered (fresh repo or repo that contains branches)
135 local default_suggestion
136 local should_check_existence
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
137 branch_count=$(git_local_branches | grep -v "^${master_branch}\$" | wc -l)
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
138 if [ "$branch_count" -eq 0 ]; then
139 should_check_existence=NO
b1033aa3 »
2010-03-23 gitflow-init honors global gitflow configuration that may exist before a
140 default_suggestion=$(git config --get gitflow.branch.develop || echo develop)
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
141 else
142 echo
143 echo "Which branch should be used for integration of the \"next release\"?"
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
144 git_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
145
146 should_check_existence=YES
147 default_suggestion=
b1033aa3 »
2010-03-23 gitflow-init honors global gitflow configuration that may exist before a
148 for guess in $(git config --get gitflow.branch.develop) \
149 'develop' 'int' 'integration' 'master'; do
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
150 if git_local_branch_exists "$guess"; then
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
151 default_suggestion="$guess"
152 break
153 fi
154 done
155 fi
156
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
157 printf "Branch name for \"next release\" development: [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
158 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
159 read answer
160 else
161 printf "\n"
162 fi
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
163 develop_branch=${answer:-$default_suggestion}
164
165 if [ "$master_branch" = "$develop_branch" ]; then
166 die "Production and integration branches should differ."
167 fi
168
169 # check existence in case of an already existing repo
170 if [ "$should_check_existence" = "YES" ]; then
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
171 git_local_branch_exists "$develop_branch" || \
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
172 die "Local branch '$develop_branch' does not exist."
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
173 fi
61882067 »
2010-02-18 Always set the gitflow.branch.master and gitflow.branch.develop prope…
174
175 # store the name of the develop branch
176 git config gitflow.branch.develop "$develop_branch"
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
177 fi
178
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
179 # Creation of HEAD
180 # ----------------
181 # We create a HEAD now, if it does not exist yet (in a fresh repo). We need
182 # it to be able to create new branches.
3227d802 »
2010-02-20 Only switch to develop branch if it's newly created.
183 local created_gitflow_branch=0
0161de58 »
2010-02-18 Added implementation of git-flow-init that asks the user interactivel…
184 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
185 git symbolic-ref HEAD "refs/heads/$master_branch"
61882067 »
2010-02-18 Always set the gitflow.branch.master and gitflow.branch.develop prope…
186 git commit --allow-empty --quiet -m "Initial commit"
3227d802 »
2010-02-20 Only switch to develop branch if it's newly created.
187 created_gitflow_branch=1
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
188 fi
189
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
190 # Creation of master
191 # ------------------
192 # At this point, there always is a master branch: either it existed already
193 # (and was picked interactively as the production branch) or it has just
194 # been created in a fresh repo
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
195
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
196 # Creation of develop
197 # -------------------
198 # The develop branch possibly does not exist yet. This is the case when,
199 # in a git init'ed repo with one or more commits, master was picked as the
200 # default production branch and develop was "created". We should create
201 # the develop branch now in that case (we base it on master, of course)
7832d6ef »
2010-02-21 Better naming of common functions categorizing them into common,
202 if ! git_local_branch_exists "$develop_branch"; then
62c339eb » emreberge
2011-11-27 develop_branch uses origin/develop_branch as start-point if one exists.
203 if git_remote_branch_exists "origin/$develop_branch"; then
204 git branch "$develop_branch" "origin/$develop_branch" >/dev/null 2>&1
205 else
206 git branch --no-track "$develop_branch" "$master_branch"
207 fi
3227d802 »
2010-02-20 Only switch to develop branch if it's newly created.
208 created_gitflow_branch=1
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
209 fi
210
131c2988 »
2010-02-20 Provided a better way of checking whether master/develop branches need
211 # assert the gitflow repo has been correctly initialized
212 gitflow_is_initialized
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
213
3227d802 »
2010-02-20 Only switch to develop branch if it's newly created.
214 # switch to develop branch if its newly created
215 if [ $created_gitflow_branch -eq 1 ]; then
216 git checkout -q "$develop_branch"
217 fi
61882067 »
2010-02-18 Always set the gitflow.branch.master and gitflow.branch.develop prope…
218
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
219 # finally, ask the user for naming conventions (branch and tag prefixes)
b1033aa3 »
2010-03-23 gitflow-init honors global gitflow configuration that may exist before a
220 if flag force || \
221 ! git config --get gitflow.prefix.feature >/dev/null 2>&1 ||
222 ! git config --get gitflow.prefix.release >/dev/null 2>&1 ||
223 ! git config --get gitflow.prefix.hotfix >/dev/null 2>&1 ||
224 ! git config --get gitflow.prefix.support >/dev/null 2>&1 ||
225 ! git config --get gitflow.prefix.versiontag >/dev/null 2>&1; then
226 echo
227 echo "How to name your supporting branch prefixes?"
228 fi
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
229
230 local prefix
231
232 # Feature branches
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
233 if ! git config --get gitflow.prefix.feature >/dev/null 2>&1 || flag force; then
234 default_suggestion=$(git config --get gitflow.prefix.feature || echo feature/)
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
235 printf "Feature branches? [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
236 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
237 read answer
238 else
239 printf "\n"
240 fi
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
241 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
242 git config gitflow.prefix.feature "$prefix"
243 fi
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
244
245 # Release branches
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
246 if ! git config --get gitflow.prefix.release >/dev/null 2>&1 || flag force; then
247 default_suggestion=$(git config --get gitflow.prefix.release || echo release/)
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
248 printf "Release branches? [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
249 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
250 read answer
251 else
252 printf "\n"
253 fi
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
254 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
255 git config gitflow.prefix.release "$prefix"
256 fi
257
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
258
259 # Hotfix branches
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
260 if ! git config --get gitflow.prefix.hotfix >/dev/null 2>&1 || flag force; then
261 default_suggestion=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
262 printf "Hotfix branches? [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
263 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
264 read answer
265 else
266 printf "\n"
267 fi
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
268 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
269 git config gitflow.prefix.hotfix "$prefix"
270 fi
271
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
272
273 # Support branches
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
274 if ! git config --get gitflow.prefix.support >/dev/null 2>&1 || flag force; then
275 default_suggestion=$(git config --get gitflow.prefix.support || echo support/)
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
276 printf "Support branches? [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
277 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
278 read answer
279 else
280 printf "\n"
281 fi
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
282 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
283 git config gitflow.prefix.support "$prefix"
284 fi
285
f476d260 »
2010-02-20 While we're user input in git flow init, ask the user for all prefix
286
287 # Version tag prefix
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
288 if ! git config --get gitflow.prefix.versiontag >/dev/null 2>&1 || flag force; then
289 default_suggestion=$(git config --get gitflow.prefix.versiontag || echo "")
f6228ed8 »
2010-03-23 Replace \c-terminated echo calls by more portable printf calls.
290 printf "Version tag prefix? [$default_suggestion] "
5f860bf3 »
2011-02-05 Rewrite Joseph Levin's solution a bit.
291 if noflag defaults; then
d2eccaa7 » josephalevin
2011-02-04 Issue: 88
292 read answer
293 else
294 printf "\n"
295 fi
1d8bb0d1 »
2010-02-20 Add function gitflow_has_prefixes_configured(), to check whether a re…
296 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
297 git config gitflow.prefix.versiontag "$prefix"
298 fi
299
61882067 »
2010-02-18 Always set the gitflow.branch.master and gitflow.branch.develop prope…
300
301 # TODO: what to do with origin?
186d2b5f »
2010-01-27 Refactored the subcommand invocation logic to form a more hierarchica…
302 }
303
b866b01b »
2010-01-28 Give all subcommands an optional setup() function that will be called…
304 cmd_help() {
305 usage
306 exit 0
307 }
Something went wrong with that request. Please try again.