Skip to content

Commit

Permalink
squash: aligned with tmt codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
happz committed Jul 31, 2024
1 parent 5287f6b commit b7e8759
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
40 changes: 31 additions & 9 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import os
import re
import shutil
import subprocess
import sys
import tempfile
import time
Expand Down Expand Up @@ -1797,7 +1796,7 @@ def _initialize_worktree(self) -> None:
# Prepare worktree path and detect the source tree root
assert self.workdir is not None # narrow type
self.worktree = self.workdir / 'tree'
tree_root = self.node.root
tree_root = Path(self.node.root) if self.node.root else None

# Create an empty directory if there's no metadata tree
if not tree_root:
Expand All @@ -1807,14 +1806,37 @@ def _initialize_worktree(self) -> None:

# Sync metadata root to the worktree
self.debug(f"Sync the worktree to '{self.worktree}'.", level=2)
excludes_tempfile = tempfile.NamedTemporaryFile()

ignore: list[Path] = [
Path('.git')
]

# If we're in a git repository, honor .gitignore; xref
# https://stackoverflow.com/questions/13713101/rsync-exclude-according-to-gitignore-hgignore-svnignore-like-filter-c
if os.path.isdir(f"{tree_root}/.git"):
subprocess.check_call(["git", "ls-files", "--exclude-standard", "-oi", "--directory"], stdout=excludes_tempfile)
# Note: rsync doesn't use reflinks right now, so in the future it'd be even better to
# use e.g. `cp` but filtering out the above.
self.run(Command("rsync", "-ar", "--exclude", ".git", "--exclude-from", excludes_tempfile.name, f"{tree_root}/", self.worktree))
# https://stackoverflow.com/questions/13713101/rsync-exclude-according-to-gitignore-hgignore-svnignore-like-filter-c # noqa: E501
git_root = tmt.utils.git_root(fmf_root=tree_root, logger=self._logger)
if git_root:
ignore.extend(tmt.utils.git_ignore(root=git_root, logger=self._logger))

self.debug(
"Ignoring the following paths during worktree sync",
tmt.utils.format_value(ignore),
level=4)

with tempfile.NamedTemporaryFile(mode='w') as excludes_tempfile:
excludes_tempfile.write('\n'.join(str(path) for path in ignore))

# Make sure ignored paths are saved before telling rsync to use them.
# With Python 3.12, we could use `delete_on_false=False` and call `close()`.
excludes_tempfile.flush()
os.fsync(excludes_tempfile.fileno())

This comment has been minimized.

Copy link
@cgwalters

cgwalters Jul 31, 2024

Contributor

fsync is way overkill for this, the flush should be sufficient

This comment has been minimized.

Copy link
@martinhoyer

martinhoyer Jul 31, 2024

Collaborator

+1
Perhaps it could also be NamedTemporaryFile(mode='w', delete=False) and

    excludes_path = excludes_file.name
try:
    self.run(<rsync>)
finally:
    Path(excludes_path).unlink()

This comment has been minimized.

Copy link
@happz

happz Jul 31, 2024

Author Collaborator

fsync dropped in 8beaa2b, I'm keeping the rest as it is.


# Note: rsync doesn't use reflinks right now, so in the future it'd be even better to
# use e.g. `cp` but filtering out the above.
self.run(Command(
"rsync",
"-ar",
"--exclude-from", excludes_tempfile.name,
f"{tree_root}/", self.worktree))

def _initialize_data_directory(self) -> None:
"""
Expand Down
25 changes: 25 additions & 0 deletions tmt/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4524,6 +4524,31 @@ def git_add(*, path: Path, logger: tmt.log.Logger) -> None:
raise GeneralError(f"Failed to add path '{path}' to git index.") from error


def git_ignore(*, root: Path, logger: tmt.log.Logger) -> list[Path]:
"""
Collect effective paths ignored by git.
:param root: path to the root of git repository.
:param logger: used for logging.
:returns: list of actual paths tah would be ignored by git based on

This comment has been minimized.

Copy link
@cgwalters

cgwalters Jul 31, 2024

Contributor

s/tah/that

its ``.gitignore`` files. If a whole directory is to be ignored,
its listed as a directory path, not listing its content.
"""

output = Command(
'git',
'ls-files',
# Consider standard git exclusion files
'--exclude-standard',
# List untracked files matching exclusion patterns
'-oi',
# If a whole directory is to be ignored, list only its name with a trailing slash
'--directory') \
.run(cwd=root, logger=logger)

return [Path(line.strip()) for line in output.stdout.splitlines()] if output.stdout else []


def default_branch(
*,
repository: Path,
Expand Down

0 comments on commit b7e8759

Please sign in to comment.