blob: e5ffc748fc9c8e2fea41fe0dccf1fed1aad582f2 [file] [log] [blame]
Vincent Driessenc3607ac2010-02-05 19:53:45 +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:
Vincent Driessenddb350b2010-07-09 09:42:09 +02006# http://nvie.com/git-model
Vincent Driessenc3607ac2010-02-05 19:53:45 +01007#
8# Feel free to contribute to this project at:
9# http://github.com/nvie/gitflow
10#
Vincent Driessend72acba2010-04-04 16:10:17 +020011# Copyright 2010 Vincent Driessen. All rights reserved.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are met:
15#
16# 1. Redistributions of source code must retain the above copyright notice,
17# this list of conditions and the following disclaimer.
18#
19# 2. Redistributions in binary form must reproduce the above copyright
20# notice, this list of conditions and the following disclaimer in the
21# documentation and/or other materials provided with the distribution.
22#
23# THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26# EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
30# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34# The views and conclusions contained in the software and documentation are
35# those of the authors and should not be interpreted as representing official
36# policies, either expressed or implied, of Vincent Driessen.
Vincent Driessenc3607ac2010-02-05 19:53:45 +010037#
38
Vincent Driessen7832d6e2010-02-21 21:31:03 +010039#
40# Common functionality
41#
42
Vincent Driessenc3607ac2010-02-05 19:53:45 +010043# shell output
44warn() { echo "$@" >&2; }
45die() { warn "$@"; exit 1; }
46
Vincent Driessenf50df992010-10-08 08:39:25 +020047# argument processing
48last_arg() {
49 if [ $# -ne 0 ]; then
50 shift $(expr $# - 1)
51 echo "$1"
52 fi
53}
54
Vincent Driessenc3607ac2010-02-05 19:53:45 +010055# set logic
56has() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010057 local item=$1; shift
Vincent Driessenc3607ac2010-02-05 19:53:45 +010058 echo " $@ " | grep -q " $item "
59}
60
61# basic math
62min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
63max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }
64
65# basic string matching
66startswith() { [ "$1" != "${1#$2}" ]; }
Vincent Driessene0d8af32010-02-22 12:21:36 +010067endswith() { [ "$1" != "${1%$2}" ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010068
69# convenience functions for checking shFlags flags
Vincent Driessenf46e2902010-02-15 23:01:52 +010070flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
71noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010072
73#
74# Git specific common functionality
75#
76
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010077git_local_branches() { git branch --no-color | sed 's/^[* ] //'; }
78git_remote_branches() { git branch -r --no-color | sed 's/^[* ] //'; }
79git_all_branches() { ( git branch --no-color; git branch -r --no-color) | sed 's/^[* ] //'; }
Vincent Driessen7832d6e2010-02-21 21:31:03 +010080git_all_tags() { git tag; }
81
Vincent Driessen55c15532010-02-22 07:28:27 +010082git_current_branch() {
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010083 git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
Vincent Driessen55c15532010-02-22 07:28:27 +010084}
85
86git_is_clean_working_tree() {
87 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
88 return 1
89 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
90 return 2
91 else
92 return 0
93 fi
94}
95
96git_repo_is_headless() {
97 ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1
98}
99
100git_local_branch_exists() {
101 has $1 $(git_local_branches)
102}
103
104git_branch_exists() {
105 has $1 $(git_all_branches)
106}
107
108git_tag_exists() {
109 has $1 $(git_all_tags)
110}
111
112#
113# git_compare_branches()
114#
115# Tests whether branches and their "origin" counterparts have diverged and need
116# merging first. It returns error codes to provide more detail, like so:
117#
118# 0 Branch heads point to the same commit
119# 1 First given branch needs fast-forwarding
120# 2 Second given branch needs fast-forwarding
121# 3 Branch needs a real merge
122# 4 There is no merge base, i.e. the branches have no common ancestors
123#
124git_compare_branches() {
125 local commit1=$(git rev-parse "$1")
126 local commit2=$(git rev-parse "$2")
127 if [ "$commit1" != "$commit2" ]; then
128 local base=$(git merge-base "$commit1" "$commit2")
129 if [ $? -ne 0 ]; then
130 return 4
131 elif [ "$commit1" = "$base" ]; then
132 return 1
133 elif [ "$commit2" = "$base" ]; then
134 return 2
135 else
136 return 3
137 fi
138 else
139 return 0
140 fi
141}
142
143#
144# git_is_branch_merged_into()
145#
146# Checks whether branch $1 is succesfully merged into $2
147#
148git_is_branch_merged_into() {
149 local subject=$1
150 local base=$2
Adam Gibbinscf3da5a2010-08-22 19:13:34 +0100151 local all_merges=$(git branch --no-color --contains $subject | sed 's/^[* ] //')
Vincent Driessen55c15532010-02-22 07:28:27 +0100152 has $base $all_merges
153}
154
155#
156# gitflow specific common functionality
157#
158
Vincent Driessenb25ab832010-02-20 11:21:23 +0100159# check if this repo has been inited for gitflow
160gitflow_has_master_configured() {
161 local master=$(git config --get gitflow.branch.master)
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100162 [ "$master" != "" ] && git_local_branch_exists "$master"
Vincent Driessenb25ab832010-02-20 11:21:23 +0100163}
164
165gitflow_has_develop_configured() {
166 local develop=$(git config --get gitflow.branch.develop)
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100167 [ "$develop" != "" ] && git_local_branch_exists "$develop"
Vincent Driessenb25ab832010-02-20 11:21:23 +0100168}
169
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100170gitflow_has_prefixes_configured() {
171 git config --get gitflow.prefix.feature >/dev/null 2>&1 && \
172 git config --get gitflow.prefix.release >/dev/null 2>&1 && \
173 git config --get gitflow.prefix.hotfix >/dev/null 2>&1 && \
174 git config --get gitflow.prefix.support >/dev/null 2>&1 && \
175 git config --get gitflow.prefix.versiontag >/dev/null 2>&1
176}
177
Vincent Driessenb25ab832010-02-20 11:21:23 +0100178gitflow_is_initialized() {
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100179 gitflow_has_master_configured && \
180 gitflow_has_develop_configured && \
181 [ "$(git config --get gitflow.branch.master)" != \
182 "$(git config --get gitflow.branch.develop)" ] && \
183 gitflow_has_prefixes_configured
Vincent Driessenb25ab832010-02-20 11:21:23 +0100184}
185
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100186# loading settings that can be overridden using git config
187gitflow_load_settings() {
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100188 export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1)
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100189 export MASTER_BRANCH=$(git config --get gitflow.branch.master)
190 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100191 export ORIGIN=$(git config --get gitflow.origin || echo origin)
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100192}
193
Vincent Driessend0991262010-02-06 21:19:07 +0100194#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100195# gitflow_resolve_nameprefix
Vincent Driessend0991262010-02-06 21:19:07 +0100196#
197# Inputs:
198# $1 = name prefix to resolve
199# $2 = branch prefix to use
200#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100201# Searches branch names from git_local_branches() to look for a unique
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100202# branch name whose name starts with the given name prefix.
Vincent Driessend0991262010-02-06 21:19:07 +0100203#
204# There are multiple exit codes possible:
205# 0: The unambiguous full name of the branch is written to stdout
206# (success)
207# 1: No match is found.
208# 2: Multiple matches found. These matches are written to stderr
209#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100210gitflow_resolve_nameprefix() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100211 local name=$1
212 local prefix=$2
213 local matches
214 local num_matches
Vincent Driessend0991262010-02-06 21:19:07 +0100215
216 # first, check if there is a perfect match
Vincent Driessen5b05ad72010-05-27 11:51:50 -0700217 if git_local_branch_exists "$prefix$name"; then
Vincent Driessend0991262010-02-06 21:19:07 +0100218 echo "$name"
219 return 0
220 fi
221
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100222 matches=$(echo "$(git_local_branches)" | grep "^$prefix$name")
Vincent Driessend0991262010-02-06 21:19:07 +0100223 num_matches=$(echo "$matches" | wc -l)
224 if [ -z "$matches" ]; then
225 # no prefix match, so take it literally
226 warn "No branch matches prefix '$name'"
227 return 1
228 else
229 if [ $num_matches -eq 1 ]; then
230 echo "${matches#$prefix}"
231 return 0
232 else
233 # multiple matches, cannot decide
234 warn "Multiple branches match prefix '$name':"
235 for match in $matches; do
236 warn "- $match"
237 done
238 return 2
239 fi
240 fi
241}
242
Vincent Driessen55c15532010-02-22 07:28:27 +0100243#
244# Assertions for use in git-flow subcommands
245#
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100246
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100247require_git_repo() {
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100248 if ! git rev-parse --git-dir >/dev/null 2>&1; then
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100249 die "fatal: Not a git repository"
250 fi
251}
252
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100253require_gitflow_initialized() {
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100254 if ! gitflow_is_initialized; then
255 die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first."
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100256 fi
257}
258
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100259require_clean_working_tree() {
260 git_is_clean_working_tree
Vincent Driessenf46e2902010-02-15 23:01:52 +0100261 local result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100262 if [ $result -eq 1 ]; then
263 die "fatal: Working tree contains unstaged changes. Aborting."
264 fi
265 if [ $result -eq 2 ]; then
266 die "fatal: Index contains uncommited changes. Aborting."
267 fi
268}
269
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100270require_local_branch() {
Vincent Driessen6ee62232010-02-22 07:45:24 +0100271 if ! git_local_branch_exists $1; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100272 die "fatal: Local branch '$1' does not exist and is required."
273 fi
274}
275
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100276require_remote_branch() {
277 if ! has $1 $(git_remote_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100278 die "Remote branch '$1' does not exist and is required."
279 fi
280}
281
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100282require_branch() {
283 if ! has $1 $(git_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100284 die "Branch '$1' does not exist and is required."
285 fi
286}
287
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100288require_branch_absent() {
289 if has $1 $(git_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100290 die "Branch '$1' already exists. Pick another name."
291 fi
292}
293
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100294require_tag_absent() {
295 if has $1 $(git_all_tags); then
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100296 die "Tag '$1' already exists. Pick another name."
297 fi
298}
299
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100300require_branches_equal() {
301 require_local_branch "$1"
302 require_remote_branch "$2"
303 git_compare_branches "$1" "$2"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100304 local status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100305 if [ $status -gt 0 ]; then
306 warn "Branches '$1' and '$2' have diverged."
307 if [ $status -eq 1 ]; then
308 die "And branch '$1' may be fast-forwarded."
309 elif [ $status -eq 2 ]; then
310 # Warn here, since there is no harm in being ahead
311 warn "And local branch '$1' is ahead of '$2'."
312 else
313 die "Branches need merging first."
314 fi
315 fi
316}