forked from fmahnke/shell-semver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbump-version-push-tag.sh
executable file
·89 lines (70 loc) · 1.74 KB
/
bump-version-push-tag.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SCRIPT_DIR/colors.sh
usage() {
echo ""
echo "usage: ./bump-version-push-tag.sh [major|minor|patch] [pathToGitRepo]"
echo ""
echo "For instance,"
echo ""
echo "./bump-version-push-tag.sh major"
echo ""
echo "(Case matters. 'major' is a valid argument. 'MAJOR' is not.)"
}
ensure_repo_not_dirty() {
local repo_path
repo_path=$1
git_description="$(git -C $repo_path describe --always --tags --dirty)"
if [[ $git_description =~ ^.*-dirty$ ]] ;
then
echo Repo is dirty. Please commit first.
exit 1
fi
}
fetch_tags() {
local repo_path
repo_path=$1
git -C $repo_path fetch --tags
}
get_latest_tag() {
local repo_path default_tag git_tag
repo_path=$1
default_tag=$2
git_tag=$(git -C ${repo_path} tag --sort=v:refname | tail -n 1)
if [ -z "$default_tag" ]; then
default_tag=v0.0.1
fi
if [ -z "$git_tag" ]; then
git_tag=$default_tag
fi
echo -n $git_tag
}
#get_incremented_tag() {
#}
if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]; then
usage
exit 1
fi
case "$1" in
major) semver_flag=-M
;;
minor) semver_flag=-m
;;
patch) semver_flag=-p
;;
*) usage; exit 1;;
esac
if [ -z "$2" ]; then
repo_path=$(realpath .)
else
repo_path=$(realpath "$2")
fi
ensure_repo_not_dirty "$repo_path"
fetch_tags "$repo_path"
current_tag=$(get_latest_tag "$repo_path" "v0.0.1")
echo "Latest tag is $current_tag"
new_tag=$($SCRIPT_DIR/semver/increment_version.sh $semver_flag $current_tag)
echo "New tag is $new_tag"
git -C $repo_path tag -a $new_tag -m "$new_tag"
git -C $repo_path push origin $new_tag
echo "Done"