-
Notifications
You must be signed in to change notification settings - Fork 7
/
autotag
executable file
·156 lines (134 loc) · 3.43 KB
/
autotag
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
#!/usr/bin/env bash
# AutoTag application.
#
# MIT License
#
# Copyright (c) 2020 - 2021 MichaelCurrin
#
# updated by f@pulilab.com on 06.12.2021
#
# https://github.com/MichaelCurrin/auto-tag/blob/master/LICENSE
set -e
FALLBACK_TAG='0.0.0'
USAGE='USAGE:
autotag LEVEL [-p] [-u] [-h]
'
HELP="HELP:
Increment git tag using given increment level.
Positional arguments:
LEVEL : 'M' for major, 'm' for minor or 'b' for bug.
Flags:
-h --help : Show help and exit.
-p --preview : Do a dry run to show the new tag label only, without creating it.
This must be used as the 2nd arg i.e. after the LEVEL.
-u --upgrade : Download latest script from GitHub, write over current script to
install it, then exit. Uses cURL and not Git. If using this flag,
not other options are needed or used.
"
USER_ARGS="$*"
DOWNLOAD_URL='https://raw.githubusercontent.com/MichaelCurrin/auto-tag/master/autotag'
# Dynamic variables. Unfortunately all are global but at least there are functions now so the script
# is easier to work with. Also they don't have to be set here even, but are set for clarity.
LEVEL_CHOICE=''
PREVIEW=''
MAJOR=''
MINOR=''
BUG=''
LAST_TAG=''
NEW_TAG=''
help_if_needed() {
if [[ "$#" -eq 0 ]] || [[ "$1" == '-h' ]] || [[ "$1" == '--help' ]]; then
echo "$USAGE"
echo "$HELP"
exit 1
fi
}
invalid_args_error() {
echo "🛑 Invalid arguments: '$USER_ARGS'"
echo
echo "$USAGE"
exit 1
}
download_update_if_needed() {
if [[ "$#" -eq 0 ]] || [[ "$1" == '-u' ]] || [[ "$1" == '--upgrade' ]]; then
SCRIPT_FILEPATH="$(realpath $0)"
echo 'ℹ️ Current path: $SCRIPT_FILEPATH'
echo '⬇️ Downloading latest script using curl'
curl -q "$DOWNLOAD_URL" >"$SCRIPT_FILEPATH"
echo '🚀 `autotag` script updated. Exiting.'
exit 0
fi
}
process_args() {
LEVEL_CHOICE="$1"
if [[ "$2" ]]; then
if [[ "$2" == '-p' ]] || [[ "$2" == '--preview' ]]; then
PREVIEW='true'
else
invalid_args_error
fi
else
PREVIEW='false'
fi
}
get_last_tag() {
LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null)
LAST_TAG="${LAST_TAG:-$FALLBACK_TAG}"
LAST_TAG="${LAST_TAG/v/}"
# Replace dot with space then split into array.
LAST_TAG_ARR=(${LAST_TAG//./ })
MAJOR="${LAST_TAG_ARR[0]}"
MINOR="${LAST_TAG_ARR[1]}"
BUG="${LAST_TAG_ARR[2]}"
}
set_level() {
# Although the exit only happens after fetching, this needs to happen here so variables are set.
# Otherwise a refactor is needed to check M|m|b and exit if needed, then actually calculate here.
case "$LEVEL_CHOICE" in
"M")
((MAJOR += 1))
MINOR=0
BUG=0
;;
"m")
((MINOR += 1))
BUG=0
;;
"b")
((BUG += 1))
;;
*)
invalid_args_error
;;
esac
}
make_tag() {
git tag \
-a "$NEW_TAG" \
-m "$NEW_TAG"
}
run() {
echo '🚛 Fetching tags...'
git fetch --tags
echo '🔍 Finding most recent tag...'
get_last_tag
echo "👴 Last tag: $MAJOR.$MINOR.$BUG"
set_level
NEW_TAG="$MAJOR.$MINOR.$BUG"
echo "⭐ New tag: $NEW_TAG"
# For some reason these emojis need a double space after to avoid looking squashed, at least on
# macOS.
if [[ "$PREVIEW" == true ]]; then
echo '⏭️ Skipping tag creation'
else
echo '🏷️ Creating annotated tag...'
make_tag
fi
}
main() {
help_if_needed $@
download_update_if_needed $@
process_args $@
run
}
main $@