Fix error when running the "prune" action with both "archive_name_format" and "prefix" options set (#668).

This commit is contained in:
Dan Helfman 2023-04-05 14:58:05 -07:00
parent 080c3afa0d
commit 192bfe46a9
3 changed files with 34 additions and 12 deletions

2
NEWS
View File

@ -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

View File

@ -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,
)
)
)

View File

@ -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)))