Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 247 lines (211 sloc) 8.232 kB
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
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() {
a2e4116 @nvie Document all available flags in all of the subcommand synopsis texts.
authored
16 echo "usage: git flow init [-f]"
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
17 }
18
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
19 parse_args() {
20 # parse options
21 FLAGS "$@" || exit $?
22 eval set -- "${FLAGS_ARGV}"
23 }
24
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
25 # Default entry when no SUBACTION is given
26 cmd_default() {
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
27 DEFINE_boolean force false 'force setting of gitflow branches, even if already configured' f
28 parse_args "$@"
29
283b0f7 @nvie Fixed a problem with redirection of stdout/stderr. The specifier '2>&1'
authored
30 if ! git rev-parse --git-dir >/dev/null 2>&1; then
0161de5 @nvie Added implementation of git-flow-init that asks the user interactivel…
authored
31 git init
32 else
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
33 # assure that we are not working in a repo with local changes
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
34 git_repo_is_headless || require_clean_working_tree
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
35 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
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
42 fi
43
0161de5 @nvie Added implementation of git-flow-init that asks the user interactivel…
authored
44 local branch_count
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
45 local answer
0161de5 @nvie Added implementation of git-flow-init that asks the user interactivel…
authored
46
47 # add a master branch if no such branch exists yet
48 local master_branch
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
49 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
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
60 branch_count=$(git_local_branches | wc -l)
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
61 if [ "$branch_count" -eq 0 ]; then
62 echo "No branches exist yet. Base branches must be created now."
63 should_check_existence=NO
b1033aa @nvie gitflow-init honors global gitflow configuration that may exist before a
authored
64 default_suggestion=$(git config --get gitflow.branch.master || echo master)
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
65 else
66 echo
67 echo "Which branch should be used for bringing forth production releases?"
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
68 git_local_branches | sed 's/^.*$/ - &/g'
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
69
70 should_check_existence=YES
71 default_suggestion=
b1033aa @nvie gitflow-init honors global gitflow configuration that may exist before a
authored
72 for guess in $(git config --get gitflow.branch.master) \
73 'production' 'main' 'master'; do
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
74 if git_local_branch_exists "$guess"; then
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
75 default_suggestion="$guess"
76 break
77 fi
78 done
79 fi
80
81 echo "Branch name for production releases: [$default_suggestion] \c"
82 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
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
87 git_local_branch_exists "$master_branch" || \
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
88 die "Local branch '$master_branch' does not exist."
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
89 fi
6188206 @nvie Always set the gitflow.branch.master and gitflow.branch.develop prope…
authored
90
91 # store the name of the master branch
92 git config gitflow.branch.master "$master_branch"
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
93 fi
94
0161de5 @nvie Added implementation of git-flow-init that asks the user interactivel…
authored
95 # add a develop branch if no such branch exists yet
96 local develop_branch
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
97 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
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
104 branch_count=$(git_local_branches | grep -v "^${master_branch}\$" | wc -l)
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
105 if [ "$branch_count" -eq 0 ]; then
106 should_check_existence=NO
b1033aa @nvie gitflow-init honors global gitflow configuration that may exist before a
authored
107 default_suggestion=$(git config --get gitflow.branch.develop || echo develop)
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
108 else
109 echo
110 echo "Which branch should be used for integration of the \"next release\"?"
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
111 git_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
112
113 should_check_existence=YES
114 default_suggestion=
b1033aa @nvie gitflow-init honors global gitflow configuration that may exist before a
authored
115 for guess in $(git config --get gitflow.branch.develop) \
116 'develop' 'int' 'integration' 'master'; do
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
117 if git_local_branch_exists "$guess"; then
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
118 default_suggestion="$guess"
119 break
120 fi
121 done
122 fi
123
124 echo "Branch name for \"next release\" development: [$default_suggestion] \c"
125 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
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
134 git_local_branch_exists "$develop_branch" || \
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
135 die "Local branch '$develop_branch' does not exist."
0161de5 @nvie Added implementation of git-flow-init that asks the user interactivel…
authored
136 fi
6188206 @nvie Always set the gitflow.branch.master and gitflow.branch.develop prope…
authored
137
138 # store the name of the develop branch
139 git config gitflow.branch.develop "$develop_branch"
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
140 fi
141
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
142 # 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.
3227d80 @nvie Only switch to develop branch if it's newly created.
authored
146 local created_gitflow_branch=0
0161de5 @nvie Added implementation of git-flow-init that asks the user interactivel…
authored
147 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
148 git symbolic-ref HEAD "refs/heads/$master_branch"
6188206 @nvie Always set the gitflow.branch.master and gitflow.branch.develop prope…
authored
149 git commit --allow-empty --quiet -m "Initial commit"
3227d80 @nvie Only switch to develop branch if it's newly created.
authored
150 created_gitflow_branch=1
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
151 fi
152
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
153 # 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
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
158
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
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)
7832d6e @nvie Better naming of common functions categorizing them into common,
authored
165 if ! git_local_branch_exists "$develop_branch"; then
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
166 git branch "$develop_branch" "$master_branch"
3227d80 @nvie Only switch to develop branch if it's newly created.
authored
167 created_gitflow_branch=1
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
168 fi
169
131c298 @nvie Provided a better way of checking whether master/develop branches need
authored
170 # assert the gitflow repo has been correctly initialized
171 gitflow_is_initialized
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
172
3227d80 @nvie Only switch to develop branch if it's newly created.
authored
173 # switch to develop branch if its newly created
174 if [ $created_gitflow_branch -eq 1 ]; then
175 git checkout -q "$develop_branch"
176 fi
6188206 @nvie Always set the gitflow.branch.master and gitflow.branch.develop prope…
authored
177
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
178 # finally, ask the user for naming conventions (branch and tag prefixes)
b1033aa @nvie gitflow-init honors global gitflow configuration that may exist before a
authored
179 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
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
188
189 local prefix
190
191 # Feature branches
1d8bb0d @nvie Add function gitflow_has_prefixes_configured(), to check whether a re…
authored
192 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/)
194 echo "Feature branches? [$default_suggestion] \c"
195 read answer
196 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
197 git config gitflow.prefix.feature "$prefix"
198 fi
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
199
200 # Release branches
1d8bb0d @nvie Add function gitflow_has_prefixes_configured(), to check whether a re…
authored
201 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/)
203 echo "Release branches? [$default_suggestion] \c"
204 read answer
205 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
206 git config gitflow.prefix.release "$prefix"
207 fi
208
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
209
210 # Hotfix branches
1d8bb0d @nvie Add function gitflow_has_prefixes_configured(), to check whether a re…
authored
211 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/)
213 echo "Hotfix branches? [$default_suggestion] \c"
214 read answer
215 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
216 git config gitflow.prefix.hotfix "$prefix"
217 fi
218
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
219
220 # Support branches
1d8bb0d @nvie Add function gitflow_has_prefixes_configured(), to check whether a re…
authored
221 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/)
223 echo "Support branches? [$default_suggestion] \c"
224 read answer
225 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
226 git config gitflow.prefix.support "$prefix"
227 fi
228
f476d26 @nvie While we're user input in git flow init, ask the user for all prefix
authored
229
230 # Version tag prefix
1d8bb0d @nvie Add function gitflow_has_prefixes_configured(), to check whether a re…
authored
231 if ! git config --get gitflow.prefix.versiontag >/dev/null 2>&1 || flag force; then
232 default_suggestion=$(git config --get gitflow.prefix.versiontag || echo "")
233 echo "Version tag prefix? [$default_suggestion] \c"
234 read answer
235 [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
236 git config gitflow.prefix.versiontag "$prefix"
237 fi
238
6188206 @nvie Always set the gitflow.branch.master and gitflow.branch.develop prope…
authored
239
240 # TODO: what to do with origin?
186d2b5 @nvie Refactored the subcommand invocation logic to form a more hierarchica…
authored
241 }
242
b866b01 @nvie Give all subcommands an optional setup() function that will be called…
authored
243 cmd_help() {
244 usage
245 exit 0
246 }
Something went wrong with that request. Please try again.