forked from tftc/flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitflow-common
378 lines (332 loc) · 9.94 KB
/
gitflow-common
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# shell output
warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }
escape() {
echo "$1" | sed 's/\([\.\$\*]\)/\\\1/g'
}
# set logic
has() {
local item=$1; shift
echo " $@ " | grep -q " $(escape $item) "
}
# basic math
min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }
# basic string matching
startswith() { [ "$1" != "${1#$2}" ]; }
endswith() { [ "$1" != "${1%$2}" ]; }
# convenience functions for checking shFlags flags
flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
#
# Git specific common functionality
#
git_do() {
# equivalent to git, used to indicate actions that make modifications
if flag show_commands; then
echo "git $@" >&2
fi
git "$@"
}
git_local_branches() { git branch --no-color | sed 's/^[* ] //'; }
git_remote_branches() { git branch -r --no-color | sed 's/^[* ] //'; }
git_all_branches() { ( git branch --no-color; git branch -r --no-color) | sed 's/^[* ] //'; }
git_all_tags() { git tag; }
git_current_branch() {
git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
}
git_is_clean_working_tree() {
if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
return 1
elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
return 2
else
return 0
fi
}
git_repo_is_headless() {
! git rev-parse --quiet --verify HEAD >/dev/null 2>&1
}
git_local_branch_exists() {
has $1 $(git_local_branches)
}
git_remote_branch_exists() {
has $1 $(git_remote_branches)
}
git_branch_exists() {
has $1 $(git_all_branches)
}
git_tag_exists() {
has $1 $(git_all_tags)
}
#
# git_compare_branches()
#
# Tests whether branches and their "origin" counterparts have diverged and need
# merging first. It returns error codes to provide more detail, like so:
#
# 0 Branch heads point to the same commit
# 1 First given branch needs fast-forwarding
# 2 Second given branch needs fast-forwarding
# 3 Branch needs a real merge
# 4 There is no merge base, i.e. the branches have no common ancestors
#
git_compare_branches() {
local commit1=$(git rev-parse "$1")
local commit2=$(git rev-parse "$2")
if [ "$commit1" != "$commit2" ]; then
local base=$(git merge-base "$commit1" "$commit2")
if [ $? -ne 0 ]; then
return 4
elif [ "$commit1" = "$base" ]; then
return 1
elif [ "$commit2" = "$base" ]; then
return 2
else
return 3
fi
else
return 0
fi
}
#
# git_is_branch_merged_into()
#
# Checks whether branch $1 is succesfully merged into $2
#
git_is_branch_merged_into() {
local subject=$1
local base=$2
local all_merges="$(git branch --no-color --contains $subject | sed 's/^[* ] //')"
has $base $all_merges
}
#
# gitflow specific common functionality
#
# check if this repo has been inited for gitflow
gitflow_has_master_configured() {
local master=$(git config --get gitflow.branch.master)
[ "$master" != "" ] && git_local_branch_exists "$master"
}
gitflow_has_prefixes_configured() {
git config --get gitflow.prefix.feature >/dev/null 2>&1 && \
git config --get gitflow.prefix.release >/dev/null 2>&1 && \
git config --get gitflow.prefix.hotfix >/dev/null 2>&1
}
gitflow_is_initialized() {
gitflow_has_master_configured && \
gitflow_has_prefixes_configured
}
# loading settings that can be overridden using git config
gitflow_load_settings() {
export DOT_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
export MASTER_BRANCH=$(git config --get gitflow.branch.master)
export ORIGIN=$(git config --get gitflow.origin || echo origin)
}
#
# gitflow_resolve_nameprefix
#
# Inputs:
# $1 = name prefix to resolve
# $2 = branch prefix to use
#
# Searches branch names from git_local_branches() to look for a unique
# branch name whose name starts with the given name prefix.
#
# There are multiple exit codes possible:
# 0: The unambiguous full name of the branch is written to stdout
# (success)
# 1: No match is found.
# 2: Multiple matches found. These matches are written to stderr
#
gitflow_resolve_nameprefix() {
local name=$1
local prefix=$2
local matches
local num_matches
# first, check if there is a perfect match
if git_local_branch_exists "$prefix$name"; then
echo "$name"
return 0
fi
matches=$(echo "$(git_local_branches)" | grep "^$(escape "$prefix$name")")
num_matches=$(echo "$matches" | wc -l)
if [ -z "$matches" ]; then
# no prefix match, so take it literally
warn "No branch matches prefix '$name'"
return 1
else
if [ $num_matches -eq 1 ]; then
echo "${matches#$prefix}"
return 0
else
# multiple matches, cannot decide
warn "Multiple branches match prefix '$name':"
for match in $matches; do
warn "- $match"
done
return 2
fi
fi
}
#
# Assertions for use in git-flow subcommands
#
require_git_repo() {
if ! git rev-parse --git-dir >/dev/null 2>&1; then
die "fatal: Not a git repository"
fi
}
require_gitflow_initialized() {
if ! gitflow_is_initialized; then
die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first."
fi
}
require_clean_working_tree() {
git_is_clean_working_tree
local result=$?
if [ $result -eq 1 ]; then
die "fatal: Working tree contains unstaged changes. Aborting."
fi
if [ $result -eq 2 ]; then
die "fatal: Index contains uncommited changes. Aborting."
fi
}
require_local_branch() {
if ! git_local_branch_exists $1; then
die "fatal: Local branch '$1' does not exist and is required."
fi
}
require_remote_branch() {
if ! has $1 $(git_remote_branches); then
die "Remote branch '$1' does not exist and is required."
fi
}
require_branch() {
if ! has $1 $(git_all_branches); then
die "Branch '$1' does not exist and is required."
fi
}
require_branch_absent() {
if has $1 $(git_all_branches); then
die "Branch '$1' already exists. Pick another name."
fi
}
require_tag_absent() {
for tag in $(git_all_tags); do
if [ "$1" = "$tag" ]; then
die "Tag '$1' already exists. Pick another name."
fi
done
}
require_branches_equal() {
require_local_branch "$1"
require_remote_branch "$2"
git_compare_branches "$1" "$2"
local status=$?
if [ $status -gt 0 ]; then
warn "Branches '$1' and '$2' have diverged."
if [ $status -eq 1 ]; then
die "And branch '$1' may be fast-forwarded."
elif [ $status -eq 2 ]; then
# Warn here, since there is no harm in being ahead
warn "And local branch '$1' is ahead of '$2'."
else
die "Branches need merging first."
fi
fi
}
require_branches_absolutely_equal() {
require_local_branch "$1"
require_remote_branch "$2"
git_compare_branches "$1" "$2"
local status=$?
if [ $status -gt 0 ]; then
warn "Branches '$1' and '$2' have diverged."
if [ $status -eq 1 ]; then
warn "And branch '$1' may be fast-forwarded."
return 1;
elif [ $status -eq 2 ]; then
# Warn here, since there is no harm in being ahead
warn "And local branch '$1' is ahead of '$2'."
return 2;
else
warn "Branches need merging first."
return 3;
fi
fi
return 0
}
generate_tag_version() {
local tagname=$1
if git_tag_exists "$tagname"; then
#get tag max count
local count=$(git_do tag -l "$tagname"-*|awk -F '-build.' '{print $2}'|sort -nr|head -n 1)
if [ -z "$count" ]; then
count=1
else
count=$[$count+1]
fi
tagname="$tagname-build.$count"
fi
echo $tagname
}
require_tag_validation() {
local tagname=$1
local tagname_=$1
local latest_tag="$(get_latest_tag)"
local latest_tag_="$(get_latest_tag)"
echo $latest_tag_
local latest_tag_version_arr=(`echo $latest_tag|sed 's/v//g;s/\-build//g;s/\./ /g'|awk '{printf($0)}'`)
local tagname_version_arr=(`echo $tagname|sed 's/v//g;s/\-build//g;s/\./ /g'|awk '{printf($0)}'`)
echo "latest_tag_version: '${latest_tag_version_arr[@]}', tagname_version; '${tagname_version_arr[@]}'"
if [ ${latest_tag_version_arr[0]} -gt ${tagname_version_arr[0]} ]; then
die "current tagName is '$tagname_', git latestTagName is '$latest_tag_', Please check it0!"
elif [ ${latest_tag_version_arr[0]} -lt ${tagname_version_arr[0]} ]; then
return 0
fi
if [ ${latest_tag_version_arr[1]} -gt ${tagname_version_arr[1]} ]; then
die "current tagName is '$tagname_', git latestTagName is '$latest_tag_', Please check it1!"
elif [ ${latest_tag_version_arr[1]} -lt ${tagname_version_arr[1]} ]; then
return 0
fi
local tag_str2_array=(${latest_tag_version_arr[2]//-/ })
local tag_str2_version=${tag_str2_array[0]}
if [ $tag_str2_version -gt ${tagname_version_arr[2]} ]; then
die "current tagName is '$tagname_', git latestTagName is '$latest_tag_', Please check it2!"
elif [ $tag_str2_version -lt ${tagname_version_arr[2]} ]; then
echo "check 2"
return 0
fi
if [[ ${tagname_version_arr[3]} && $latest_tag_version_arr[3] ]]; then
if [ ${latest_tag_version_arr[3]} -gt ${tagname_version_arr[3]} ]; then
die "current tagName is '$tagname_', git latestTagName is '$latest_tag_', Please check it!"
fi
fi
}
require_in_current_branch(){
local current_branch=$(git_current_branch)
if [ "$current_branch" != "$BRANCH" ]; then
default_suggestion='yes'
echo
echo "The current branch is $current_branch, not $BRANCH"
printf " Do you want checkout to $BRANCH then contine? [$default_suggestion] "
read answer
answer=${answer:-$default_suggestion}
[ "$answer" != "yes" ] && die 'Please checkout to $BRANCH manually'
echo " Branch $current_branch Will auto checkout to $BRANCH "
git_do checkout -q $BRANCH
fi
}
get_latest_tag() {
if [ -n "$1" ]; then
local count=$(git_do tag -l "$1"*|awk -F '-build.' '{print $2}'|sort -nr|head -n 1)
if [ -z "$count" ]; then
echo $1
else
echo "$1-build.$count"
fi
else
echo "$(git_do describe --tags)"
fi
}