Skip to content

Commit 760562a

Browse files
committed
Add support to provide the full commit hash in gardenlinux.features.CName
Signed-off-by: Tobias Wolf <[email protected]>
1 parent 283aaf8 commit 760562a

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

src/gardenlinux/features/__main__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def main() -> None:
6868

6969
arch = args.arch
7070
flavor = None
71-
commit_id = args.commit
71+
commit_id_or_hash = args.commit
7272
gardenlinux_root = path.dirname(args.feature_dir)
7373
version = args.version
7474

@@ -80,7 +80,9 @@ def main() -> None:
8080

8181
if version is None or version == "":
8282
try:
83-
version, commit_id = get_version_and_commit_id_from_files(gardenlinux_root)
83+
version, commit_id_or_hash = get_version_and_commit_id_from_files(
84+
gardenlinux_root
85+
)
8486
except RuntimeError as exc:
8587
logging.debug(
8688
"Failed to parse version information for GL root '{0}': {1}".format(
@@ -91,11 +93,13 @@ def main() -> None:
9193
version = args.default_version
9294

9395
if args.cname:
94-
cname = CName(args.cname, arch=arch, commit_id=commit_id, version=version)
96+
cname = CName(
97+
args.cname, arch=arch, commit_hash=commit_id_or_hash, version=version
98+
)
9599

96100
arch = cname.arch
97101
flavor = cname.flavor
98-
commit_id = cname.commit_id
102+
commit_id_or_hash = cname.commit_id
99103
version = cname.version
100104

101105
_ = Parser.get_cname_as_feature_set(flavor)
@@ -140,8 +144,8 @@ def main() -> None:
140144
if arch is not None:
141145
cname += f"-{arch}" # type: ignore - None check is carried out.
142146

143-
if commit_id is not None:
144-
cname += f"-{version}-{commit_id}" # type: ignore - None check is carried out.
147+
if commit_id_or_hash is not None:
148+
cname += f"-{version}-{commit_id_or_hash[:8]}" # type: ignore - None check is carried out.
145149

146150
print(cname)
147151
elif args.type == "graph":
@@ -164,11 +168,11 @@ def main() -> None:
164168
elif args.type == "flags":
165169
print(",".join(features_by_type["flag"]))
166170
elif args.type == "commit_id":
167-
print(commit_id)
171+
print(commit_id_or_hash[:8])
168172
elif args.type == "version":
169173
print(version)
170174
elif args.type == "version_and_commit_id":
171-
print(f"{version}-{commit_id}")
175+
print(f"{version}-{commit_id_or_hash[:8]}")
172176

173177

174178
def get_cname_base(sorted_features: List[str]):
@@ -196,21 +200,21 @@ def get_version_and_commit_id_from_files(gardenlinux_root: str) -> tuple[str, st
196200
:since: 0.7.0
197201
"""
198202

199-
commit_id = None
203+
commit_hash = None
200204
version = None
201205

202206
if os.access(path.join(gardenlinux_root, "COMMIT"), os.R_OK):
203207
with open(path.join(gardenlinux_root, "COMMIT"), "r") as fp:
204-
commit_id = fp.read().strip()[:8]
208+
commit_hash = fp.read().strip()[:8]
205209

206210
if os.access(path.join(gardenlinux_root, "VERSION"), os.R_OK):
207211
with open(path.join(gardenlinux_root, "VERSION"), "r") as fp:
208212
version = fp.read().strip()
209213

210-
if commit_id is None or version is None:
214+
if commit_hash is None or version is None:
211215
raise RuntimeError("Failed to read version or commit ID from files")
212216

213-
return (version, commit_id)
217+
return (version, commit_hash)
214218

215219

216220
def get_minimal_feature_set(graph: Any) -> Set[str]:

src/gardenlinux/features/cname.py

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,28 @@ class CName(object):
2727
Apache License, Version 2.0
2828
"""
2929

30-
def __init__(self, cname, arch=None, commit_id=None, version=None):
30+
def __init__(self, cname, arch=None, commit_hash=None, version=None):
3131
"""
3232
Constructor __init__(CName)
3333
3434
:param cname: Canonical name to represent
3535
:param arch: Architecture if not part of cname
36-
:param commit_id: Commit ID if not part of cname
36+
:param commit_hash: Commit ID or hash if not part of cname
3737
:param version: Version if not part of cname
3838
3939
:since: 0.7.0
4040
"""
4141

4242
self._arch = None
43+
self._commit_hash = None
4344
self._commit_id = None
4445
self._feature_set_cached = None
4546
self._flavor = None
4647
self._version = None
4748
self._platforms_cached = None
4849

50+
commit_id_or_hash = None
51+
4952
re_match = re.match(
5053
"([a-zA-Z0-9]+([\\_\\-][a-zA-Z0-9]+)*?)(-([a-z0-9]+)(-([a-z0-9.]+)-([a-z0-9]+))*)?$",
5154
cname,
@@ -56,7 +59,7 @@ def __init__(self, cname, arch=None, commit_id=None, version=None):
5659
if re_match.lastindex == 1:
5760
self._flavor = re_match[1]
5861
else:
59-
self._commit_id = re_match[7]
62+
commit_id_or_hash = re_match[7]
6063
self._flavor = re_match[1]
6164
self._version = re_match[6]
6265

@@ -69,17 +72,23 @@ def __init__(self, cname, arch=None, commit_id=None, version=None):
6972
self._arch = arch
7073

7174
if self._version is None and version is not None:
72-
# Support version values formatted as <version>-<commit_id>
73-
if commit_id is None:
75+
# Support version values formatted as <version>-<commit_hash>
76+
if commit_hash is None:
7477
re_match = re.match("([a-z0-9.]+)(-([a-z0-9]+))?$", version)
7578
assert re_match, f"Not a valid version {version}"
7679

77-
self._commit_id = re_match[3]
80+
commit_id_or_hash = re_match[3]
7881
self._version = re_match[1]
7982
else:
80-
self._commit_id = commit_id
83+
commit_id_or_hash = commit_hash
8184
self._version = version
8285

86+
if commit_id_or_hash is not None:
87+
self._commit_id = commit_id_or_hash[:8]
88+
89+
if len(commit_id_or_hash) == 40: # sha1 hex
90+
self._commit_hash = commit_id_or_hash
91+
8392
@property
8493
def arch(self) -> Optional[str]:
8594
"""
@@ -110,6 +119,36 @@ def cname(self) -> str:
110119

111120
return cname
112121

122+
@property
123+
def commit_hash(self) -> str:
124+
"""
125+
Returns the commit hash if part of the cname parsed.
126+
127+
:return: (str) Commit hash
128+
:since: 0.11.0
129+
"""
130+
131+
if self._commit_hash is None:
132+
raise RuntimeError("GardenLinux canonical name given does not contain the commit hash")
133+
134+
return self._commit_hash
135+
136+
@commit_hash.setter
137+
def commit_hash(self, commit_hash) -> None:
138+
"""
139+
Sets the commit hash
140+
141+
:param commit_hash: Commit hash
142+
143+
:since: 0.11.0
144+
"""
145+
146+
if self._commit_id is not None and not commit_hash.startswith(self._commit_id):
147+
raise RuntimeError("Commit hash given differs from commit ID already set")
148+
149+
self._commit_id = commit_hash[:8]
150+
self._commit_hash = commit_hash
151+
113152
@property
114153
def commit_id(self) -> Optional[str]:
115154
"""
@@ -167,7 +206,7 @@ def platforms(self) -> List[str]:
167206
Returns the platforms for the cname parsed.
168207
169208
:return: (str) Flavor
170-
:since: 0.9.2
209+
:since: 0.11.0
171210
"""
172211

173212
if self._platforms_cached is not None:
@@ -206,7 +245,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
206245
207246
:param metadata_file: Metadata file containing information about the CName instance.
208247
209-
:since: 0.9.2
248+
:since: 0.11.0
210249
"""
211250

212251
if not isinstance(metadata_file, PathLike):
@@ -234,6 +273,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
234273
)
235274

236275
commit_id = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID")
276+
commit_hash = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID_LONG")
237277
version = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_VERSION")
238278

239279
if (
@@ -249,6 +289,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
249289

250290
self._arch = loaded_cname_instance.arch
251291
self._flavor = loaded_cname_instance.flavor
292+
self._commit_hash = commit_hash
252293
self._commit_id = commit_id
253294
self._version = version
254295

@@ -268,7 +309,7 @@ def save_to_metadata_file(
268309
269310
:param metadata_file: Metadata file containing information about the CName instance.
270311
271-
:since: 0.9.2
312+
:since: 0.11.0
272313
"""
273314

274315
if not isinstance(metadata_file, PathLike):
@@ -301,7 +342,7 @@ def save_to_metadata_file(
301342
GARDENLINUX_FEATURES_FLAGS="{flags}"
302343
GARDENLINUX_VERSION="{self.version}"
303344
GARDENLINUX_COMMIT_ID="{self.commit_id}"
304-
GARDENLINUX_COMMIT_ID_LONG=$BUILDER_COMMIT
345+
GARDENLINUX_COMMIT_ID_LONG="{self.commit_hash}"
305346
""".strip()
306347

307348
with metadata_file.open("w") as fp:

src/gardenlinux/features/cname_main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def main():
4545
assert re_match, f"Not a valid GardenLinux canonical name {args.cname}"
4646

4747
arch = args.arch
48-
commit_id = args.commit
48+
commit_id_or_hash = args.commit
4949
gardenlinux_root = dirname(args.feature_dir)
5050
version = args.version
5151

@@ -54,15 +54,17 @@ def main():
5454

5555
if not version:
5656
try:
57-
version, commit_id = get_version_and_commit_id_from_files(gardenlinux_root)
57+
version, commit_id_or_hash = get_version_and_commit_id_from_files(
58+
gardenlinux_root
59+
)
5860
except RuntimeError as exc:
5961
logging.warning(
6062
"Failed to parse version information for GL root '{0}': {1}".format(
6163
gardenlinux_root, exc
6264
)
6365
)
6466

65-
cname = CName(args.cname, arch=arch, commit_id=commit_id, version=version)
67+
cname = CName(args.cname, arch=arch, commit_hash=commit_id_or_hash, version=version)
6668

6769
assert cname.arch, "Architecture could not be determined"
6870

0 commit comments

Comments
 (0)