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