@@ -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(
301342GARDENLINUX_FEATURES_FLAGS="{ flags } "
302343GARDENLINUX_VERSION="{ self .version } "
303344GARDENLINUX_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 :
0 commit comments