-
Notifications
You must be signed in to change notification settings - Fork 5
/
bump.sh
executable file
·67 lines (60 loc) · 1.77 KB
/
bump.sh
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
#!/bin/bash
# Bumps the semantic version of the git-project.
# If no semantic version tags exist in the project, the version starts out at v0.0.0
# and is incremented by one for the field indicated by the bump command argument.
find_latest_semver() {
pattern="^$PREFIX([0-9]+\.[0-9]+\.[0-9]+)\$"
versions=$(for tag in $(git tag); do
[[ "$tag" =~ $pattern ]] && echo "${BASH_REMATCH[1]}"
done)
if [ -z "$versions" ];then
echo 0.0.0
else
echo "$versions" | tr '.' ' ' | sort -nr -k 1 -k 2 -k 3 | tr ' ' '.' | head -1
fi
}
increment_ver() {
find_latest_semver | awk -F. -v a="$1" -v b="$2" -v c="$3" \
'{printf("%d.%d.%d", $1+a, $2+b , $3+c)}'
}
bump() {
next_ver="${PREFIX}$(increment_ver "$1" "$2" "$3")"
latest_ver="${PREFIX}$(find_latest_semver)"
latest_commit=$(git rev-list -n 1 "${latest_ver}" 2>/dev/null )
head_commit=$(git rev-parse HEAD)
if [ "$latest_commit" == "$head_commit" ]; then
echo "refusing to tag; $latest_ver already tagged for HEAD ($head_commit)" >&2
else
echo "tagging $next_ver $head_commit" >&2
git tag -a "$next_ver" "$head_commit" -m "$next_ver"
echo "$next_ver"
fi
}
usage() {
echo "Usage: bump [-p prefix] {major|minor|patch} | -l"
echo "Bumps the semantic version field by one for a git-project."
echo
echo "Options:"
echo " -l list the latest tagged version instead of bumping."
echo " -p prefix [to be] used for the semver tags."
exit 1
}
while getopts :p:l opt; do
case $opt in
p) PREFIX="$OPTARG";;
l) LIST=1;;
\?) usage;;
:) echo "option -$OPTARG requires an argument"; exit 1;;
esac
done
shift $((OPTIND-1))
if [ -n "$LIST" ];then
find_latest_semver
exit 0
fi
case $1 in
major) bump 1 0 0;;
minor) bump 0 1 0;;
patch) bump 0 0 1;;
*) usage
esac