Skip to content

Commit dd70048

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

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

src/gardenlinux/features/__main__.py

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

7272
arch = args.arch
7373
flavor = None
74-
commit_id = args.commit
74+
commit_id_or_hash = args.commit
7575
gardenlinux_root = path.dirname(args.feature_dir)
7676
version = args.version
7777

@@ -83,7 +83,9 @@ def main() -> None:
8383

8484
if version is None or version == "":
8585
try:
86-
version, commit_id = get_version_and_commit_id_from_files(gardenlinux_root)
86+
version, commit_id_or_hash = get_version_and_commit_id_from_files(
87+
gardenlinux_root
88+
)
8789
except RuntimeError as exc:
8890
logging.debug(
8991
"Failed to parse version information for GL root '{0}': {1}".format(
@@ -94,11 +96,13 @@ def main() -> None:
9496
version = args.default_version
9597

9698
if args.cname:
97-
cname = CName(args.cname, arch=arch, commit_id=commit_id, version=version)
99+
cname = CName(
100+
args.cname, arch=arch, commit_hash=commit_id_or_hash, version=version
101+
)
98102

99103
arch = cname.arch
100104
flavor = cname.flavor
101-
commit_id = cname.commit_id
105+
commit_id_or_hash = cname.commit_id
102106
version = cname.version
103107

104108
input_features = Parser.get_cname_as_feature_set(flavor)
@@ -143,8 +147,8 @@ def main() -> None:
143147
if arch is not None:
144148
cname += f"-{arch}"
145149

146-
if commit_id is not None:
147-
cname += f"-{version}-{commit_id}"
150+
if commit_id_or_hash is not None:
151+
cname += f"-{version}-{commit_id_or_hash[:8]}"
148152

149153
print(cname)
150154
elif args.type == "graph":
@@ -167,11 +171,11 @@ def main() -> None:
167171
elif args.type == "flags":
168172
print(",".join(features_by_type["flag"]))
169173
elif args.type == "commit_id":
170-
print(commit_id)
174+
print(commit_id_or_hash[:8])
171175
elif args.type == "version":
172176
print(version)
173177
elif args.type == "version_and_commit_id":
174-
print(f"{version}-{commit_id}")
178+
print(f"{version}-{commit_id_or_hash[:8]}")
175179

176180

177181
def get_cname_base(sorted_features: Set[str]):
@@ -199,21 +203,21 @@ def get_version_and_commit_id_from_files(gardenlinux_root: str) -> tuple[str, st
199203
:since: 0.7.0
200204
"""
201205

202-
commit_id = None
206+
commit_hash = None
203207
version = None
204208

205209
if os.access(path.join(gardenlinux_root, "COMMIT"), os.R_OK):
206210
with open(path.join(gardenlinux_root, "COMMIT"), "r") as fp:
207-
commit_id = fp.read().strip()[:8]
211+
commit_hash = fp.read().strip()[:8]
208212

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

213-
if commit_id is None or version is None:
217+
if commit_hash is None or version is None:
214218
raise RuntimeError("Failed to read version or commit ID from files")
215219

216-
return (version, commit_id)
220+
return (version, commit_hash)
217221

218222

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

src/gardenlinux/features/cname.py

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

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

4343
self._arch = None
44+
self._commit_hash = None
4445
self._commit_id = None
4546
self._feature_set_cached = None
4647
self._flavor = None
4748
self._version = None
4849
self._platforms_cached = None
4950

51+
commit_id_or_hash = None
52+
5053
re_match = re.match(
5154
"([a-zA-Z0-9]+([\\_\\-][a-zA-Z0-9]+)*?)(-([a-z0-9]+)(-([a-z0-9.]+)-([a-z0-9]+))*)?$",
5255
cname,
@@ -57,7 +60,7 @@ def __init__(self, cname, arch=None, commit_id=None, version=None):
5760
if re_match.lastindex == 1:
5861
self._flavor = re_match[1]
5962
else:
60-
self._commit_id = re_match[7]
63+
commit_id_or_hash = re_match[7]
6164
self._flavor = re_match[1]
6265
self._version = re_match[6]
6366

@@ -70,17 +73,23 @@ def __init__(self, cname, arch=None, commit_id=None, version=None):
7073
self._arch = arch
7174

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

78-
self._commit_id = re_match[3]
81+
commit_id_or_hash = re_match[3]
7982
self._version = re_match[1]
8083
else:
81-
self._commit_id = commit_id
84+
commit_id_or_hash = commit_hash
8285
self._version = version
8386

87+
if commit_id_or_hash is not None:
88+
self._commit_id = commit_id_or_hash[:8]
89+
90+
if len(commit_id_or_hash) == 40: # sha1 hex
91+
self._commit_hash = commit_id_or_hash
92+
8493
@property
8594
def arch(self) -> Optional[str]:
8695
"""
@@ -111,6 +120,33 @@ def cname(self) -> str:
111120

112121
return cname
113122

123+
@property
124+
def commit_hash(self) -> str:
125+
"""
126+
Returns the commit hash if part of the cname parsed.
127+
128+
:return: (str) Commit hash
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+
144+
if self._commit_id is not None and not commit_hash.startswith(self._commit_id):
145+
raise RuntimeError("Commit hash given differs from commit ID already set")
146+
147+
self._commit_id = commit_hash[:8]
148+
self._commit_hash = commit_hash
149+
114150
@property
115151
def commit_id(self) -> Optional[str]:
116152
"""
@@ -234,6 +270,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
234270
)
235271

236272
commit_id = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID")
273+
commit_hash = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID_LONG")
237274
version = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_VERSION")
238275

239276
if (
@@ -249,6 +286,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
249286

250287
self._arch = loaded_cname_instance.arch
251288
self._flavor = loaded_cname_instance.flavor
289+
self._commit_hash = commit_hash
252290
self._commit_id = commit_id
253291
self._version = version
254292

@@ -301,7 +339,7 @@ def save_to_metadata_file(
301339
GARDENLINUX_FEATURES_FLAGS="{flags}"
302340
GARDENLINUX_VERSION="{self.version}"
303341
GARDENLINUX_COMMIT_ID="{self.commit_id}"
304-
GARDENLINUX_COMMIT_ID_LONG=$BUILDER_COMMIT
342+
GARDENLINUX_COMMIT_ID_LONG="{self.commit_hash}"
305343
""".strip()
306344

307345
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
@@ -47,7 +47,7 @@ def main():
4747
assert re_match, f"Not a valid GardenLinux canonical name {args.cname}"
4848

4949
arch = args.arch
50-
commit_id = args.commit
50+
commit_id_or_hash = args.commit
5151
gardenlinux_root = dirname(args.feature_dir)
5252
version = args.version
5353

@@ -56,15 +56,17 @@ def main():
5656

5757
if not version:
5858
try:
59-
version, commit_id = get_version_and_commit_id_from_files(gardenlinux_root)
59+
version, commit_id_or_hash = get_version_and_commit_id_from_files(
60+
gardenlinux_root
61+
)
6062
except RuntimeError as exc:
6163
logging.warning(
6264
"Failed to parse version information for GL root '{0}': {1}".format(
6365
gardenlinux_root, exc
6466
)
6567
)
6668

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

6971
assert cname.arch, "Architecture could not be determined"
7072

0 commit comments

Comments
 (0)