blob: aa337007c96d3849b9e54f33a278196df032eb91 [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
Vincent Driessen7832d6e2010-02-21 21:31:03 +010015#
16# Common functionality
17#
18
Vincent Driessenc3607ac2010-02-05 19:53:45 +010019# shell output
20warn() { echo "$@" >&2; }
21die() { warn "$@"; exit 1; }
22
23# set logic
24has() {
Vincent Driessenf46e2902010-02-15 23:01:52 +010025 local item=$1; shift
Vincent Driessenc3607ac2010-02-05 19:53:45 +010026 echo " $@ " | grep -q " $item "
27}
28
29# basic math
30min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
31max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }
32
33# basic string matching
34startswith() { [ "$1" != "${1#$2}" ]; }
Vincent Driessene0d8af32010-02-22 12:21:36 +010035endswith() { [ "$1" != "${1%$2}" ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010036
37# convenience functions for checking shFlags flags
Vincent Driessenf46e2902010-02-15 23:01:52 +010038flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
39noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
Vincent Driessenc3607ac2010-02-05 19:53:45 +010040
41#
42# Git specific common functionality
43#
44
Vincent Driessen7832d6e2010-02-21 21:31:03 +010045git_local_branches() { git branch | sed 's/^[* ] //'; }
46git_remote_branches() { git branch -r | sed 's/^[* ] //'; }
47git_all_branches() { ( git branch; git branch -r) | sed 's/^[* ] //'; }
48git_all_tags() { git tag; }
49
Vincent Driessen55c15532010-02-22 07:28:27 +010050git_current_branch() {
51 git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
52}
53
54git_is_clean_working_tree() {
55 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
56 return 1
57 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
58 return 2
59 else
60 return 0
61 fi
62}
63
64git_repo_is_headless() {
65 ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1
66}
67
68git_local_branch_exists() {
69 has $1 $(git_local_branches)
70}
71
72git_branch_exists() {
73 has $1 $(git_all_branches)
74}
75
76git_tag_exists() {
77 has $1 $(git_all_tags)
78}
79
80#
81# git_compare_branches()
82#
83# Tests whether branches and their "origin" counterparts have diverged and need
84# merging first. It returns error codes to provide more detail, like so:
85#
86# 0 Branch heads point to the same commit
87# 1 First given branch needs fast-forwarding
88# 2 Second given branch needs fast-forwarding
89# 3 Branch needs a real merge
90# 4 There is no merge base, i.e. the branches have no common ancestors
91#
92git_compare_branches() {
93 local commit1=$(git rev-parse "$1")
94 local commit2=$(git rev-parse "$2")
95 if [ "$commit1" != "$commit2" ]; then
96 local base=$(git merge-base "$commit1" "$commit2")
97 if [ $? -ne 0 ]; then
98 return 4
99 elif [ "$commit1" = "$base" ]; then
100 return 1
101 elif [ "$commit2" = "$base" ]; then
102 return 2
103 else
104 return 3
105 fi
106 else
107 return 0
108 fi
109}
110
111#
112# git_is_branch_merged_into()
113#
114# Checks whether branch $1 is succesfully merged into $2
115#
116git_is_branch_merged_into() {
117 local subject=$1
118 local base=$2
119 local all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
120 has $base $all_merges
121}
122
123#
124# gitflow specific common functionality
125#
126
Vincent Driessenb25ab832010-02-20 11:21:23 +0100127# check if this repo has been inited for gitflow
128gitflow_has_master_configured() {
129 local master=$(git config --get gitflow.branch.master)
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100130 [ "$master" != "" ] && git_local_branch_exists "$master"
Vincent Driessenb25ab832010-02-20 11:21:23 +0100131}
132
133gitflow_has_develop_configured() {
134 local develop=$(git config --get gitflow.branch.develop)
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100135 [ "$develop" != "" ] && git_local_branch_exists "$develop"
Vincent Driessenb25ab832010-02-20 11:21:23 +0100136}
137
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100138gitflow_has_prefixes_configured() {
139 git config --get gitflow.prefix.feature >/dev/null 2>&1 && \
140 git config --get gitflow.prefix.release >/dev/null 2>&1 && \
141 git config --get gitflow.prefix.hotfix >/dev/null 2>&1 && \
142 git config --get gitflow.prefix.support >/dev/null 2>&1 && \
143 git config --get gitflow.prefix.versiontag >/dev/null 2>&1
144}
145
Vincent Driessenb25ab832010-02-20 11:21:23 +0100146gitflow_is_initialized() {
Vincent Driessen1d8bb0d2010-02-20 16:46:38 +0100147 gitflow_has_master_configured && \
148 gitflow_has_develop_configured && \
149 [ "$(git config --get gitflow.branch.master)" != \
150 "$(git config --get gitflow.branch.develop)" ] && \
151 gitflow_has_prefixes_configured
Vincent Driessenb25ab832010-02-20 11:21:23 +0100152}
153
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100154# loading settings that can be overridden using git config
155gitflow_load_settings() {
Vincent Driessencf6e92a2010-02-19 19:44:48 +0100156 export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1)
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100157 export MASTER_BRANCH=$(git config --get gitflow.branch.master)
158 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100159 export ORIGIN=$(git config --get gitflow.origin || echo origin)
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100160}
161
Vincent Driessend0991262010-02-06 21:19:07 +0100162#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100163# gitflow_resolve_nameprefix
Vincent Driessend0991262010-02-06 21:19:07 +0100164#
165# Inputs:
166# $1 = name prefix to resolve
167# $2 = branch prefix to use
168#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100169# Searches branch names from git_local_branches() to look for a unique
Vincent Driessen21c7aa92010-02-16 20:57:35 +0100170# branch name whose name starts with the given name prefix.
Vincent Driessend0991262010-02-06 21:19:07 +0100171#
172# There are multiple exit codes possible:
173# 0: The unambiguous full name of the branch is written to stdout
174# (success)
175# 1: No match is found.
176# 2: Multiple matches found. These matches are written to stderr
177#
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100178gitflow_resolve_nameprefix() {
Vincent Driessenf46e2902010-02-15 23:01:52 +0100179 local name=$1
180 local prefix=$2
181 local matches
182 local num_matches
Vincent Driessend0991262010-02-06 21:19:07 +0100183
184 # first, check if there is a perfect match
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100185 if has "$(git_local_branches)" "$prefix$name"; then
Vincent Driessend0991262010-02-06 21:19:07 +0100186 echo "$name"
187 return 0
188 fi
189
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100190 matches=$(echo "$(git_local_branches)" | grep "^$prefix$name")
Vincent Driessend0991262010-02-06 21:19:07 +0100191 num_matches=$(echo "$matches" | wc -l)
192 if [ -z "$matches" ]; then
193 # no prefix match, so take it literally
194 warn "No branch matches prefix '$name'"
195 return 1
196 else
197 if [ $num_matches -eq 1 ]; then
198 echo "${matches#$prefix}"
199 return 0
200 else
201 # multiple matches, cannot decide
202 warn "Multiple branches match prefix '$name':"
203 for match in $matches; do
204 warn "- $match"
205 done
206 return 2
207 fi
208 fi
209}
210
Vincent Driessen55c15532010-02-22 07:28:27 +0100211#
212# Assertions for use in git-flow subcommands
213#
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100214
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100215require_git_repo() {
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100216 if ! git rev-parse --git-dir >/dev/null 2>&1; then
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100217 die "fatal: Not a git repository"
218 fi
219}
220
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100221require_gitflow_initialized() {
Vincent Driessenc1598bf2010-02-20 16:52:48 +0100222 if ! gitflow_is_initialized; then
223 die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first."
Vincent Driessend72e4ac2010-02-16 21:33:51 +0100224 fi
225}
226
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100227require_clean_working_tree() {
228 git_is_clean_working_tree
Vincent Driessenf46e2902010-02-15 23:01:52 +0100229 local result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100230 if [ $result -eq 1 ]; then
231 die "fatal: Working tree contains unstaged changes. Aborting."
232 fi
233 if [ $result -eq 2 ]; then
234 die "fatal: Index contains uncommited changes. Aborting."
235 fi
236}
237
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100238require_local_branch() {
Vincent Driessen6ee62232010-02-22 07:45:24 +0100239 if ! git_local_branch_exists $1; then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100240 die "fatal: Local branch '$1' does not exist and is required."
241 fi
242}
243
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100244require_remote_branch() {
245 if ! has $1 $(git_remote_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100246 die "Remote branch '$1' does not exist and is required."
247 fi
248}
249
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100250require_branch() {
251 if ! has $1 $(git_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100252 die "Branch '$1' does not exist and is required."
253 fi
254}
255
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100256require_branch_absent() {
257 if has $1 $(git_all_branches); then
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100258 die "Branch '$1' already exists. Pick another name."
259 fi
260}
261
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100262require_tag_absent() {
263 if has $1 $(git_all_tags); then
Vincent Driessen1a2868b2010-02-07 23:23:16 +0100264 die "Tag '$1' already exists. Pick another name."
265 fi
266}
267
Vincent Driessen7832d6e2010-02-21 21:31:03 +0100268require_branches_equal() {
269 require_local_branch "$1"
270 require_remote_branch "$2"
271 git_compare_branches "$1" "$2"
Vincent Driessenf46e2902010-02-15 23:01:52 +0100272 local status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100273 if [ $status -gt 0 ]; then
274 warn "Branches '$1' and '$2' have diverged."
275 if [ $status -eq 1 ]; then
276 die "And branch '$1' may be fast-forwarded."
277 elif [ $status -eq 2 ]; then
278 # Warn here, since there is no harm in being ahead
279 warn "And local branch '$1' is ahead of '$2'."
280 else
281 die "Branches need merging first."
282 fi
283 fi
284}