blob: 4a25a8162bde53e25d144ba504acf51d7bd63cc5 [file] [log] [blame]
Benedikt Böhm00ccea62010-01-26 12:39:36 +01001#
Vincent Driessen6c2d30b2010-01-26 22:18:36 +01002# git-flow -- A collection of Git extensions to provide high-level
3# repository operations for Vincent Driessen's branching model.
Benedikt Böhm00ccea62010-01-26 12:39:36 +01004#
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
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010015PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
Vincent Driessene034e4a2010-01-29 12:10:44 +010016FLAG_FETCH=0
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010017
Benedikt Böhm00ccea62010-01-26 12:39:36 +010018usage() {
Vincent Driessenb866b012010-01-28 01:01:53 +010019 echo "usage: git flow feature [list]"
Vincent Driessen186d2b52010-01-27 23:48:39 +010020 echo " git flow feature start <name> [<base>]"
21 echo " git flow feature finish <name> [<base>]"
22 echo " git flow feature publish <name>"
23 echo " git flow feature track <name>"
24 echo " git flow feature diff <name>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010025 # TODO
26 #echo ""
27 #echo "options:"
28 #echo "--option Explanation"
29 #echo ""
30 #echo "start-only options:"
31 #echo "--option Explanation"
32 #echo ""
33 #echo "finish-only options:"
34 #echo "--rebase Rebases the feature branch on top of develop, instead of merging"
35 #echo "--squash Squashes all commits of the feature branch into a single commit"
36 #echo " on develop"
37 #echo "--push Push to the origin repo when finished"
38}
39
Vincent Driessen186d2b52010-01-27 23:48:39 +010040cmd_default() {
Vincent Driessenb866b012010-01-28 01:01:53 +010041 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010042}
43
Vincent Driessenb866b012010-01-28 01:01:53 +010044cmd_list() {
Vincent Driessen186d2b52010-01-27 23:48:39 +010045 FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
46 if [ -z "$FEATURE_BRANCHES" ]; then
47 warn "No feature branches exist."
48 exit 0
49 fi
50 echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g"
51}
52
Benedikt Böhm00ccea62010-01-26 12:39:36 +010053cmd_help() {
54 usage
55 exit 0
56}
57
Vincent Driessenb866b012010-01-28 01:01:53 +010058parse_args() {
Vincent Driessene034e4a2010-01-29 12:10:44 +010059 # TODO: When we have a nice structured way of parsing flags with getopt,
60 # implement the --fetch flag, to set FLAG_FETCH=1
Vincent Driessenb866b012010-01-28 01:01:53 +010061 NAME="$1"
62 BASE="${2:-$DEVELOP_BRANCH}"
63 if [ "$NAME" = "" ]; then
64 echo "Missing argument <name>."
65 usage
66 exit 1
67 fi
Vincent Driessenb866b012010-01-28 01:01:53 +010068 BRANCH=$PREFIX$NAME
69}
70
Benedikt Böhm00ccea62010-01-26 12:39:36 +010071cmd_start() {
72 parse_args "$@"
73
74 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +010075 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +010076 gitflow_require_branch_absent $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +010077
78 # update the local repo with remote changes, if asked
79 if [ $FLAG_FETCH -eq 1 ]; then
Benedikt Böhm4d222272010-01-26 14:46:56 +010080 git fetch -q $ORIGIN $DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +010081 fi
82
Vincent Driessene034e4a2010-01-29 12:10:44 +010083 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
84
Benedikt Böhm00ccea62010-01-26 12:39:36 +010085 # create branch
86 git checkout -b $BRANCH $BASE
87
88 echo
89 echo "Summary of actions:"
90 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
91 echo "- You are now on branch '$BRANCH'"
92 echo ""
93 echo "Now, start committing on your feature. When done, use:"
94 echo ""
95 echo " git flow finish feature $NAME"
96 echo
97}
98
99cmd_finish() {
100 parse_args "$@"
101
102 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100103 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100104 gitflow_require_branch $BRANCH
Vincent Driessene034e4a2010-01-29 12:10:44 +0100105
106 # update local repo with remote changes first, if asked
107 if [ $FLAG_FETCH -eq 1 ]; then
108 git fetch -q $ORIGIN $BRANCH
109 fi
110
Benedikt Böhm350e7152010-01-26 13:05:05 +0100111 if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
112 gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100113 fi
Benedikt Böhm4a864fb2010-01-26 12:59:27 +0100114 if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
Benedikt Böhm350e7152010-01-26 13:05:05 +0100115 gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100116 fi
117
118 # merge into BASE
119 git checkout $BASE
120 if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
121 git merge --ff $BRANCH
122 else
123 git merge --no-ff $BRANCH
124 fi
125
126 # delete branch
127 # TODO: How do we handle merge conflicts here??
Benedikt Böhm350e7152010-01-26 13:05:05 +0100128 git push $ORIGIN :refs/heads/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100129 git branch -d $BRANCH
130
131 echo
132 echo "Summary of actions:"
133 echo "- The feature branch '$BRANCH' was merged into '$BASE'"
134 #echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
135 echo "- Feature branch '$BRANCH' has been removed"
136 echo "- You are now on branch '$BASE'"
137 echo
138}
139
140cmd_publish() {
141 parse_args "$@"
142
143 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100144 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100145 gitflow_require_branch $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100146 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100147 gitflow_require_branch_absent $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100148
149 # create remote branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100150 git push $ORIGIN $BRANCH:refs/heads/$BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100151 git fetch -q $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100152
153 # configure remote tracking
Benedikt Böhm350e7152010-01-26 13:05:05 +0100154 git config branch.$BRANCH.remote $ORIGIN
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100155 git config branch.$BRANCH.merge refs/heads/$BRANCH
156 git checkout $BRANCH
157
158 echo
159 echo "Summary of actions:"
160 echo "- A new remote branch '$BRANCH' was created"
161 echo "- The local branch '$BRANCH' was configured to track the remote branch"
162 echo "- You are now on branch '$BRANCH'"
163 echo
164}
165
166cmd_track() {
167 parse_args "$@"
168
169 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100170 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100171 gitflow_require_branch_absent $BRANCH
Benedikt Böhm4d222272010-01-26 14:46:56 +0100172 git fetch -q $ORIGIN
Benedikt Böhm350e7152010-01-26 13:05:05 +0100173 gitflow_require_branch $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100174
175 # create tracking branch
Benedikt Böhm350e7152010-01-26 13:05:05 +0100176 git checkout -b $BRANCH $ORIGIN/$BRANCH
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100177
178 echo
179 echo "Summary of actions:"
180 echo "- A new remote tracking branch '$BRANCH' was created"
181 echo "- You are now on branch '$BRANCH'"
182 echo
183}
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100184
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100185cmd_diff() {
186 parse_args "$@"
187 # TODO: if this feature has been based on a non-develop branch, we really
188 # should not be comparing to $DEVELOP. How to deal with this?
Vincent Driessen278884b2010-01-28 16:29:34 +0100189 git diff $DEVELOP_BRANCH..$BRANCH
Vincent Driessenbd4f0952010-01-27 13:57:15 +0100190}