Newer
Older
"""
Generate version based on the status of the local git checkout.
By default, a plain string will be emitted to stdout. Append command with `-c`
to generate a C source instead.
See docs/badge/firmware(-development).rst for information about version naming
and the release process.
"""
import subprocess
import sys
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
class Tag:
def __init__(self, name, rc):
self.name = name
self.rc = rc
def __repr__(self):
return self.name
def tags_for_commit(release, commit):
res = []
tags = (
subprocess.check_output(
[
"git",
"tag",
"--contains",
commit,
]
)
.decode()
.strip()
)
for tag in tags.split("\n"):
tag = tag.strip()
if not tag:
continue
if not tag.startswith("v" + release):
continue
if tag == "v" + release:
res.append(Tag(tag, False))
continue
if tag.startswith("v" + release + "+rc"):
res.append(Tag(tag, True))
continue
return res
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
commit = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
branches = (
subprocess.check_output(
[
"git",
"branch",
"--format",
"%(refname)",
"--contains",
commit,
]
)
.decode()
.strip()
)
release = None
for branch in branches.split("\n"):
branch = branch.strip()
if not branch:
continue
parts = branch.split("/")
if len(parts) != 4:
continue
if parts[:3] != ["refs", "heads", "release"]:
continue
v = parts[3]
release = v
break
main_count = (
subprocess.check_output(
[
"git",
"rev-list",
"--count",
commit,
]
)
.decode()
.strip()
)
version = None
if release is None:
version = f"v0-dev{main_count}"
return version
tags = tags_for_commit(release, commit)
if not tags:
return f"v{release}-dev{main_count}"
releases = sorted([t for t in tags if not t.rc])
candidates = sorted([t for t in tags if t.rc])
if releases:
return str(releases[0])
else:
return str(candidates[0])
fmt = None
if len(sys.argv) > 1:
if sys.argv[1] == "-c":
fmt = "C"
v = None
if os.environ.get('CI') is not None:
tag = os.environ.get('CI_COMMIT_TAG')
if tag is not None:
# If we're building a tag, just use that as a version.
v = tag
if v is None:
v = get_git_based_version()