1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-04 02:28:34 +00:00

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

This commit is contained in:
William D. Jones 2024-08-15 20:42:40 -04:00 committed by Thomas Waldmann
parent ea619e759d
commit 16dc660d1d
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01

View file

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