瀏覽代碼

Remove 0-added,0-removed modified entries from JSON output.

William D. Jones 9 月之前
父節點
當前提交
16dc660d1d
共有 1 個文件被更改,包括 28 次插入14 次删除
  1. 28 14
      src/borg/archiver/diff_cmd.py

+ 28 - 14
src/borg/archiver/diff_cmd.py

@@ -18,6 +18,33 @@ class DiffMixIn:
     @with_repository(compatibility=(Manifest.Operation.READ,))
     def do_diff(self, args, repository, manifest):
         """Diff contents of two archives"""
+
+        def print_json_output(diff):
+            def actual_change(j):
+                j = j.to_dict()
+                if j["type"] == "modified":
+                    # It's useful to show 0 added and 0 removed for text output
+                    # but for JSON this is essentially noise. Additionally, the
+                    # JSON key is "changes", and this is not actually a change.
+                    return not (j["added"] == 0 and j["removed"] == 0)
+                else:
+                    return True
+
+            print(
+                json.dumps(
+                    {
+                        "path": diff.path,
+                        "changes": [
+                            change.to_dict()
+                            for name, change in diff.changes().items()
+                            if actual_change(change) and (not args.content_only or (name not in DiffFormatter.METADATA))
+                        ],
+                    },
+                    sort_keys=True,
+                    cls=BorgJsonEncoder,
+                )
+            )
+
         if args.format is not None:
             format = args.format
         elif args.content_only:
@@ -56,20 +83,7 @@ class DiffMixIn:
         formatter = DiffFormatter(format, args.content_only)
         for diff in diffs:
             if args.json_lines:
-                print(
-                    json.dumps(
-                        {
-                            "path": diff.path,
-                            "changes": [
-                                change.to_dict()
-                                for name, change in diff.changes().items()
-                                if not args.content_only or (name not in DiffFormatter.METADATA)
-                            ],
-                        },
-                        sort_keys=True,
-                        cls=BorgJsonEncoder,
-                    )
-                )
+                print_json_output(diff)
             else:
                 res: str = formatter.format_item(diff)
                 if res.strip():