浏览代码

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

Dan Helfman 2 年之前
父节点
当前提交
192bfe46a9
共有 3 个文件被更改,包括 34 次插入12 次删除
  1. 2 0
      NEWS
  2. 14 12
      borgmatic/borg/prune.py
  3. 18 0
      tests/unit/borg/test_prune.py

+ 2 - 0
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

+ 14 - 12
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,
+            )
+        )
     )
 
 

+ 18 - 0
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)))