Skip to content

Commit 50b50e4

Browse files
Merge pull request #9107 from ThomasWaldmann/port-9096-to-master
json: include archive keys in JSON lines when requested via --format, fixes #9095
2 parents 736f2bf + 5fef3f0 commit 50b50e4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/borg/helpers/parseformat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ def __init__(self, archive, format):
898898
super().__init__(format, static_data)
899899
self.xxh64 = StreamingXXH64
900900
self.archive = archive
901+
# track which keys were requested in the format string
901902
self.format_keys = {f[1] for f in Formatter().parse(format)}
902903
self.call_keys = {
903904
"size": self.calculate_size,
@@ -942,6 +943,14 @@ def get_item_data(self, item, jsonline=False):
942943
item_data["inode"] = item.get("inode")
943944
for key in self.used_call_keys:
944945
item_data[key] = self.call_keys[key](item)
946+
# When producing JSON lines, include selected static archive-level keys if they were
947+
# requested via --format. This mirrors text output behavior and fixes #9095.
948+
if jsonline:
949+
# Include selected static archive-level keys when requested via --format.
950+
# Keep implementation style aligned with 1.4-maint.
951+
for k in ("archivename", "archiveid"):
952+
if k in self.format_keys:
953+
item_data[k] = self.static_data[k]
945954
return item_data
946955

947956
def calculate_num_chunks(self, item):

src/borg/testsuite/archiver/list_cmd_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ def test_list_json(archivers, request):
7979
assert file1["sha256"] == "b2915eb69f260d8d3c25249195f2c8f4f716ea82ec760ae929732c0262442b2b"
8080

8181

82+
def test_list_json_lines_includes_archive_keys_in_format(archivers, request):
83+
# Issue #9095 / PR #9096: archivename/archiveid should be available in JSON lines when
84+
# requested via --format.
85+
archiver = request.getfixturevalue(archivers)
86+
create_regular_file(archiver.input_path, "file1", size=1024)
87+
cmd(archiver, "repo-create", RK_ENCRYPTION)
88+
cmd(archiver, "create", "test", "input")
89+
90+
# Query archive info to obtain expected name and id
91+
info_archive = json.loads(cmd(archiver, "info", "--json", "-a", "test"))
92+
assert len(info_archive["archives"]) == 1
93+
archive_info = info_archive["archives"][0]
94+
expected_name = archive_info["name"]
95+
expected_id = archive_info["id"]
96+
97+
out = cmd(archiver, "list", "test", "--json-lines", "--format={archivename} {archiveid}")
98+
rows = [json.loads(s) for s in out.splitlines() if s]
99+
assert len(rows) >= 2 # directory + file
100+
row = rows[-1]
101+
assert row["archivename"] == expected_name
102+
assert row["archiveid"] == expected_id
103+
104+
82105
def test_list_depth(archivers, request):
83106
"""Test the --depth option for the list command."""
84107
archiver = request.getfixturevalue(archivers)

0 commit comments

Comments
 (0)