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