diff --git a/src/borg/legacy/archives.py b/src/borg/legacy/archives.py index 6f6f403dea..5f216b333e 100644 --- a/src/borg/legacy/archives.py +++ b/src/borg/legacy/archives.py @@ -15,7 +15,7 @@ from ..helpers.datastruct import StableDict from ..helpers.errors import CommandError, Error from ..helpers.parseformat import bin_to_hex -from ..helpers.time import parse_timestamp +from ..helpers.time import parse_timestamp, compile_date_pattern, DatePatternError from ..item import ArchiveItem from ..patterns import get_regex_from_pattern @@ -123,6 +123,13 @@ def _matching_info_tuples(self, match_patterns, match_end, *, deleted=False): elif match.startswith("host:"): wanted_host = match.removeprefix("host:") archive_infos = [x for x in archive_infos if x.host == wanted_host] + elif match.startswith("date:"): + wanted_date = match.removeprefix("date:") + try: + date_matches = compile_date_pattern(wanted_date) + except DatePatternError as exc: + raise CommandError(f"Invalid date pattern: {match} ({exc})") + archive_infos = [x for x in archive_infos if date_matches(x.ts)] else: match = match.removeprefix("name:") regex = get_regex_from_pattern(match) diff --git a/src/borg/testsuite/legacy_archives_test.py b/src/borg/testsuite/legacy_archives_test.py index 55e4180958..916b709f2d 100644 --- a/src/borg/testsuite/legacy_archives_test.py +++ b/src/borg/testsuite/legacy_archives_test.py @@ -371,6 +371,20 @@ def test_list_match_host(): assert la.list(match=["host:laptop"]) == [i1] +def test_list_match_date(): + i1 = _archiveinfo("a", _id(1), ts=TS) # 2020-06-01 + i2 = _archiveinfo("b", _id(2), ts=TS2) # 2021-06-01 + la = _make_list_target([i1, i2]) + assert la.list(match=["date:2020-06"]) == [i1] + + +def test_list_match_date_invalid_raises(): + i1 = _archiveinfo("a", _id(1)) + la = _make_list_target([i1]) + with pytest.raises(CommandError, match="Invalid date pattern"): + la.list(match=["date:not-a-date"]) + + def test_list_match_tags(): i1 = _archiveinfo("a", _id(1), tags=("prod", "db")) i2 = _archiveinfo("b", _id(2), tags=("dev",))