Skip to content

Commit

Permalink
Remove 0-added,0-removed modified entries from JSON output.
Browse files Browse the repository at this point in the history
  • Loading branch information
cr1901 authored and ThomasWaldmann committed Oct 5, 2024
1 parent ea619e7 commit 16dc660
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/borg/archiver/diff_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -56,20 +83,7 @@ def do_diff(self, args, repository, manifest):
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():
Expand Down

0 comments on commit 16dc660

Please sign in to comment.