blob: 70b137bc68974f100dbd6f415fe9492cecd76229 [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
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +010052gitflow_has_prefixes_configured() {
53 git config --get gitflow.prefix.feature >/dev/null 2>&1 && \
54 git config --get gitflow.prefix.release >/dev/null 2>&1 && \
55 git config --get gitflow.prefix.hotfix >/dev/null 2>&1 && \
56 git config --get gitflow.prefix.support >/dev/null 2>&1 && \
57 git config --get gitflow.prefix.versiontag >/dev/null 2>&1
58}
59
Vincent Driessenb25ab832010-02-20 11:21:23 +010060gitflow_is_initialized() {
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +010061 gitflow_has_master_configured && \
62 gitflow_has_develop_configured && \
63 [ "$(git config --get gitflow.branch.master)" != \
64 "$(git config --get gitflow.branch.develop)" ] && \
65 gitflow_has_prefixes_configured
Vincent Driessenb25ab832010-02-20 11:21:23 +010066}
67
Vincent Driessenc3607ac2010-02-05 19:53:45 +010068# get all available branches
Vincent Driessen21c7aa92010-02-16 20:57:35 +010069gitflow_local_branches() { git branch | sed 's/^[* ] //'; }
70gitflow_remote_branches() { git branch -r | sed 's/^[* ] //'; }
Vincent Driessen4e11dd62010-02-19 19:48:23 +010071gitflow_all_branches() { ( git branch; git branch -r) | sed 's/^[* ] //'; }
Vincent Driessen21c7aa92010-02-16 20:57:35 +010072gitflow_all_tags() { git tag; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010073
Vincent Driessend72e4ac2010-02-16 21:33:51 +010074# loading settings that can be overridden using git config
75gitflow_load_settings() {
Vincent Driessencf6e92a2010-02-19 19:44:48 +010076 export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1)
Vincent Driessenc1598bf2010-02-20 16:52:48 +010077 export MASTER_BRANCH=$(git config --get gitflow.branch.master)
78 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
Vincent Driessend72e4ac2010-02-16 21:33:51 +010079 export ORIGIN=$(git config --get gitflow.origin || echo origin)
Vincent Driessend72e4ac2010-02-16 21:33:51 +010080}
81
Vincent Driessend0991262010-02-06 21:19:07 +010082#
83# resolve_nameprefix
84#
85# Inputs:
86# $1 = name prefix to resolve
87# $2 = branch prefix to use
88#
Vincent Driessen21c7aa92010-02-16 20:57:35 +010089# Searches branch names from gitflow_local_branches() to look for a unique
90# branch name whose name starts with the given name prefix.
Vincent Driessend0991262010-02-06 21:19:07 +010091#
92# There are multiple exit codes possible:
93# 0: The unambiguous full name of the branch is written to stdout
94# (success)
95# 1: No match is found.
96# 2: Multiple matches found. These matches are written to stderr
97#
98resolve_nameprefix() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010099 local name=$1
100 local prefix=$2
101 local matches
102 local num_matches
Vincent Driessend0991262010-02-06 21:19:07 +0100103
104 # first, check if there is a perfect match
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100105 if has "$(gitflow_local_branches)" "$prefix$name"; then
Vincent Driessend0991262010-02-06 21:19:07 +0100106 echo "$name"
107 return 0
108 fi
109
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100110 matches=$(echo "$(gitflow_local_branches)" | grep "^$prefix$name")
Vincent Driessend0991262010-02-06 21:19:07 +0100111 num_matches=$(echo "$matches" | wc -l)
112 if [ -z "$matches" ]; then
113 # no prefix match, so take it literally
114 warn "No branch matches prefix '$name'"
115 return 1
116 else
117 if [ $num_matches -eq 1 ]; then
118 echo "${matches#$prefix}"
119 return 0
120 else
121 # multiple matches, cannot decide
122 warn "Multiple branches match prefix '$name':"
123 for match in $matches; do
124 warn "- $match"
125 done
126 return 2
127 fi
128 fi
129}
130
131gitflow_current_branch() {
132 git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
133}
134
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100135gitflow_test_clean_working_tree() {
136 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
137 return 1
138 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
139 return 2
140 else
141 return 0
142 fi
143}
144
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100145gitflow_require_git_repo() {
146 if ! git rev-parse --git-dir >/dev/null 2>&1; then
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100147 die "fatal: Not a git repository"
148 fi
149}
150
151gitflow_require_initialized() {
152 if ! gitflow_is_initialized; then
153 die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first."
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100154 fi
155}
156
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100157gitflow_require_clean_working_tree() {
158 gitflow_test_clean_working_tree
Vincent Driessenf46e2902010-02-15 23:01:52 +0100159 local result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100160 if [ $result -eq 1 ]; then
161 die "fatal: Working tree contains unstaged changes. Aborting."
162 fi
163 if [ $result -eq 2 ]; then
164 die "fatal: Index contains uncommited changes. Aborting."
165 fi
166}
167
Vincent Driessenb25ab832010-02-20 11:21:23 +0100168gitflow_local_branch_exists() {
169 has $1 $(gitflow_local_branches)
170}
171
Vincent Driessen49094bd2010-02-18 12:05:01 +0100172gitflow_branch_exists() {
173 has $1 $(gitflow_all_branches)
174}
175
176gitflow_tag_exists() {
177 has $1 $(gitflow_all_tags)
178}
179
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100180gitflow_require_local_branch() {
Vincent Driessenb25ab832010-02-20 11:21:23 +0100181 if ! gitflow_local_branch_exists; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100182 die "fatal: Local branch '$1' does not exist and is required."
183 fi
184}
185
186gitflow_require_remote_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100187 if ! has $1 $(gitflow_remote_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100188 die "Remote branch '$1' does not exist and is required."
189 fi
190}
191
192gitflow_require_branch() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100193 if ! has $1 $(gitflow_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100194 die "Branch '$1' does not exist and is required."
195 fi
196}
197
198gitflow_require_branch_absent() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100199 if has $1 $(gitflow_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100200 die "Branch '$1' already exists. Pick another name."
201 fi
202}
203
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100204gitflow_require_tag_absent() {
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100205 if has $1 $(gitflow_all_tags); then
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100206 die "Tag '$1' already exists. Pick another name."
207 fi
208}
209
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100210#
211# gitflow_test_branches_equal()
212#
213# Tests whether branches and their "origin" counterparts have diverged and need
214# merging first. It returns error codes to provide more detail, like so:
215#
216# 0 Branch heads point to the same commit
217# 1 First given branch needs fast-forwarding
218# 2 Second given branch needs fast-forwarding
219# 3 Branch needs a real merge
Vincent Driessen49094bd2010-02-18 12:05:01 +0100220# 4 There is no merge base, i.e. the branches have no common ancestors
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100221#
222gitflow_test_branches_equal() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100223 local commit1=$(git rev-parse "$1")
224 local commit2=$(git rev-parse "$2")
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100225 if [ "$commit1" != "$commit2" ]; then
Vincent Driessenf46e2902010-02-15 23:01:52 +0100226 local base=$(git merge-base "$commit1" "$commit2")
Vincent Driessen49094bd2010-02-18 12:05:01 +0100227 if [ $? -ne 0 ]; then
228 return 4
229 elif [ "$commit1" = "$base" ]; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100230 return 1
231 elif [ "$commit2" = "$base" ]; then
232 return 2
233 else
234 return 3
235 fi
236 else
237 return 0
238 fi
239}
240
241gitflow_require_branches_equal() {
242 gitflow_require_local_branch "$1"
243 gitflow_require_remote_branch "$2"
244 gitflow_test_branches_equal "$1" "$2"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100245 local status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100246 if [ $status -gt 0 ]; then
247 warn "Branches '$1' and '$2' have diverged."
248 if [ $status -eq 1 ]; then
249 die "And branch '$1' may be fast-forwarded."
250 elif [ $status -eq 2 ]; then
251 # Warn here, since there is no harm in being ahead
252 warn "And local branch '$1' is ahead of '$2'."
253 else
254 die "Branches need merging first."
255 fi
256 fi
257}
258
259#
260# gitflow_is_branch_merged_into()
261#
262# Checks whether branch $1 is succesfully merged into $2
263#
264gitflow_is_branch_merged_into() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100265 local subject=$1
266 local base=$2
267 local all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
Vincent Driessen27592dd2010-02-06 14:45:39 +0100268 has $base $all_merges
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100269}