Skip to content

Commit 79da80f

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

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
@@ -70,7 +70,7 @@ def main() -> None:
7070

7171
arch = args.arch
7272
flavor = None
73-
commit_id = args.commit
73+
commit_id_or_hash = args.commit
7474
gardenlinux_root = path.dirname(args.feature_dir)
7575
version = args.version
7676

@@ -82,7 +82,9 @@ def main() -> None:
8282

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

9597
if args.cname:
96-
cname = CName(args.cname, arch=arch, commit_id=commit_id, version=version)
98+
cname = CName(
99+
args.cname, arch=arch, commit_hash=commit_id_or_hash, version=version
100+
)
97101

98102
arch = cname.arch
99103
flavor = cname.flavor
100-
commit_id = cname.commit_id
104+
commit_id_or_hash = cname.commit_id
101105
version = cname.version
102106

103107
input_features = Parser.get_cname_as_feature_set(flavor)
@@ -142,8 +146,8 @@ def main() -> None:
142146
if arch is not None:
143147
cname += f"-{arch}"
144148

145-
if commit_id is not None:
146-
cname += f"-{version}-{commit_id}"
149+
if commit_id_or_hash is not None:
150+
cname += f"-{version}-{commit_id_or_hash[:8]}"
147151

148152
print(cname)
149153
elif args.type == "graph":
@@ -166,11 +170,11 @@ def main() -> None:
166170
elif args.type == "flags":
167171
print(",".join(features_by_type["flag"]))
168172
elif args.type == "commit_id":
169-
print(commit_id)
173+
print(commit_id_or_hash[:8])
170174
elif args.type == "version":
171175
print(version)
172176
elif args.type == "version_and_commit_id":
173-
print(f"{version}-{commit_id}")
177+
print(f"{version}-{commit_id_or_hash[:8]}")
174178

175179

176180
def get_cname_base(sorted_features: Set[str]):
@@ -198,21 +202,21 @@ def get_version_and_commit_id_from_files(gardenlinux_root: str) -> tuple[str, st
198202
:since: 0.7.0
199203
"""
200204

201-
commit_id = None
205+
commit_hash = None
202206
version = None
203207

204208
if os.access(path.join(gardenlinux_root, "COMMIT"), os.R_OK):
205209
with open(path.join(gardenlinux_root, "COMMIT"), "r") as fp:
206-
commit_id = fp.read().strip()[:8]
210+
commit_hash = fp.read().strip()[:8]
207211

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

212-
if commit_id is None or version is None:
216+
if commit_hash is None or version is None:
213217
raise RuntimeError("Failed to read version or commit ID from files")
214218

215-
return (version, commit_id)
219+
return (version, commit_hash)
216220

217221

218222
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
"""
@@ -166,7 +205,7 @@ def platforms(self) -> List[str]:
166205
Returns the platforms for the cname parsed.
167206
168207
:return: (str) Flavor
169-
:since: 0.9.2
208+
:since: 0.11.0
170209
"""
171210

172211
if self._platforms_cached is not None:
@@ -205,7 +244,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
205244
206245
:param metadata_file: Metadata file containing information about the CName instance.
207246
208-
:since: 0.9.2
247+
:since: 0.11.0
209248
"""
210249

211250
if not isinstance(metadata_file, PathLike):
@@ -233,6 +272,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
233272
)
234273

235274
commit_id = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID")
275+
commit_hash = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID_LONG")
236276
version = metadata_config.get(UNNAMED_SECTION, "GARDENLINUX_VERSION")
237277

238278
if (
@@ -248,6 +288,7 @@ def load_from_metadata_file(self, metadata_file: PathLike | str) -> None:
248288

249289
self._arch = loaded_cname_instance.arch
250290
self._flavor = loaded_cname_instance.flavor
291+
self._commit_hash = commit_hash
251292
self._commit_id = commit_id
252293
self._version = version
253294

@@ -267,7 +308,7 @@ def save_to_metadata_file(
267308
268309
:param metadata_file: Metadata file containing information about the CName instance.
269310
270-
:since: 0.9.2
311+
:since: 0.11.0
271312
"""
272313

273314
if not isinstance(metadata_file, PathLike):
@@ -300,7 +341,7 @@ def save_to_metadata_file(
300341
GARDENLINUX_FEATURES_FLAGS="{flags}"
301342
GARDENLINUX_VERSION="{self.version}"
302343
GARDENLINUX_COMMIT_ID="{self.commit_id}"
303-
GARDENLINUX_COMMIT_ID_LONG=$BUILDER_COMMIT
344+
GARDENLINUX_COMMIT_ID_LONG="{self.commit_hash}"
304345
""".strip()
305346

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

4848
arch = args.arch
49-
commit_id = args.commit
49+
commit_id_or_hash = args.commit
5050
gardenlinux_root = dirname(args.feature_dir)
5151
version = args.version
5252

@@ -55,15 +55,17 @@ def main():
5555

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

66-
cname = CName(args.cname, arch=arch, commit_id=commit_id, version=version)
68+
cname = CName(args.cname, arch=arch, commit_hash=commit_id_or_hash, version=version)
6769

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

0 commit comments

Comments
 (0)