Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base: Honor .gitignore for rsync #3037

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,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 @@ -1806,7 +1806,36 @@ def _initialize_worktree(self) -> None:

# Sync metadata root to the worktree
self.debug(f"Sync the worktree to '{self.worktree}'.", level=2)
self.run(Command("rsync", "-ar", "--exclude", ".git", f"{tree_root}/", self.worktree))

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 # 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()

# 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 that would be ignored by git based on
its ``.gitignore`` files. If a whole directory is to be ignored,
it is 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
Loading