Skip to content

Commit

Permalink
dts: avoid "git describe" without some "--exact-match"
Browse files Browse the repository at this point in the history
We currently use "git describe --always HEAD" to retrieve
HEAD information about the Git repository at ZEPHYR_BASE.

This permits to get, with a single Git command:
- either HEAD's tag, if HEAD is tagged
- or the most recent tag behind HEAD,
  postfixed with HEAD's short hash

This is not necessarily the best version string,
but more importantly it can be significantly slower
than executing successively the two commands bellow:

          git describe --exact-match --tags
          git rev-parse --short HEAD
  • Loading branch information
dottspina committed Aug 27, 2024
1 parent ccdef36 commit 3e1d076
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/dtsh/dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,21 @@ def toolchain_dir(self) -> Optional[str]:
return toolchain_dir

def get_zephyr_head(self) -> Optional[str]:
"""Retrieve Zephyr kernel version.
"""Retrieve Zephyr repository HEAD version.
Returns:
Suffixed most recent tag of the ZEPHYR_BASE repository
if Git is found in the current environment.
A version string with at least the short commit hash,
and a tag name if HEAD is tagged.
Depends on the git command availability.
"""
if self.zephyr_base:
git = GitUtil(self.zephyr_base)
if git.is_available:
return git.describe_head()
head_short = git.head_get_short()
head_tag = git.head_get_tag()
if head_tag:
return f"{head_tag} ({head_short})"
return head_short
return None

def _init_cmake_cache(self) -> Optional["CMakeCache"]:
Expand Down Expand Up @@ -1054,14 +1059,16 @@ def is_available(self) -> bool:
"""True when we should be able to execute Git commands."""
return self._enabled

def describe_head(self) -> Optional[str]:
"""Retrieve repository head (suffixed most recent tag).
def head_get_tag(self) -> Optional[str]:
"""Get the tag of the repository HEAD, if any."""
(ret, output) = self._git_exec(["describe", "--exact-match", "--tags"])
if ret == 0:
return output
return None

Returns:
The output of "git describe --always HEAD",
or None if the command failed.
"""
(ret, output) = self._git_exec(["describe", "--always", "HEAD"])
def head_get_short(self) -> Optional[str]:
"""Get the short commit hash of the repository HEAD."""
(ret, output) = self._git_exec(["rev-parse", "--short", "HEAD"])
if ret == 0:
return output
return None
Expand Down

0 comments on commit 3e1d076

Please sign in to comment.