From 0995849c0e52200bbe804d631ba38f604fa0740c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zachar?= Date: Wed, 10 Apr 2024 18:05:16 +0200 Subject: [PATCH] Log hash of the beakerlib library repo Make common tmt.utils.git_hash function to have single way of getting this information --- tmt/libraries/beakerlib.py | 9 ++++++++- tmt/steps/discover/fmf.py | 14 +++++++------- tmt/steps/discover/shell.py | 6 ++++++ tmt/utils.py | 20 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/tmt/libraries/beakerlib.py b/tmt/libraries/beakerlib.py index 3be2ae4b64..2d29dd0249 100644 --- a/tmt/libraries/beakerlib.py +++ b/tmt/libraries/beakerlib.py @@ -220,13 +220,16 @@ def fetch(self) -> None: raise LibraryError with TemporaryDirectory() as tmp: assert self.url is not None # narrow type + destination = Path(tmp) try: tmt.utils.git_clone( url=self.url, - destination=Path(tmp), + destination=destination, shallow=True, env=Environment({"GIT_ASKPASS": EnvVarValue("echo")}), logger=self._logger) + self.parent.debug('hash', tmt.utils.git_hash( + directory=destination, logger=self._logger)) except (tmt.utils.RunError, tmt.utils.RetryError): self.parent.debug(f"Repository '{self.url}' not found.") self._nonexistent_url.add(self.url) @@ -313,6 +316,10 @@ def fetch(self) -> None: f"Reference '{self.ref}' for library '{self}' not found.") raise + # Log what HEAD really is + self.parent.debug('hash', tmt.utils.git_hash( + directory=clone_dir, logger=self._logger)) + # Copy only the required library library_path: Path = clone_dir / str(self.fmf_node_path).strip('/') local_library_path: Path = directory / str(self.fmf_node_path).strip('/') diff --git a/tmt/steps/discover/fmf.py b/tmt/steps/discover/fmf.py index 97a03045ea..a09c47561d 100644 --- a/tmt/steps/discover/fmf.py +++ b/tmt/steps/discover/fmf.py @@ -1,3 +1,4 @@ +import contextlib import dataclasses import glob import os @@ -431,13 +432,12 @@ def assert_git_url(plan_name: Optional[str] = None) -> None: # Show current commit hash if inside a git repository if self.testdir.is_dir(): - try: - output = self.run(Command("git", "rev-parse", "--short", "HEAD"), - cwd=self.testdir) - if output.stdout is not None: - self.verbose('hash', output.stdout.strip(), 'green') - except (tmt.utils.RunError, AttributeError): - pass + with contextlib.suppress(tmt.utils.RunError, AttributeError): + self.verbose( + 'hash', + tmt.utils.git_hash(directory=self.testdir, logger=self._logger), + 'green' + ) # Dist-git source processing during discover step if dist_git_source: diff --git a/tmt/steps/discover/shell.py b/tmt/steps/discover/shell.py index 0961ed6917..0f7dda52b0 100644 --- a/tmt/steps/discover/shell.py +++ b/tmt/steps/discover/shell.py @@ -308,6 +308,12 @@ def fetch_remote_repository( self.debug(f"Checkout ref '{ref}'.") self.run(Command('git', 'checkout', '-f', ref), cwd=testdir) + # Log where HEAD leads to + self.debug('hash', tmt.utils.git_hash( + directory=testdir, + logger=self._logger + )) + # Remove .git so that it's not copied to the SUT # if 'keep-git-metadata' option is not specified if not keep_git_metadata: diff --git a/tmt/utils.py b/tmt/utils.py index 49783490ba..b7028fbee7 100644 --- a/tmt/utils.py +++ b/tmt/utils.py @@ -4426,6 +4426,26 @@ def remove_color(text: str) -> str: return re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', text) +def git_hash(*, directory: Path, logger: tmt.log.Logger) -> Optional[str]: + """ + Return short hash of current HEAD in the git repo in directory. + + :param directory: path to a local git repository. + :param logger: used for logging. + :returns: short hash as string + """ + cmd = Command("git", "rev-parse", "--short", "HEAD") + result = cmd.run(cwd=directory, logger=logger) + + if result.stdout is None: + raise RunError(message="No output from 'git' when looking for the hash of HEAD.", + command=cmd, + returncode=0, + stderr=result.stderr) + + return result.stdout.strip() + + def git_root(*, fmf_root: Path, logger: tmt.log.Logger) -> Optional[Path]: """ Find a path to the root of git repository containing an fmf root.