-
Notifications
You must be signed in to change notification settings - Fork 26
/
GENVER
executable file
·130 lines (115 loc) · 4.09 KB
/
GENVER
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
#!/bin/bash
# GENVER returns a semantic version of the current branch.
#
# It generates the semantic version based on the tag references on
# the repo.
#
# Args:
# Can pass "--pep440" to generate PEP-440 compliant version
#
# Assumptions:
# - Will match only tags matching "v[0-9]*" (e.g., v0.0.1)
# - Repo owners tag accordingly per release
#
# Example:
#
# 1. If the latest tag (v0.0.1) is at HEAD~4 and HEAD is at commit 60f987bf,
# running
#
# ./GENVER --pep440
#
# will return: 0.0.1.dev4+g60f987bfd
#
# NOTE: PyPI will not allow these local versions to be pushed: "Can't use
# PEP 440 local versions. See https://packaging.python.org/specifications/core-metadata
# for more information."
#
# 2. If "--pep440" is not passed into example (1), it would return 0.0.1-4.g60f987bfd
#
# 3. If the latest tag (v0.0.2) is at HEAD of branch (60f987bf), it'll
# treat it as a released state, so running:
#
# ./GENVER --pep440
#
# will return: 0.0.2
#
set -e
# Generates a PEP 440 compatible version instead of a semantic version.
if test "$1" = "--pep440"; then
GENVER_PEP440="true"
fi
# Dependency checks.
dependencies="cat expr git"
for cmd in ${dependencies}; do
if ! command -v "${cmd}" >/dev/null 2>&1; then
>&2 echo "Command ${cmd} is unavailable."
exit 1
fi
done
# Validate the candidate version.
ROOT=$(git rev-parse --show-toplevel)
VERSION_FILE="${ROOT}/VERSION"
if test -f "${VERSION_FILE}"; then
can_ver=$(cat "${VERSION_FILE}")
else
>&2 echo "Error: Failed to find the VERSION file, please refer to RELEASE.md" \
"as we reference VERSION file for the base candidate version."
exit 1
fi
# Get the "git describe" version.
# Normal case, the versioning tags looks like "v1.0.0", "v1.0.1",...
if ! git_ver=$(git describe --tags --abbrev=9 --match "v[0-9]*" HEAD 2>/dev/null)
then
>&2 echo "Error: Can't get any versioning tag. Are tags pushed?"
fi
# Strip the leading 'v' from the git describe version.
git_ver=$(expr "${git_ver}" : v*'\(.*\)')
if expr "${git_ver}" : '.*-[0-9]*-g.*' >/dev/null
then
# Branch is in the developing state (e.g., there have been commits
# since the last tag).
# Extract each part of the "git describe" version.
tag_ver=$(expr "$git_ver" : '\(.*\)-[0-9]*-g.*')
commit_count=$(expr "$git_ver" : '.*-\([0-9]*\)-g.*')
commit_hash=$(expr "$git_ver" : '.*-[0-9]*-\(g.*\)')
if test "$can_ver" = "$tag_ver"
then
>&2 echo "Error: The candidate version on this branch is still at" \
"what has been released. You can pull in latest VERSION file" \
"to be up-to-date"
exit 1
fi
if test "${GENVER_PEP440:-}" = "true"
then
sem_ver="${tag_ver}.dev${commit_count}+${commit_hash}"
else
# Generate the semantic version for pre-release.
# Case 1 (candidate version contains hyphen):
# candidate version(VERSION file) : 1.0.0-p1
# ========
# git describe returns : 1.0.0-2-gde2198c
# ~~~~~~~~~~~
# semantic version(generated) : 1.0.0-p1.2.gde2198c
# ========~~~~~~~~~~~
# ^ ^
# Case 2:
# candidate version(VERSION file) : 1.0.1
# =====
# git describe returns : 1.0.0-2-gde2198c
# ~~~~~~~~~~~
# semantic version(generated) : 1.0.1-2.gde2198c
# =====~~~~~~~~~~~
# ^
if expr "$tag_ver" : ".*-" >/dev/null
then
sem_ver="${tag_ver}.${commit_count}.${commit_hash}"
else
sem_ver="${tag_ver}-${commit_count}.${commit_hash}"
fi
fi
else
# Branch is in the released state.
# Use the version taken from tag as-is
sem_ver="${git_ver}"
fi
echo "${sem_ver}"