Repository: borgbackup/borg

Issue:
borg info: No output for empty repo with --last or --first supplied
```
$ borg --version
borg 1.1.17
```

`borg info` or `borg info --json` do not give any output if `--last=1` or `--first=1` is supplied and there are no archives yet.
borg info: No output for empty repo with --last or --first supplied
```
$ borg --version
borg 1.1.17
```

`borg info` or `borg info --json` do not give any output if `--last=1` or `--first=1` is supplied and there are no archives yet.


Discussion:
I'ld assume that if you give last/first=N that then **up to** N items are output (that is less than N if fewer than N archives exist). If 0 archives exist, 0 items are output.

So guess that is correct / consistent behaviour?

`borg info --json` without the `--last=1` gives the following

```json
{
    "cache": {
        "path": "...",
        "stats": {
            "total_chunks": 0,
            "total_csize": 0,
            "total_size": 0,
            "total_unique_chunks": 0,
            "unique_csize": 0,
            "unique_size": 0
        }
    },
    "encryption": {
        "mode": "repokey"
    },
    "repository": {
        "id": "...",
        "last_modified": "2021-10-12T00:03:37.000000",
        "location": "..."
    },
    "security_dir": "..."
}
```

With `--last=1` it returns nothing. Instead I would expect the same output but with `"archives": []` added.
Oh, ok, that makes sense.
also applies to master branch.
The cause of this is that if you invoke do_info for archive(s) (either giving an archive name, last/first or prefix), it calls `_info_archives` while it calls `_info_repository` when only giving the repository.

Also, there is a shortcut in the code if the archives list is empty - I'll remove that.
@sophie-h JSON info has been fixed: it now prints `"archives": []`, but without --json the output is still empty. Has this bug been fully resolved? Your first comment made it sound like you did not expect this behavior.
the code specifically checks for `--json` and only includes that info if that is given.
I'ld assume that if you give last/first=N that then **up to** N items are output (that is less than N if fewer than N archives exist). If 0 archives exist, 0 items are output.

So guess that is correct / consistent behaviour?

`borg info --json` without the `--last=1` gives the following

```json
{
    "cache": {
        "path": "...",
        "stats": {
            "total_chunks": 0,
            "total_csize": 0,
            "total_size": 0,
            "total_unique_chunks": 0,
            "unique_csize": 0,
            "unique_size": 0
        }
    },
    "encryption": {
        "mode": "repokey"
    },
    "repository": {
        "id": "...",
        "last_modified": "2021-10-12T00:03:37.000000",
        "location": "..."
    },
    "security_dir": "..."
}
```

With `--last=1` it returns nothing. Instead I would expect the same output but with `"archives": []` added.
Oh, ok, that makes sense.
also applies to master branch.
The cause of this is that if you invoke do_info for archive(s) (either giving an archive name, last/first or prefix), it calls `_info_archives` while it calls `_info_repository` when only giving the repository.

Also, there is a shortcut in the code if the archives list is empty - I'll remove that.
@sophie-h JSON info has been fixed: it now prints `"archives": []`, but without --json the output is still empty. Has this bug been fully resolved? Your first comment made it sound like you did not expect this behavior.
the code specifically checks for `--json` and only includes that info if that is given.

Resolving patch:
diff --git a/src/borg/archiver.py b/src/borg/archiver.py
--- a/src/borg/archiver.py
+++ b/src/borg/archiver.py
@@ -1530,8 +1530,6 @@ def format_cmdline(cmdline):
             archive_names = (args.location.archive,)
         else:
             archive_names = tuple(x.name for x in manifest.archives.list_considering(args))
-            if not archive_names:
-                return self.exit_code
 
         output_data = []
 
diff --git a/src/borg/testsuite/archiver.py b/src/borg/testsuite/archiver.py
--- a/src/borg/testsuite/archiver.py
+++ b/src/borg/testsuite/archiver.py
@@ -1476,6 +1476,14 @@ def test_info_json(self):
         assert datetime.strptime(archive['start'], ISO_FORMAT)
         assert datetime.strptime(archive['end'], ISO_FORMAT)
 
+    def test_info_json_of_empty_archive(self):
+        """See https://github.com/borgbackup/borg/issues/6120"""
+        self.cmd('init', '--encryption=repokey', self.repository_location)
+        info_repo = json.loads(self.cmd('info', '--json', '--first=1', self.repository_location))
+        assert info_repo["archives"] == []
+        info_repo = json.loads(self.cmd('info', '--json', '--last=1', self.repository_location))
+        assert info_repo["archives"] == []
+
     def test_comment(self):
         self.create_regular_file('file1', size=1024 * 80)
         self.cmd('init', '--encryption=repokey', self.repository_location)


Test patch: