blob: f443e337f81153af09b9054662215ffaa831fecd [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
49
50 # add a master branch if no such branch exists yet
51 local master_branch
Vincent Driessen131c2982010-02-20 14:30:16 +010052 if gitflow_has_master_configured && ! flag force; then
53 master_branch=$(git config --get gitflow.branch.master)
54 else
55 # Two cases are distinguished:
56 # 1. A fresh git repo (without any branches)
57 # We will create a new master/develop branch for the user
58 # 2. Some branches do already exist
59 # We will disallow creation of new master/develop branches and
60 # rather allow to use existing branches for git-flow.
61 local default_suggestion
62 local should_check_existence
63 branch_count=$(gitflow_local_branches | wc -l)
64 if [ "$branch_count" -eq 0 ]; then
65 echo "No branches exist yet. Base branches must be created now."
66 should_check_existence=NO
67 default_suggestion=master
68 else
69 echo
70 echo "Which branch should be used for bringing forth production releases?"
71 gitflow_local_branches | sed 's/^.*$/ - &/g'
72
73 should_check_existence=YES
74 default_suggestion=
75 for guess in 'production' 'main' 'master'; do
76 if gitflow_local_branch_exists "$guess"; then
77 default_suggestion="$guess"
78 break
79 fi
80 done
Vincent Driessen186d2b52010-01-27 23:48:39 +010081 fi
Vincent Driessen131c2982010-02-20 14:30:16 +010082
83 echo "Branch name for production releases: [$default_suggestion] \c"
84 read answer
85 master_branch=${answer:-$default_suggestion}
86
87 # check existence in case of an already existing repo
88 if [ "$should_check_existence" = "YES" ]; then
89 gitflow_local_branch_exists "$master_branch" || \
90 die "Local branch '$master_branch' does not exist."
91 fi
Vincent Driessen0161de52010-02-18 12:07:34 +010092
Vincent Driessen61882062010-02-18 12:32:20 +010093 # store the name of the master branch
94 git config gitflow.branch.master "$master_branch"
Vincent Driessen186d2b52010-01-27 23:48:39 +010095 fi
96
Vincent Driessen0161de52010-02-18 12:07:34 +010097 # add a develop branch if no such branch exists yet
98 local develop_branch
Vincent Driessen131c2982010-02-20 14:30:16 +010099 if gitflow_has_develop_configured && ! flag force; then
100 develop_branch=$(git config --get gitflow.branch.develop)
101 else
102 # Again, the same two cases as with the master selection are
103 # considered (fresh repo or repo that contains branches)
104 local default_suggestion
105 local should_check_existence
106 branch_count=$(gitflow_local_branches | grep -v "^${master_branch}\$" | wc -l)
107 if [ "$branch_count" -eq 0 ]; then
108 should_check_existence=NO
109 default_suggestion=develop
110 else
111 echo
112 echo "Which branch should be used for integration of the \"next release\"?"
113 gitflow_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
114
115 should_check_existence=YES
116 default_suggestion=
117 for guess in 'develop' 'int' 'integration' 'master'; do
118 if gitflow_local_branch_exists "$guess"; then
119 default_suggestion="$guess"
120 break
121 fi
122 done
Vincent Driessen0161de52010-02-18 12:07:34 +0100123 fi
Vincent Driessen131c2982010-02-20 14:30:16 +0100124
125 echo "Branch name for \"next release\" development: [$default_suggestion] \c"
126 read answer
127 develop_branch=${answer:-$default_suggestion}
128
129 if [ "$master_branch" = "$develop_branch" ]; then
130 die "Production and integration branches should differ."
131 fi
132
133 # check existence in case of an already existing repo
134 if [ "$should_check_existence" = "YES" ]; then
135 gitflow_local_branch_exists "$develop_branch" || \
136 die "Local branch '$develop_branch' does not exist."
137 fi
Vincent Driessen0161de52010-02-18 12:07:34 +0100138
Vincent Driessen61882062010-02-18 12:32:20 +0100139 # store the name of the develop branch
140 git config gitflow.branch.develop "$develop_branch"
Vincent Driessen0161de52010-02-18 12:07:34 +0100141 fi
142
Vincent Driessen131c2982010-02-20 14:30:16 +0100143 # Creation of HEAD
144 # ----------------
145 # We create a HEAD now, if it does not exist yet (in a fresh repo). We need
146 # it to be able to create new branches.
Vincent Driessen3227d802010-02-20 16:13:23 +0100147 local created_gitflow_branch=0
Vincent Driessen0161de52010-02-18 12:07:34 +0100148 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +0100149 git symbolic-ref HEAD "refs/heads/$master_branch"
Vincent Driessen61882062010-02-18 12:32:20 +0100150 git commit --allow-empty --quiet -m "Initial commit"
Vincent Driessen3227d802010-02-20 16:13:23 +0100151 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100152 fi
153
Vincent Driessen131c2982010-02-20 14:30:16 +0100154 # Creation of master
155 # ------------------
156 # At this point, there always is a master branch: either it existed already
157 # (and was picked interactively as the production branch) or it has just
158 # been created in a fresh repo
159
160 # Creation of develop
161 # -------------------
162 # The develop branch possibly does not exist yet. This is the case when,
163 # in a git init'ed repo with one or more commits, master was picked as the
164 # default production branch and develop was "created". We should create
165 # the develop branch now in that case (we base it on master, of course)
166 if ! gitflow_local_branch_exists "$develop_branch"; then
167 git branch "$develop_branch" "$master_branch"
Vincent Driessen3227d802010-02-20 16:13:23 +0100168 created_gitflow_branch=1
Vincent Driessen0161de52010-02-18 12:07:34 +0100169 fi
170
Vincent Driessen131c2982010-02-20 14:30:16 +0100171 # assert the gitflow repo has been correctly initialized
172 gitflow_is_initialized
Vincent Driessen186d2b52010-01-27 23:48:39 +0100173
Vincent Driessen3227d802010-02-20 16:13:23 +0100174 # switch to develop branch if its newly created
175 if [ $created_gitflow_branch -eq 1 ]; then
176 git checkout -q "$develop_branch"
177 fi
Vincent Driessen61882062010-02-18 12:32:20 +0100178
179 # TODO: finally, ask the user for naming convention preferences
180 # i.e. tag prefixes, prefixes for supporting branches, etc.
181
182 # TODO: what to do with origin?
Vincent Driessen186d2b52010-01-27 23:48:39 +0100183}
184
Vincent Driessenb866b012010-01-28 01:01:53 +0100185cmd_help() {
186 usage
187 exit 0
188}