Skip to content

Commit

Permalink
Log hash of the beakerlib library repo
Browse files Browse the repository at this point in the history
Make common tmt.utils.git_hash function to have single way of getting
this information
  • Loading branch information
lukaszachy authored and psss committed Apr 30, 2024
1 parent 159b37c commit 4f2caaa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
9 changes: 8 additions & 1 deletion tmt/libraries/beakerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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('/')
Expand Down
14 changes: 7 additions & 7 deletions tmt/steps/discover/fmf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import dataclasses
import glob
import os
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tmt/steps/discover/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4f2caaa

Please sign in to comment.