forked from digitalocean/doctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.sh
executable file
·97 lines (74 loc) · 1.42 KB
/
version.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
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
# do NOT use pipefail in this script, as it will cause problems with the version
# line
set -e
me=$(basename "$0")
help_message="\
Usage: $me [<options>]
Display doctl version
Options:
-h, --help Show this help information.
-s, --short major.minor.patch only
-i, --image snap image version
-b, --branch branch only
-c, --commit commit only
"
semver() {
git tag -l | sort --version-sort | tail -n1 | cut -c 2-
}
branch() {
local branch
branch=$(git rev-parse --abbrev-ref HEAD)
if [[ $branch != 'main' && $branch != HEAD ]]; then
echo "${branch}"
fi
}
commit() {
if [[ $(git status --porcelain) != "" ]]; then
git rev-parse --short HEAD
fi
}
image() {
echo "$(semver)-$(commit)-pre"
}
ORIGIN=${ORIGIN:-origin}
set +e
git fetch --tags "${ORIGIN}" &>/dev/null
set -e
if [[ "$#" -eq 0 ]]; then
SNAP_IMAGE=${SNAP_IMAGE:-false}
if [[ "${SNAP_IMAGE}" != 'false' ]]; then
image
exit 0
fi
version=$(semver)
br=$(branch)
if [[ -n "$br" ]]; then
version="${version}-${br}"
fi
cm=$(commit)
if [[ -n "$cm" ]]; then
version="${version}-${cm}"
fi
echo "$version"
exit 0
fi
case "$1" in
"-b"|"--branch")
version=$(branch)
;;
"-c"|"--commit")
version=$(commit)
;;
"-s"|"--short")
version=$(semver)
;;
"-i"|"--image")
version=$(image)
;;
*)
echo "$help_message"
exit 0
;;
esac
echo "$version"