blob: 8aaa3577dcf114c30d3065c56e43b0afb3993357 [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
Vincent Driessenb25ab832010-02-20 11:21:23 +010041# check if this repo has been inited for gitflow
42gitflow_has_master_configured() {
43 local master=$(git config --get gitflow.branch.master)
44 [ "$master" != "" ] && gitflow_local_branch_exists "$master"
45}
46
47gitflow_has_develop_configured() {
48 local develop=$(git config --get gitflow.branch.develop)
49 [ "$develop" != "" ] && gitflow_local_branch_exists "$develop"
50}
51
52gitflow_is_initialized() {
53 gitflow_has_master_configured && \
54 gitflow_has_develop_configured && \
55 # they should be different
56 [ "$(git config --get gitflow.branch.master)" != \
57 "$(git config --get gitflow.branch.develop)" ]
58}
59
Vincent Driessenc3607ac2010-02-05 19:53:45 +010060# get all available branches
Vincent Driessen21c7aa92010-02-16 20:57:35 +010061gitflow_local_branches() { git branch | sed 's/^[* ] //'; }
62gitflow_remote_branches() { git branch -r | sed 's/^[* ] //'; }
Vincent Driessen4e11dd62010-02-19 19:48:23 +010063gitflow_all_branches() { ( git branch; git branch -r) | sed 's/^[* ] //'; }
Vincent Driessen21c7aa92010-02-16 20:57:35 +010064gitflow_all_tags() { git tag; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010065
Vincent Driessend72e4ac2010-02-16 21:33:51 +010066# loading settings that can be overridden using git config
67gitflow_load_settings() {
Vincent Driessencf6e92a2010-02-19 19:44:48 +010068 export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1)
Vincent Driessend72e4ac2010-02-16 21:33:51 +010069 export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
70 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
71 export ORIGIN=$(git config --get gitflow.origin || echo origin)
Vincent Driessend72e4ac2010-02-16 21:33:51 +010072}
73
Vincent Driessend0991262010-02-06 21:19:07 +010074#
75# resolve_nameprefix
76#
77# Inputs:
78# $1 = name prefix to resolve
79# $2 = branch prefix to use
80#
Vincent Driessen21c7aa92010-02-16 20:57:35 +010081# Searches branch names from gitflow_local_branches() to look for a unique
82# branch name whose name starts with the given name prefix.
Vincent Driessend0991262010-02-06 21:19:07 +010083#
84# There are multiple exit codes possible:
85# 0: The unambiguous full name of the branch is written to stdout
86# (success)
87# 1: No match is found.
88# 2: Multiple matches found. These matches are written to stderr
89#
90resolve_nameprefix() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010091 local name=$1
92 local prefix=$2
93 local matches
94 local num_matches
Vincent Driessend0991262010-02-06 21:19:07 +010095
96 # first, check if there is a perfect match
Vincent Driessen21c7aa92010-02-16 20:57:35 +010097 if has "$(gitflow_local_branches)" "$prefix$name"; then
Vincent Driessend0991262010-02-06 21:19:07 +010098 echo "$name"
99 return 0
100 fi
101
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100102 matches=$(echo "$(gitflow_local_branches)" | grep "^$prefix$name")
Vincent Driessend0991262010-02-06 21:19:07 +0100103 num_matches=$(echo "$matches" | wc -l)
104 if [ -z "$matches" ]; then
105 # no prefix match, so take it literally
106 warn "No branch matches prefix '$name'"
107 return 1
108 else
109 if [ $num_matches -eq 1 ]; then
110 echo "${matches#$prefix}"
111 return 0
112 else
113 # multiple matches, cannot decide
114 warn "Multiple branches match prefix '$name':"
115 for match in $matches; do
116 warn "- $match"
117 done
118 return 2
119 fi
120 fi
121}
122
123gitflow_current_branch() {
124 git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
125}
126
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100127gitflow_test_clean_working_tree() {
128 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
129 return 1
130 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
131 return 2
132 else
133 return 0
134 fi
135}
136
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100137gitflow_require_git_repo() {
138 if ! git rev-parse --git-dir >/dev/null 2>&1; then
139 die "Not a git repository"
140 fi
141}
142
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100143gitflow_require_clean_working_tree() {
144 gitflow_test_clean_working_tree
Vincent Driessenf46e2902010-02-15 23:01:52 +0100145 local result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100146 if [ $result -eq 1 ]; then
147 die "fatal: Working tree contains unstaged changes. Aborting."
148 fi
149 if [ $result -eq 2 ]; then
150 die "fatal: Index contains uncommited changes. Aborting."
151 fi
152}
153
Vincent Driessenb25ab832010-02-20 11:21:23 +0100154gitflow_local_branch_exists() {
155 has $1 $(gitflow_local_branches)
156}
157
Vincent Driessen49094bd2010-02-18 12:05:01 +0100158gitflow_branch_exists() {
159 has $1 $(gitflow_all_branches)
160}
161
162gitflow_tag_exists() {
163 has $1 $(gitflow_all_tags)
164}
165
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100166gitflow_require_local_branch() {
Vincent Driessenb25ab832010-02-20 11:21:23 +0100167 if ! gitflow_local_branch_exists; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100168 die "fatal: Local branch '$1' does not exist and is required."
169 fi
170}
171
172gitflow_require_remote_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100173 if ! has $1 $(gitflow_remote_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100174 die "Remote branch '$1' does not exist and is required."
175 fi
176}
177
178gitflow_require_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100179 if ! has $1 $(gitflow_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100180 die "Branch '$1' does not exist and is required."
181 fi
182}
183
184gitflow_require_branch_absent() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100185 if has $1 $(gitflow_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100186 die "Branch '$1' already exists. Pick another name."
187 fi
188}
189
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100190gitflow_require_tag_absent() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100191 if has $1 $(gitflow_all_tags); then
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100192 die "Tag '$1' already exists. Pick another name."
193 fi
194}
195
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100196#
197# gitflow_test_branches_equal()
198#
199# Tests whether branches and their "origin" counterparts have diverged and need
200# merging first. It returns error codes to provide more detail, like so:
201#
202# 0 Branch heads point to the same commit
203# 1 First given branch needs fast-forwarding
204# 2 Second given branch needs fast-forwarding
205# 3 Branch needs a real merge
Vincent Driessen49094bd2010-02-18 12:05:01 +0100206# 4 There is no merge base, i.e. the branches have no common ancestors
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100207#
208gitflow_test_branches_equal() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100209 local commit1=$(git rev-parse "$1")
210 local commit2=$(git rev-parse "$2")
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100211 if [ "$commit1" != "$commit2" ]; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100212 local base=$(git merge-base "$commit1" "$commit2")
Vincent Driessen49094bd2010-02-18 12:05:01 +0100213 if [ $? -ne 0 ]; then
214 return 4
215 elif [ "$commit1" = "$base" ]; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100216 return 1
217 elif [ "$commit2" = "$base" ]; then
218 return 2
219 else
220 return 3
221 fi
222 else
223 return 0
224 fi
225}
226
227gitflow_require_branches_equal() {
228 gitflow_require_local_branch "$1"
229 gitflow_require_remote_branch "$2"
230 gitflow_test_branches_equal "$1" "$2"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100231 local status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100232 if [ $status -gt 0 ]; then
233 warn "Branches '$1' and '$2' have diverged."
234 if [ $status -eq 1 ]; then
235 die "And branch '$1' may be fast-forwarded."
236 elif [ $status -eq 2 ]; then
237 # Warn here, since there is no harm in being ahead
238 warn "And local branch '$1' is ahead of '$2'."
239 else
240 die "Branches need merging first."
241 fi
242 fi
243}
244
245#
246# gitflow_is_branch_merged_into()
247#
248# Checks whether branch $1 is succesfully merged into $2
249#
250gitflow_is_branch_merged_into() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100251 local subject=$1
252 local base=$2
253 local all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
Vincent Driessen27592dd2010-02-06 14:45:39 +0100254 has $base $all_merges
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100255}