blob: 2fbd8ec5b568bf580198450275d37e970fe8198f [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 Driessen27592dd2010-02-06 14:45:39 +010021 typeset 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 Driessen27592dd2010-02-06 14:45:39 +010034flag() { typeset FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
35noflag() { typeset 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
42LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
43REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
44ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
45
Vincent Driessend0991262010-02-06 21:19:07 +010046#
47# resolve_nameprefix
48#
49# Inputs:
50# $1 = name prefix to resolve
51# $2 = branch prefix to use
52#
53# Searches branch names from LOCAL_BRANCHES to look for a unique branch
54# name whose name starts with the given name prefix.
55#
56# There are multiple exit codes possible:
57# 0: The unambiguous full name of the branch is written to stdout
58# (success)
59# 1: No match is found.
60# 2: Multiple matches found. These matches are written to stderr
61#
62resolve_nameprefix() {
63 typeset name="$1"
64 typeset prefix="$2"
65 typeset matches
66 typeset -i num_matches
67
68 # first, check if there is a perfect match
69 if has "$LOCAL_BRANCHES" "$prefix$name"; then
70 echo "$name"
71 return 0
72 fi
73
74 matches="$(echo "$LOCAL_BRANCHES" | grep "^$prefix$name")"
75 num_matches=$(echo "$matches" | wc -l)
76 if [ -z "$matches" ]; then
77 # no prefix match, so take it literally
78 warn "No branch matches prefix '$name'"
79 return 1
80 else
81 if [ $num_matches -eq 1 ]; then
82 echo "${matches#$prefix}"
83 return 0
84 else
85 # multiple matches, cannot decide
86 warn "Multiple branches match prefix '$name':"
87 for match in $matches; do
88 warn "- $match"
89 done
90 return 2
91 fi
92 fi
93}
94
95gitflow_current_branch() {
96 git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
97}
98
Vincent Driessenc3607ac2010-02-05 19:53:45 +010099gitflow_test_clean_working_tree() {
100 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
101 return 1
102 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
103 return 2
104 else
105 return 0
106 fi
107}
108
109gitflow_require_clean_working_tree() {
110 gitflow_test_clean_working_tree
Vincent Driessen27592dd2010-02-06 14:45:39 +0100111 typeset -i result=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100112 if [ $result -eq 1 ]; then
113 die "fatal: Working tree contains unstaged changes. Aborting."
114 fi
115 if [ $result -eq 2 ]; then
116 die "fatal: Index contains uncommited changes. Aborting."
117 fi
118}
119
120gitflow_require_local_branch() {
121 if ! has $1 $LOCAL_BRANCHES; then
122 die "fatal: Local branch '$1' does not exist and is required."
123 fi
124}
125
126gitflow_require_remote_branch() {
127 if ! has $1 $REMOTE_BRANCHES; then
128 die "Remote branch '$1' does not exist and is required."
129 fi
130}
131
132gitflow_require_branch() {
133 if ! has $1 $ALL_BRANCHES; then
134 die "Branch '$1' does not exist and is required."
135 fi
136}
137
138gitflow_require_branch_absent() {
139 if has $1 $ALL_BRANCHES; then
140 die "Branch '$1' already exists. Pick another name."
141 fi
142}
143
144#
145# gitflow_test_branches_equal()
146#
147# Tests whether branches and their "origin" counterparts have diverged and need
148# merging first. It returns error codes to provide more detail, like so:
149#
150# 0 Branch heads point to the same commit
151# 1 First given branch needs fast-forwarding
152# 2 Second given branch needs fast-forwarding
153# 3 Branch needs a real merge
154#
155gitflow_test_branches_equal() {
Vincent Driessen27592dd2010-02-06 14:45:39 +0100156 typeset commit1=$(git rev-parse "$1")
157 typeset commit2=$(git rev-parse "$2")
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100158 if [ "$commit1" != "$commit2" ]; then
Vincent Driessen27592dd2010-02-06 14:45:39 +0100159 typeset base=$(git merge-base "$commit1" "$commit2")
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100160 if [ "$commit1" = "$base" ]; then
161 return 1
162 elif [ "$commit2" = "$base" ]; then
163 return 2
164 else
165 return 3
166 fi
167 else
168 return 0
169 fi
170}
171
172gitflow_require_branches_equal() {
173 gitflow_require_local_branch "$1"
174 gitflow_require_remote_branch "$2"
175 gitflow_test_branches_equal "$1" "$2"
Vincent Driessen27592dd2010-02-06 14:45:39 +0100176 typeset -i status=$?
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100177 if [ $status -gt 0 ]; then
178 warn "Branches '$1' and '$2' have diverged."
179 if [ $status -eq 1 ]; then
180 die "And branch '$1' may be fast-forwarded."
181 elif [ $status -eq 2 ]; then
182 # Warn here, since there is no harm in being ahead
183 warn "And local branch '$1' is ahead of '$2'."
184 else
185 die "Branches need merging first."
186 fi
187 fi
188}
189
190#
191# gitflow_is_branch_merged_into()
192#
193# Checks whether branch $1 is succesfully merged into $2
194#
195gitflow_is_branch_merged_into() {
Vincent Driessen27592dd2010-02-06 14:45:39 +0100196 typeset subject=$1
197 typeset base=$2
198 typeset all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
199 has $base $all_merges
Vincent Driessenc3607ac2010-02-05 19:53:45 +0100200}