-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
git-release
executable file
·138 lines (118 loc) · 3.04 KB
/
git-release
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
#!/usr/bin/env bash
set -e
hook() {
local hook=.git/hooks/$1.sh
# compat without extname
if test ! -f "$hook"; then
hook=.git/hooks/$1
fi
if test -f "$hook"; then
echo "... $1"
shift
if test -x "$hook"; then
$hook "$@"
else
. "$hook" "$@"
fi
fi
}
exit_with_msg() {
>&2 echo "$1"
exit 1
}
if test $# -gt 0; then
remote=''
# check for flags
while test $# != 0
do
case "$1" in
-c) need_changelog=true;;
-r) remote=$2; shift ;;
-m) msg=$2; shift ;;
-s)
test -n "$keyid" &&
exit_with_msg "Please use '-s' OR '-u'"
sign=true
;;
-u)
test -n "$sign" &&
exit_with_msg "Please use '-s' OR '-u'"
keyid=$2
shift
;;
--semver)
test -z "$2" &&
exit_with_msg "major/minor/patch required for --semver option"
semver=$2
shift
;;
--prefix)
test -z "$2" &&
exit_with_msg "prefix string required for --prefix option"
prefix="$2"
shift
;;
--no-empty-commit) no_empty_commit=true;;
--) shift; hook_args="$hook_args $*"; break;;
*) test -z "$version" && version=$1 ;;
esac
shift
done
if [ -n "$semver" ]; then
if [ -z "$(git tag)" ]; then
echo "there is no tag in the git repo" 1>&2
exit 1
fi
latest_tag=$(git describe --tags "$(git rev-list --tags --max-count=1)")
if [[ ! "$latest_tag" =~ \
^$prefix([^0-9]*)([1-9][0-9]+|[0-9])\.([1-9][0-9]+|[0-9])\.([1-9][0-9]+|[0-9])(.*) ]]; then
echo "the latest tag doesn't match semver format requirement" 1>&2
exit 1
fi
case "$semver" in
major ) version="${BASH_REMATCH[2]}" ;;
minor ) version="${BASH_REMATCH[3]}" ;;
patch ) version="${BASH_REMATCH[4]}" ;;
* ) echo "invalid semver argument given: $semver" 1>&2
exit 1
;;
esac
(( ++version ))
case "$semver" in
major ) version="$prefix${BASH_REMATCH[1]}$version.0.0${BASH_REMATCH[5]}" ;;
minor ) version="$prefix${BASH_REMATCH[1]}${BASH_REMATCH[2]}.$version.0${BASH_REMATCH[5]}" ;;
patch ) version="$prefix${BASH_REMATCH[1]}${BASH_REMATCH[2]}.${BASH_REMATCH[3]}.$version${BASH_REMATCH[5]}" ;;
esac
fi
hook_args="$version"
if [ -z "$msg" ]; then
msg="Release ${version}"
fi
# shellcheck disable=SC2086
hook pre-release $hook_args \
|| exit_with_msg "pre-release hook failed! Cancelling release."
echo "... releasing $version"
if [ "$need_changelog" = true ]; then
git-changelog -t "$version"
fi
if [ "$no_empty_commit" = true ]; then
git commit -a -m "$msg" || true
else
git commit -a -m "$msg" --allow-empty
fi
declare -a sign_args
if [ "$sign" == true ]; then
sign_args=("-s")
fi
if [ -n "$keyid" ]; then
sign_args=("-u" "$keyid")
fi
# shellcheck disable=SC2086
git tag "${sign_args[@]}" $version -a -m "$msg" \
&& git push $remote --tags \
&& git push $remote \
&& hook post-release $hook_args \
&& echo "... complete"
else
echo "tag required" 1>&2 && exit 1
fi