diff --git a/src/borg/archiver/diff_cmd.py b/src/borg/archiver/diff_cmd.py index 166539368..6bfb8daa7 100644 --- a/src/borg/archiver/diff_cmd.py +++ b/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():