blob: 0e3f1d81395c360a60f97923b02e0cd7c2460dd8 [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:
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
15# shell output
16warn() { echo "$@" >&2; }
17die() { warn "$@"; exit 1; }
18
19# set logic
20has() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010021 local item=$1; shift
Vincent Driessenc3607ac2010-02-05 19:53:45 +010022 echo " $@ " | grep -q " $item "
23}
24
25# basic math
26min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
27max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }
28
29# basic string matching
30startswith() { [ "$1" != "${1#$2}" ]; }
31endswith() { [ "$1" != "${1#$2}" ]; }
32
33# convenience functions for checking shFlags flags
Vincent Driessenf46e2902010-02-15 23:01:52 +010034flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
35noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010036
37#
38# Git specific common functionality
39#
40
41# get all available branches
Vincent Driessen21c7aa92010-02-16 20:57:35 +010042gitflow_local_branches() { git branch | sed 's/^[* ] //'; }
43gitflow_remote_branches() { git branch -r | sed 's/^[* ] //'; }
Vincent Driessen4e11dd62010-02-19 19:48:23 +010044gitflow_all_branches() { ( git branch; git branch -r) | sed 's/^[* ] //'; }
Vincent Driessen21c7aa92010-02-16 20:57:35 +010045gitflow_all_tags() { git tag; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010046
Vincent Driessend72e4ac2010-02-16 21:33:51 +010047# loading settings that can be overridden using git config
48gitflow_load_settings() {
Vincent Driessencf6e92a2010-02-19 19:44:48 +010049 export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1)
Vincent Driessend72e4ac2010-02-16 21:33:51 +010050 export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
51 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
52 export ORIGIN=$(git config --get gitflow.origin || echo origin)
Vincent Driessend72e4ac2010-02-16 21:33:51 +010053}
54
Vincent Driessend0991262010-02-06 21:19:07 +010055#
56# resolve_nameprefix
57#
58# Inputs:
59# $1 = name prefix to resolve
60# $2 = branch prefix to use
61#
Vincent Driessen21c7aa92010-02-16 20:57:35 +010062# Searches branch names from gitflow_local_branches() to look for a unique
63# branch name whose name starts with the given name prefix.
Vincent Driessend0991262010-02-06 21:19:07 +010064#
65# There are multiple exit codes possible:
66# 0: The unambiguous full name of the branch is written to stdout
67# (success)
68# 1: No match is found.
69# 2: Multiple matches found. These matches are written to stderr
70#
71resolve_nameprefix() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010072 local name=$1
73 local prefix=$2
74 local matches
75 local num_matches
Vincent Driessend0991262010-02-06 21:19:07 +010076
77 # first, check if there is a perfect match
Vincent Driessen21c7aa92010-02-16 20:57:35 +010078 if has "$(gitflow_local_branches)" "$prefix$name"; then
Vincent Driessend0991262010-02-06 21:19:07 +010079 echo "$name"
80 return 0
81 fi
82
Vincent Driessen21c7aa92010-02-16 20:57:35 +010083 matches=$(echo "$(gitflow_local_branches)" | grep "^$prefix$name")
Vincent Driessend0991262010-02-06 21:19:07 +010084 num_matches=$(echo "$matches" | wc -l)
85 if [ -z "$matches" ]; then
86 # no prefix match, so take it literally
87 warn "No branch matches prefix '$name'"
88 return 1
89 else
90 if [ $num_matches -eq 1 ]; then
91 echo "${matches#$prefix}"
92 return 0
93 else
94 # multiple matches, cannot decide
95 warn "Multiple branches match prefix '$name':"
96 for match in $matches; do
97 warn "- $match"
98 done
99 return 2
100 fi
101 fi
102}
103
104gitflow_current_branch() {
105 git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
106}
107
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100108gitflow_test_clean_working_tree() {
109 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
110 return 1
111 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
112 return 2
113 else
114 return 0
115 fi
116}
117
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100118gitflow_require_git_repo() {
119 if ! git rev-parse --git-dir >/dev/null 2>&1; then
120 die "Not a git repository"
121 fi
122}
123
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100124gitflow_require_clean_working_tree() {
125 gitflow_test_clean_working_tree
Vincent Driessenf46e2902010-02-15 23:01:52 +0100126 local result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100127 if [ $result -eq 1 ]; then
128 die "fatal: Working tree contains unstaged changes. Aborting."
129 fi
130 if [ $result -eq 2 ]; then
131 die "fatal: Index contains uncommited changes. Aborting."
132 fi
133}
134
Vincent Driessen49094bd2010-02-18 12:05:01 +0100135gitflow_branch_exists() {
136 has $1 $(gitflow_all_branches)
137}
138
139gitflow_tag_exists() {
140 has $1 $(gitflow_all_tags)
141}
142
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100143gitflow_require_local_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100144 if ! has $1 $(gitflow_local_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100145 die "fatal: Local branch '$1' does not exist and is required."
146 fi
147}
148
149gitflow_require_remote_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100150 if ! has $1 $(gitflow_remote_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100151 die "Remote branch '$1' does not exist and is required."
152 fi
153}
154
155gitflow_require_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100156 if ! has $1 $(gitflow_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100157 die "Branch '$1' does not exist and is required."
158 fi
159}
160
161gitflow_require_branch_absent() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100162 if has $1 $(gitflow_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100163 die "Branch '$1' already exists. Pick another name."
164 fi
165}
166
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100167gitflow_require_tag_absent() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100168 if has $1 $(gitflow_all_tags); then
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100169 die "Tag '$1' already exists. Pick another name."
170 fi
171}
172
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100173#
174# gitflow_test_branches_equal()
175#
176# Tests whether branches and their "origin" counterparts have diverged and need
177# merging first. It returns error codes to provide more detail, like so:
178#
179# 0 Branch heads point to the same commit
180# 1 First given branch needs fast-forwarding
181# 2 Second given branch needs fast-forwarding
182# 3 Branch needs a real merge
Vincent Driessen49094bd2010-02-18 12:05:01 +0100183# 4 There is no merge base, i.e. the branches have no common ancestors
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100184#
185gitflow_test_branches_equal() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100186 local commit1=$(git rev-parse "$1")
187 local commit2=$(git rev-parse "$2")
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100188 if [ "$commit1" != "$commit2" ]; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100189 local base=$(git merge-base "$commit1" "$commit2")
Vincent Driessen49094bd2010-02-18 12:05:01 +0100190 if [ $? -ne 0 ]; then
191 return 4
192 elif [ "$commit1" = "$base" ]; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100193 return 1
194 elif [ "$commit2" = "$base" ]; then
195 return 2
196 else
197 return 3
198 fi
199 else
200 return 0
201 fi
202}
203
204gitflow_require_branches_equal() {
205 gitflow_require_local_branch "$1"
206 gitflow_require_remote_branch "$2"
207 gitflow_test_branches_equal "$1" "$2"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100208 local status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100209 if [ $status -gt 0 ]; then
210 warn "Branches '$1' and '$2' have diverged."
211 if [ $status -eq 1 ]; then
212 die "And branch '$1' may be fast-forwarded."
213 elif [ $status -eq 2 ]; then
214 # Warn here, since there is no harm in being ahead
215 warn "And local branch '$1' is ahead of '$2'."
216 else
217 die "Branches need merging first."
218 fi
219 fi
220}
221
222#
223# gitflow_is_branch_merged_into()
224#
225# Checks whether branch $1 is succesfully merged into $2
226#
227gitflow_is_branch_merged_into() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100228 local subject=$1
229 local base=$2
230 local all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
Vincent Driessen27592dd2010-02-06 14:45:39 +0100231 has $base $all_merges
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100232}