blob: dcb58bdd3cb7892f6b660ba008f64c8e76850500 [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
47# set logic
48has() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010049 local item=$1; shift
Vincent Driessenc3607ac2010-02-05 19:53:45 +010050 echo " $@ " | grep -q " $item "
51}
52
53# basic math
54min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
55max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }
56
57# basic string matching
58startswith() { [ "$1" != "${1#$2}" ]; }
Vincent Driessene0d8af32010-02-22 12:21:36 +010059endswith() { [ "$1" != "${1%$2}" ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010060
61# convenience functions for checking shFlags flags
Vincent Driessenf46e2902010-02-15 23:01:52 +010062flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
63noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010064
65#
66# Git specific common functionality
67#
68
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010069git_local_branches() { git branch --no-color | sed 's/^[* ] //'; }
70git_remote_branches() { git branch -r --no-color | sed 's/^[* ] //'; }
71git_all_branches() { ( git branch --no-color; git branch -r --no-color) | sed 's/^[* ] //'; }
Vincent Driessen7832d6e2010-02-21 21:31:03 +010072git_all_tags() { git tag; }
73
Vincent Driessen55c15532010-02-22 07:28:27 +010074git_current_branch() {
Adam Gibbinscf3da5a2010-08-22 19:13:34 +010075 git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
Vincent Driessen55c15532010-02-22 07:28:27 +010076}
77
78git_is_clean_working_tree() {
79 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
80 return 1
81 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
82 return 2
83 else
84 return 0
85 fi
86}
87
88git_repo_is_headless() {
89 ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1
90}
91
92git_local_branch_exists() {
93 has $1 $(git_local_branches)
94}
95
96git_branch_exists() {
97 has $1 $(git_all_branches)
98}
99
100git_tag_exists() {
101 has $1 $(git_all_tags)
102}
103
104#
105# git_compare_branches()
106#
107# Tests whether branches and their "origin" counterparts have diverged and need
108# merging first. It returns error codes to provide more detail, like so:
109#
110# 0 Branch heads point to the same commit
111# 1 First given branch needs fast-forwarding
112# 2 Second given branch needs fast-forwarding
113# 3 Branch needs a real merge
114# 4 There is no merge base, i.e. the branches have no common ancestors
115#
116git_compare_branches() {
117 local commit1=$(git rev-parse "$1")
118 local commit2=$(git rev-parse "$2")
119 if [ "$commit1" != "$commit2" ]; then
120 local base=$(git merge-base "$commit1" "$commit2")
121 if [ $? -ne 0 ]; then
122 return 4
123 elif [ "$commit1" = "$base" ]; then
124 return 1
125 elif [ "$commit2" = "$base" ]; then
126 return 2
127 else
128 return 3
129 fi
130 else
131 return 0
132 fi
133}
134
135#
136# git_is_branch_merged_into()
137#
138# Checks whether branch $1 is succesfully merged into $2
139#
140git_is_branch_merged_into() {
141 local subject=$1
142 local base=$2
Adam Gibbinscf3da5a2010-08-22 19:13:34 +0100143 local all_merges=$(git branch --no-color --contains $subject | sed 's/^[* ] //')
Vincent Driessen55c15532010-02-22 07:28:27 +0100144 has $base $all_merges
145}
146
147#
148# gitflow specific common functionality
149#
150
Vincent Driessenb25ab832010-02-20 11:21:23 +0100151# check if this repo has been inited for gitflow
152gitflow_has_master_configured() {
153 local master=$(git config --get gitflow.branch.master)
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100154 [ "$master" != "" ] && git_local_branch_exists "$master"
Vincent Driessenb25ab832010-02-20 11:21:23 +0100155}
156
157gitflow_has_develop_configured() {
158 local develop=$(git config --get gitflow.branch.develop)
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100159 [ "$develop" != "" ] && git_local_branch_exists "$develop"
Vincent Driessenb25ab832010-02-20 11:21:23 +0100160}
161
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100162gitflow_has_prefixes_configured() {
163 git config --get gitflow.prefix.feature >/dev/null 2>&1 && \
164 git config --get gitflow.prefix.release >/dev/null 2>&1 && \
165 git config --get gitflow.prefix.hotfix >/dev/null 2>&1 && \
166 git config --get gitflow.prefix.support >/dev/null 2>&1 && \
167 git config --get gitflow.prefix.versiontag >/dev/null 2>&1
168}
169
Vincent Driessenb25ab832010-02-20 11:21:23 +0100170gitflow_is_initialized() {
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100171 gitflow_has_master_configured && \
172 gitflow_has_develop_configured && \
173 [ "$(git config --get gitflow.branch.master)" != \
174 "$(git config --get gitflow.branch.develop)" ] && \
175 gitflow_has_prefixes_configured
Vincent Driessenb25ab832010-02-20 11:21:23 +0100176}
177
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100178# loading settings that can be overridden using git config
179gitflow_load_settings() {
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100180 export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1)
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100181 export MASTER_BRANCH=$(git config --get gitflow.branch.master)
182 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100183 export ORIGIN=$(git config --get gitflow.origin || echo origin)
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100184}
185
Vincent Driessend0991262010-02-06 21:19:07 +0100186#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100187# gitflow_resolve_nameprefix
Vincent Driessend0991262010-02-06 21:19:07 +0100188#
189# Inputs:
190# $1 = name prefix to resolve
191# $2 = branch prefix to use
192#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100193# Searches branch names from git_local_branches() to look for a unique
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100194# branch name whose name starts with the given name prefix.
Vincent Driessend0991262010-02-06 21:19:07 +0100195#
196# There are multiple exit codes possible:
197# 0: The unambiguous full name of the branch is written to stdout
198# (success)
199# 1: No match is found.
200# 2: Multiple matches found. These matches are written to stderr
201#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100202gitflow_resolve_nameprefix() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100203 local name=$1
204 local prefix=$2
205 local matches
206 local num_matches
Vincent Driessend0991262010-02-06 21:19:07 +0100207
208 # first, check if there is a perfect match
Vincent Driessen5b05ad72010-05-27 11:51:50 -0700209 if git_local_branch_exists "$prefix$name"; then
Vincent Driessend0991262010-02-06 21:19:07 +0100210 echo "$name"
211 return 0
212 fi
213
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100214 matches=$(echo "$(git_local_branches)" | grep "^$prefix$name")
Vincent Driessend0991262010-02-06 21:19:07 +0100215 num_matches=$(echo "$matches" | wc -l)
216 if [ -z "$matches" ]; then
217 # no prefix match, so take it literally
218 warn "No branch matches prefix '$name'"
219 return 1
220 else
221 if [ $num_matches -eq 1 ]; then
222 echo "${matches#$prefix}"
223 return 0
224 else
225 # multiple matches, cannot decide
226 warn "Multiple branches match prefix '$name':"
227 for match in $matches; do
228 warn "- $match"
229 done
230 return 2
231 fi
232 fi
233}
234
Vincent Driessen55c15532010-02-22 07:28:27 +0100235#
236# Assertions for use in git-flow subcommands
237#
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100238
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100239require_git_repo() {
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100240 if ! git rev-parse --git-dir >/dev/null 2>&1; then
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100241 die "fatal: Not a git repository"
242 fi
243}
244
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100245require_gitflow_initialized() {
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100246 if ! gitflow_is_initialized; then
247 die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first."
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100248 fi
249}
250
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100251require_clean_working_tree() {
252 git_is_clean_working_tree
Vincent Driessenf46e2902010-02-15 23:01:52 +0100253 local result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100254 if [ $result -eq 1 ]; then
255 die "fatal: Working tree contains unstaged changes. Aborting."
256 fi
257 if [ $result -eq 2 ]; then
258 die "fatal: Index contains uncommited changes. Aborting."
259 fi
260}
261
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100262require_local_branch() {
Vincent Driessen6ee62232010-02-22 07:45:24 +0100263 if ! git_local_branch_exists $1; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100264 die "fatal: Local branch '$1' does not exist and is required."
265 fi
266}
267
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100268require_remote_branch() {
269 if ! has $1 $(git_remote_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100270 die "Remote branch '$1' does not exist and is required."
271 fi
272}
273
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100274require_branch() {
275 if ! has $1 $(git_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100276 die "Branch '$1' does not exist and is required."
277 fi
278}
279
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100280require_branch_absent() {
281 if has $1 $(git_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100282 die "Branch '$1' already exists. Pick another name."
283 fi
284}
285
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100286require_tag_absent() {
287 if has $1 $(git_all_tags); then
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100288 die "Tag '$1' already exists. Pick another name."
289 fi
290}
291
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100292require_branches_equal() {
293 require_local_branch "$1"
294 require_remote_branch "$2"
295 git_compare_branches "$1" "$2"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100296 local status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100297 if [ $status -gt 0 ]; then
298 warn "Branches '$1' and '$2' have diverged."
299 if [ $status -eq 1 ]; then
300 die "And branch '$1' may be fast-forwarded."
301 elif [ $status -eq 2 ]; then
302 # Warn here, since there is no harm in being ahead
303 warn "And local branch '$1' is ahead of '$2'."
304 else
305 die "Branches need merging first."
306 fi
307 fi
308}