Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/remote/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ def rsync(
if extra_args:
args.extend(extra_args)

# https://man.archlinux.org/man/extra/rsync/rsync.1.en#FILTER_RULES
if (
not (includes or extra_args or mirror)
and excludes is not None
and any(exclude in ("*", "**", "***") for exclude in excludes)
):
logger.info("Skipping sync due to '*' in excludes and no includes")
return

cleanup: List[Path] = []
# It is important to add include patterns before exclude patters because rsync might ignore includes if you do otherwise.
_gen_rsync_patterns_file(includes, "--include-from", args, cleanup)
Expand Down
51 changes: 51 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import sys

from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -75,6 +76,56 @@ def test_rsync_copies_files_with_mirror(tmp_path, rsync_ssh):
assert not (dst / "fourth.txt").exists()


@pytest.mark.parametrize(
"should_be_skipped, includes, excludes",
[
(False, [], []),
(False, ["/.remoteenv"], ["*", "foobar"]),
(False, ["/.remoteenv"], ["*"]),
(False, ["/.remoteenv"], ["**"]),
(False, ["/.remoteenv"], ["***"]),
(True, [], ["*", "foobar"]),
(True, [], ["*"]),
(True, [], ["**", "foobar"]),
(True, [], ["**"]),
(True, [], ["***", "foobar"]),
(True, [], ["***"]),
],
)
@patch("remote.util.subprocess.run")
def test_rsync_skip_on_globstar_exclude(mock_run, rsync_ssh, should_be_skipped, includes, excludes):
mock_run.return_value = MagicMock(returncode=0)

rsync_with = functools.partial(
rsync,
"src/",
"dst",
rsync_ssh,
info=True,
verbose=True,
mirror=False,
dry_run=True,
includes=includes,
excludes=excludes,
)

rsync_with(mirror=False, extra_args=None)
if should_be_skipped:
mock_run.assert_not_called()
else:
mock_run.assert_called_once()

mock_run.reset_mock()

rsync_with(mirror=False, extra_args=["--some-extra"])
mock_run.assert_called_once()

mock_run.reset_mock()

rsync_with(mirror=True, extra_args=None)
mock_run.assert_called_once()


@patch("remote.util.subprocess.run")
def test_rsync_respects_all_options(mock_run, rsync_ssh):
mock_run.return_value = MagicMock(returncode=0)
Expand Down
Loading