From 192bfe46a95a39e6eb34d64dbff3d2dbcb9ecce1 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Wed, 5 Apr 2023 14:58:05 -0700 Subject: [PATCH] Fix error when running the "prune" action with both "archive_name_format" and "prefix" options set (#668). --- NEWS | 2 ++ borgmatic/borg/prune.py | 26 ++++++++++++++------------ tests/unit/borg/test_prune.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 96634fd0..f80169a8 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ * #666: Fix error when running the "info" action with the "--match-archives" flag. Also fix the "--match-archives" flag to correctly override the "match_archives" configuration option for the "transfer", "list", "rlist", and "info" actions. + * #668: Fix error when running the "prune" action with both "archive_name_format" and "prefix" + options set. 1.7.11 * #479, #588: BREAKING: Automatically use the "archive_name_format" option to filter which archives diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index 7e12b9c2..3f06dc2d 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -26,22 +26,24 @@ def make_prune_flags(storage_config, retention_config, local_borg_version): config = retention_config.copy() prefix = config.pop('prefix', None) - if prefix: - if feature.available(feature.Feature.MATCH_ARCHIVES, local_borg_version): - config['match_archives'] = f'sh:{prefix}*' - else: - config['glob_archives'] = f'{prefix}*' - flag_pairs = ( ('--' + option_name.replace('_', '-'), str(value)) for option_name, value in config.items() ) - return tuple( - element for pair in flag_pairs for element in pair - ) + flags.make_match_archives_flags( - storage_config.get('match_archives'), - storage_config.get('archive_name_format'), - local_borg_version, + return tuple(element for pair in flag_pairs for element in pair) + ( + ( + ('--match-archives', f'sh:{prefix}*') + if feature.available(feature.Feature.MATCH_ARCHIVES, local_borg_version) + else ('--glob-archives', f'{prefix}*') + ) + if prefix + else ( + flags.make_match_archives_flags( + storage_config.get('match_archives'), + storage_config.get('archive_name_format'), + local_borg_version, + ) + ) ) diff --git a/tests/unit/borg/test_prune.py b/tests/unit/borg/test_prune.py index 37731aca..128bdc0a 100644 --- a/tests/unit/borg/test_prune.py +++ b/tests/unit/borg/test_prune.py @@ -69,6 +69,24 @@ def test_make_prune_flags_with_prefix_without_borg_features_uses_glob_archives() assert result == expected +def test_make_prune_flags_prefers_prefix_to_archive_name_format(): + storage_config = {'archive_name_format': 'bar-{now}'} # noqa: FS003 + retention_config = OrderedDict((('keep_daily', 1), ('prefix', 'bar-'))) + flexmock(module.feature).should_receive('available').and_return(True) + flexmock(module.flags).should_receive('make_match_archives_flags').never() + + result = module.make_prune_flags(storage_config, retention_config, local_borg_version='1.2.3') + + expected = ( + '--keep-daily', + '1', + '--match-archives', + 'sh:bar-*', # noqa: FS003 + ) + + assert result == expected + + def test_make_prune_flags_without_prefix_uses_archive_name_format_instead(): storage_config = {'archive_name_format': 'bar-{now}'} # noqa: FS003 retention_config = OrderedDict((('keep_daily', 1), ('prefix', None)))