Permalink
Browse files

Provided a better way of checking whether master/develop branches need

initialization and a more user-friendly (and more comprehensable) way of
asking the user which branches are the master/develop branch.
  • Loading branch information...
1 parent b25ab83 commit 131c2988c61a929fe49d432f1bed9f67c3915bd2 @nvie committed Feb 20, 2010
Showing with 118 additions and 48 deletions.
  1. +118 −48 git-flow-init
View
@@ -16,87 +16,157 @@ usage() {
echo "usage: git flow init"
}
+parse_args() {
+ # parse options
+ FLAGS "$@" || exit $?
+ eval set -- "${FLAGS_ARGV}"
+}
+
# Default entry when no SUBACTION is given
cmd_default() {
+ DEFINE_boolean force false 'force setting of gitflow branches, even if already configured' f
+ parse_args "$@"
+
if ! git rev-parse --git-dir >/dev/null 2>&1; then
git init
else
- echo "Will try to incorporate git-flow into your current repo."
+ # TODO: This still fails when running:
+ # git init
+ # git flow init
+
+ # assure that we are not working in a repo with local changes
+ gitflow_require_clean_working_tree
+ fi
+
+ # running git flow init on an already initialized repo is fine
+ if gitflow_is_initialized && ! flag force; then
+ warn "Already initialized for gitflow."
+ warn "To force reinitialization, use: git flow init -f"
+ exit 0
fi
local branch_count
# add a master branch if no such branch exists yet
- # TODO: Distinguish two cases:
- # 1. NO BRANCHES EXIST AT ALL! (fresh repo):
- # allow a name for the branch-to-be-created
- # 2. THERE EXIST SOME BRANCHES
- # master must be based on (or *be*) one of those!
local master_branch
- master_branch=$(git config --get gitflow.branch.master)
- if [ "$master_branch" = "" ]; then
- # first, ask how to create the master branch
- echo
- echo "Which branch should be used for bringing forth production releases?"
- branch_count=$(gitflow_all_branches | wc -l)
- if [ "$branch_count" -gt 0 ]; then
- gitflow_all_branches | sed 's/^.*$/ - &/g'
- echo "You may use an existing branch name, or specify a new one."
+ if gitflow_has_master_configured && ! flag force; then
+ master_branch=$(git config --get gitflow.branch.master)
+ else
+ # Two cases are distinguished:
+ # 1. A fresh git repo (without any branches)
+ # We will create a new master/develop branch for the user
+ # 2. Some branches do already exist
+ # We will disallow creation of new master/develop branches and
+ # rather allow to use existing branches for git-flow.
+ local default_suggestion
+ local should_check_existence
+ branch_count=$(gitflow_local_branches | wc -l)
+ if [ "$branch_count" -eq 0 ]; then
+ echo "No branches exist yet. Base branches must be created now."
+ should_check_existence=NO
+ default_suggestion=master
+ else
+ echo
+ echo "Which branch should be used for bringing forth production releases?"
+ gitflow_local_branches | sed 's/^.*$/ - &/g'
+
+ should_check_existence=YES
+ default_suggestion=
+ for guess in 'production' 'main' 'master'; do
+ if gitflow_local_branch_exists "$guess"; then
+ default_suggestion="$guess"
+ break
+ fi
+ done
+ fi
+
+ echo "Branch name for production releases: [$default_suggestion] \c"
+ read answer
+ master_branch=${answer:-$default_suggestion}
+
+ # check existence in case of an already existing repo
+ if [ "$should_check_existence" = "YES" ]; then
+ gitflow_local_branch_exists "$master_branch" || \
+ die "Local branch '$master_branch' does not exist."
fi
- echo "Branch name for production releases: [master] \c"
- read master_branch
- master_branch=${master_branch:-master}
# store the name of the master branch
git config gitflow.branch.master "$master_branch"
fi
# add a develop branch if no such branch exists yet
local develop_branch
- develop_branch=$(git config --get gitflow.branch.develop)
- if [ "$develop_branch" = "" ]; then
- # next, ask how to create the develop branch
- echo
- echo "Which branch should be used for developing the \"next release\"?"
- branch_count=$(gitflow_all_branches | grep -v "^${master_branch}\$" | wc -l)
- if [ "$branch_count" -gt 0 ]; then
- gitflow_all_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
- echo "You may use an existing branch name, or specify a new one."
+ if gitflow_has_develop_configured && ! flag force; then
+ develop_branch=$(git config --get gitflow.branch.develop)
+ else
+ # Again, the same two cases as with the master selection are
+ # considered (fresh repo or repo that contains branches)
+ local default_suggestion
+ local should_check_existence
+ branch_count=$(gitflow_local_branches | grep -v "^${master_branch}\$" | wc -l)
+ if [ "$branch_count" -eq 0 ]; then
+ should_check_existence=NO
+ default_suggestion=develop
+ else
+ echo
+ echo "Which branch should be used for integration of the \"next release\"?"
+ gitflow_local_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
+
+ should_check_existence=YES
+ default_suggestion=
+ for guess in 'develop' 'int' 'integration' 'master'; do
+ if gitflow_local_branch_exists "$guess"; then
+ default_suggestion="$guess"
+ break
+ fi
+ done
+ fi
+
+ echo "Branch name for \"next release\" development: [$default_suggestion] \c"
+ read answer
+ develop_branch=${answer:-$default_suggestion}
+
+ if [ "$master_branch" = "$develop_branch" ]; then
+ die "Production and integration branches should differ."
+ fi
+
+ # check existence in case of an already existing repo
+ if [ "$should_check_existence" = "YES" ]; then
+ gitflow_local_branch_exists "$develop_branch" || \
+ die "Local branch '$develop_branch' does not exist."
fi
- echo "Branch name for \"next release\" development: [develop] \c"
- read develop_branch
- develop_branch=${develop_branch:-develop}
# store the name of the develop branch
git config gitflow.branch.develop "$develop_branch"
fi
- # create a HEAD now, if it does not exist yet (in a just init'ed repo)
+ # Creation of HEAD
+ # ----------------
+ # We create a HEAD now, if it does not exist yet (in a fresh repo). We need
+ # it to be able to create new branches.
if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
git symbolic-ref HEAD "refs/heads/$master_branch"
git commit --allow-empty --quiet -m "Initial commit"
fi
- # if the selected master branch exists, it's okay, else create it (base on
- # the currently checked out branch)
- if ! gitflow_branch_exists "$master_branch"; then
- git branch "$master_branch"
- fi
+ # Creation of master
+ # ------------------
+ # At this point, there always is a master branch: either it existed already
+ # (and was picked interactively as the production branch) or it has just
+ # been created in a fresh repo
- if ! gitflow_branch_exists "$develop_branch"; then
- git branch "$develop_branch" "$master_branch" # base it on the master branch!
- else
- # TODO: this test should be moved up, in the selection of the develop
- # branch! Branches that do not have a merge base with master shouldn't
- # be allowed to pick in the first place!
- gitflow_test_branches_equal "$develop_branch" "$master_branch"
- if [ $? -eq 4 ]; then
- warn "fatal: $develop_branch and $master_branch have no common ancestors."
- die "fatal: $develop_branch cannot be used for development."
- fi
+ # Creation of develop
+ # -------------------
+ # The develop branch possibly does not exist yet. This is the case when,
+ # in a git init'ed repo with one or more commits, master was picked as the
+ # default production branch and develop was "created". We should create
+ # the develop branch now in that case (we base it on master, of course)
+ if ! gitflow_local_branch_exists "$develop_branch"; then
+ git branch "$develop_branch" "$master_branch"
fi
- gitflow_require_clean_working_tree
+ # assert the gitflow repo has been correctly initialized
+ gitflow_is_initialized
# checkout the develop branch to start working
git checkout -q "$develop_branch"

0 comments on commit 131c298

Please sign in to comment.