blob: 31be29f91e09a0301350a29cf9cbafdc7c7313bf [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 Driessen186d2b52010-01-27 23:48:39 +010019# Default entry when no SUBACTION is given
20cmd_default() {
Vincent Driessen283b0f72010-02-15 23:23:14 +010021 if ! git rev-parse --git-dir >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +010022 git init
23 else
24 echo "Will try to incorporate git-flow into your current repo."
Vincent Driessen186d2b52010-01-27 23:48:39 +010025 fi
26
Vincent Driessen0161de52010-02-18 12:07:34 +010027 local branch_count
28
29 # add a master branch if no such branch exists yet
Vincent Driessen61882062010-02-18 12:32:20 +010030 # TODO: Distinguish two cases:
31 # 1. NO BRANCHES EXIST AT ALL! (fresh repo):
32 # allow a name for the branch-to-be-created
33 # 2. THERE EXIST SOME BRANCHES
34 # master must be based on (or *be*) one of those!
Vincent Driessen0161de52010-02-18 12:07:34 +010035 local master_branch
Vincent Driessen61882062010-02-18 12:32:20 +010036 master_branch=$(git config --get gitflow.branch.master)
37 if [ "$master_branch" = "" ]; then
Vincent Driessen0161de52010-02-18 12:07:34 +010038 # first, ask how to create the master branch
39 echo
40 echo "Which branch should be used for bringing forth production releases?"
41 branch_count=$(gitflow_all_branches | wc -l)
42 if [ "$branch_count" -gt 0 ]; then
43 gitflow_all_branches | sed 's/^.*$/ - &/g'
44 echo "You may use an existing branch name, or specify a new one."
Vincent Driessen186d2b52010-01-27 23:48:39 +010045 fi
Vincent Driessen0161de52010-02-18 12:07:34 +010046 echo "Branch name for production releases: [master] \c"
47 read master_branch
Vincent Driessen61882062010-02-18 12:32:20 +010048 master_branch=${master_branch:-master}
Vincent Driessen0161de52010-02-18 12:07:34 +010049
Vincent Driessen61882062010-02-18 12:32:20 +010050 # store the name of the master branch
51 git config gitflow.branch.master "$master_branch"
Vincent Driessen186d2b52010-01-27 23:48:39 +010052 fi
53
Vincent Driessen0161de52010-02-18 12:07:34 +010054 # add a develop branch if no such branch exists yet
55 local develop_branch
Vincent Driessen61882062010-02-18 12:32:20 +010056 develop_branch=$(git config --get gitflow.branch.develop)
57 if [ "$develop_branch" = "" ]; then
Vincent Driessen0161de52010-02-18 12:07:34 +010058 # next, ask how to create the develop branch
59 echo
60 echo "Which branch should be used for developing the \"next release\"?"
61 branch_count=$(gitflow_all_branches | grep -v "^${master_branch}\$" | wc -l)
62 if [ "$branch_count" -gt 0 ]; then
63 gitflow_all_branches | grep -v "^${master_branch}\$" | sed 's/^.*$/ - &/g'
64 echo "You may use an existing branch name, or specify a new one."
65 fi
66 echo "Branch name for \"next release\" development: [develop] \c"
67 read develop_branch
Vincent Driessen61882062010-02-18 12:32:20 +010068 develop_branch=${develop_branch:-develop}
Vincent Driessen0161de52010-02-18 12:07:34 +010069
Vincent Driessen61882062010-02-18 12:32:20 +010070 # store the name of the develop branch
71 git config gitflow.branch.develop "$develop_branch"
Vincent Driessen0161de52010-02-18 12:07:34 +010072 fi
73
Vincent Driessen61882062010-02-18 12:32:20 +010074 # create a HEAD now, if it does not exist yet (in a just init'ed repo)
Vincent Driessen0161de52010-02-18 12:07:34 +010075 if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
Vincent Driessen0161de52010-02-18 12:07:34 +010076 git symbolic-ref HEAD "refs/heads/$master_branch"
Vincent Driessen61882062010-02-18 12:32:20 +010077 git commit --allow-empty --quiet -m "Initial commit"
Vincent Driessen0161de52010-02-18 12:07:34 +010078 fi
79
80 # if the selected master branch exists, it's okay, else create it (base on
81 # the currently checked out branch)
82 if ! gitflow_branch_exists "$master_branch"; then
83 git branch "$master_branch"
84 fi
85
86 if ! gitflow_branch_exists "$develop_branch"; then
87 git branch "$develop_branch" "$master_branch" # base it on the master branch!
88 else
Vincent Driessen61882062010-02-18 12:32:20 +010089 # TODO: this test should be moved up, in the selection of the develop
90 # branch! Branches that do not have a merge base with master shouldn't
91 # be allowed to pick in the first place!
Vincent Driessen0161de52010-02-18 12:07:34 +010092 gitflow_test_branches_equal "$develop_branch" "$master_branch"
93 if [ $? -eq 4 ]; then
94 warn "fatal: $develop_branch and $master_branch have no common ancestors."
95 die "fatal: $develop_branch cannot be used for development."
96 fi
Vincent Driessen186d2b52010-01-27 23:48:39 +010097 fi
98
Vincent Driessen48386442010-01-29 10:30:40 +010099 gitflow_require_clean_working_tree
Vincent Driessen186d2b52010-01-27 23:48:39 +0100100
Vincent Driessen0161de52010-02-18 12:07:34 +0100101 # checkout the develop branch to start working
102 git checkout -q "$develop_branch"
Vincent Driessen61882062010-02-18 12:32:20 +0100103
104 # TODO: finally, ask the user for naming convention preferences
105 # i.e. tag prefixes, prefixes for supporting branches, etc.
106
107 # TODO: what to do with origin?
Vincent Driessen186d2b52010-01-27 23:48:39 +0100108}
109
Vincent Driessenb866b012010-01-28 01:01:53 +0100110cmd_help() {
111 usage
112 exit 0
113}