scripts: zephyr_module now handles when unable to retrieve git rev

We now return "unknown" as the revision, and only concat onto
the revision if the revision is a valid git has value.

Signed-off-by: Andy Lee <andy@chiefmarley.com>
This commit is contained in:
Andy Lee 2025-05-01 05:32:26 -05:00 committed by Benjamin Cabé
commit 45746c172e

View file

@ -453,6 +453,13 @@ def process_twister(module, meta):
return out return out
def is_valid_git_revision(revision):
"""
Returns True if the given string is a valid git revision hash (40 hex digits).
"""
if not isinstance(revision, str):
return False
return bool(re.fullmatch(r'[0-9a-fA-F]{40}', revision))
def _create_meta_project(project_path): def _create_meta_project(project_path):
def git_revision(path): def git_revision(path):
@ -480,7 +487,7 @@ def _create_meta_project(project_path):
if rc: if rc:
return revision + '-dirty', True return revision + '-dirty', True
return revision, False return revision, False
return None, False return "unknown", False
def git_remote(path): def git_remote(path):
popen = subprocess.Popen(['git', 'remote'], popen = subprocess.Popen(['git', 'remote'],
@ -575,7 +582,7 @@ def process_meta(zephyr_base, west_projs, modules, extra_modules=None,
workspace_extra = extra_modules is not None workspace_extra = extra_modules is not None
workspace_off = zephyr_off workspace_off = zephyr_off
if zephyr_off: if zephyr_off and is_valid_git_revision(zephyr_project['revision']):
zephyr_project['revision'] += '-off' zephyr_project['revision'] += '-off'
meta['zephyr'] = zephyr_project meta['zephyr'] = zephyr_project
@ -607,7 +614,7 @@ def process_meta(zephyr_base, west_projs, modules, extra_modules=None,
manifest_project, manifest_dirty = _create_meta_project( manifest_project, manifest_dirty = _create_meta_project(
projects[0].posixpath) projects[0].posixpath)
manifest_off = manifest_project.get("remote") is None manifest_off = manifest_project.get("remote") is None
if manifest_off: if manifest_off and is_valid_git_revision(manifest_project['revision']):
manifest_project["revision"] += "-off" manifest_project["revision"] += "-off"
if manifest_project: if manifest_project:
@ -630,7 +637,8 @@ def process_meta(zephyr_base, west_projs, modules, extra_modules=None,
off = True off = True
if off: if off:
meta_project['revision'] += '-off' if is_valid_git_revision(meta_project['revision']):
meta_project['revision'] += '-off'
workspace_off |= off workspace_off |= off
# If manifest is in project, updates related variables # If manifest is in project, updates related variables
@ -667,22 +675,24 @@ def process_meta(zephyr_base, west_projs, modules, extra_modules=None,
if propagate_state: if propagate_state:
zephyr_revision = zephyr_project['revision'] zephyr_revision = zephyr_project['revision']
if workspace_dirty and not zephyr_dirty: if is_valid_git_revision(zephyr_revision):
zephyr_revision += '-dirty' if workspace_dirty and not zephyr_dirty:
if workspace_extra: zephyr_revision += '-dirty'
zephyr_revision += '-extra' if workspace_extra:
if workspace_off and not zephyr_off: zephyr_revision += '-extra'
zephyr_revision += '-off' if workspace_off and not zephyr_off:
zephyr_revision += '-off'
zephyr_project.update({'revision': zephyr_revision}) zephyr_project.update({'revision': zephyr_revision})
if west_projs is not None: if west_projs is not None:
manifest_revision = manifest_project['revision'] manifest_revision = manifest_project['revision']
if workspace_dirty and not manifest_dirty: if is_valid_git_revision(manifest_revision):
manifest_revision += '-dirty' if workspace_dirty and not manifest_dirty:
if workspace_extra: manifest_revision += '-dirty'
manifest_revision += '-extra' if workspace_extra:
if workspace_off and not manifest_off: manifest_revision += '-extra'
manifest_revision += '-off' if workspace_off and not manifest_off:
manifest_revision += '-off'
manifest_project.update({'revision': manifest_revision}) manifest_project.update({'revision': manifest_revision})
return meta return meta