Enhancement: snapshots: Add --reverse option to reverse sort order (#5677)

This commit is contained in:
flinteger-code authored and GitHub committed 2026-11-27 22:24:36 +00:00
1 parent 4ea95e8de3
commit 1d0668f656
5 files changed
+82 -10

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
Enhancement: snapshots: Add --reverse option to reverse sort order
The `snapshots` command now supports a `--reverse` option, which lists
snapshots newest first instead of the default oldest first.
https://github.com/restic/restic/pull/5677
+8
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"sort"
"strconv"
"sync"
@@ -363,6 +364,13 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
return nil
}
// PrintSnapshots prints a text table of the snapshots in list to stdout.
// The list is sorted so that the newest snapshot is listed last.
func PrintSnapshots(stdout io.Writer, list data.Snapshots, reasons []data.KeepReason, compact bool) error {
sort.Stable(sort.Reverse(list))
return printSnapshotsTable(stdout, list, reasons, compact)
}
// ForgetGroup helps to print what is forgotten in JSON.
type ForgetGroup struct {
Tags []string `json:"tags"`
+12 -9
View File
@@ -59,6 +59,7 @@ type SnapshotOptions struct {
last bool // Deprecated in favour of Latest.
Latest int
GroupBy data.SnapshotGroupByOptions
Reverse bool
}
func (opts *SnapshotOptions) AddFlags(f *pflag.FlagSet) {
@@ -72,6 +73,7 @@ func (opts *SnapshotOptions) AddFlags(f *pflag.FlagSet) {
}
f.IntVar(&opts.Latest, "latest", 0, "only show the last `n` snapshots for each host and path")
f.VarP(&opts.GroupBy, "group-by", "g", "`group` snapshots by host, paths and/or tags, separated by comma")
f.BoolVar(&opts.Reverse, "reverse", false, "reverse sort order of snapshots, newest first")
}
func (opts *SnapshotOptions) Finalize() error {
@@ -118,7 +120,11 @@ func runSnapshots(ctx context.Context, opts SnapshotOptions, gopts global.Option
list = filterLatestSnapshots(list, opts.Latest)
}
}
sort.Sort(sort.Reverse(list))
if opts.Reverse {
sort.Stable(list)
} else {
sort.Stable(sort.Reverse(list))
}
snapshotGroups[k] = list
}
@@ -141,7 +147,7 @@ func runSnapshots(ctx context.Context, opts SnapshotOptions, gopts global.Option
return err
}
}
err := PrintSnapshots(gopts.Term.OutputWriter(), list, nil, opts.Compact)
err := printSnapshotsTable(gopts.Term.OutputWriter(), list, nil, opts.Compact)
if err != nil {
return err
}
@@ -199,8 +205,9 @@ func filterLatestSnapshotsInGroup(list data.Snapshots, limit int) data.Snapshots
return list[:min(limit, len(list))]
}
// PrintSnapshots prints a text table of the snapshots in list to stdout.
func PrintSnapshots(stdout io.Writer, list data.Snapshots, reasons []data.KeepReason, compact bool) error {
// printSnapshotsTable prints a text table of the snapshots in list to stdout,
// in the order the list is given. The caller is responsible for sorting it beforehand.
func printSnapshotsTable(stdout io.Writer, list data.Snapshots, reasons []data.KeepReason, compact bool) error {
// keep the reasons a snasphot is being kept in a map, so that it doesn't
// get lost when the list of snapshots is sorted
keepReasons := make(map[restic.ID]data.KeepReason, len(reasons))
@@ -216,11 +223,6 @@ func PrintSnapshots(stdout io.Writer, list data.Snapshots, reasons []data.KeepRe
hasSize = hasSize || (sn.Summary != nil)
}
// always sort the snapshots so that the newer ones are listed last
sort.SliceStable(list, func(i, j int) bool {
return list[i].Time.Before(list[j].Time)
})
// Determine the max widths for host and tag.
maxHost, maxTag := 10, 6
for _, sn := range list {
@@ -373,6 +375,7 @@ type SnapshotGroup struct {
}
// printSnapshotGroupJSON writes the JSON representation of list to stdout.
// The snapshots in snGroups are expected to already be sorted; the order is preserved as-is.
func printSnapshotGroupJSON(stdout io.Writer, snGroups map[string]data.Snapshots, grouped bool) error {
if grouped {
snapshotGroups := []SnapshotGroup{}
@@ -93,3 +93,28 @@ func TestSnapshotsLatest(t *testing.T) {
rtest.Assert(t, len(snapshots) == 1, "expected only one snapshot, got %d", len(snapshots))
rtest.Equals(t, snapshots[0].ID.String(), secondSnapshotID, "unexpected snapshot ID")
}
func TestSnapshotsReverse(t *testing.T) {
env, cleanup := withTestEnvironment(t)
defer cleanup()
secondSnapshotID := snapshotsGroupTestData(t, env, true)
for _, reverse := range []bool{false, true} {
buf, err := withCaptureStdout(t, env.gopts, func(ctx context.Context, gopts global.Options) error {
gopts.JSON = true
opts := SnapshotOptions{Reverse: reverse}
return runSnapshots(ctx, opts, gopts, []string{}, gopts.Term)
})
rtest.OK(t, err)
snapshots := []Snapshot{}
rtest.OK(t, json.Unmarshal(buf.Bytes(), &snapshots))
rtest.Assert(t, len(snapshots) == 2, "expected two snapshots, got %d", len(snapshots))
if reverse {
rtest.Equals(t, secondSnapshotID, snapshots[0].ID.String(), "expected newest snapshot first with --reverse")
} else {
rtest.Equals(t, secondSnapshotID, snapshots[1].ID.String(), "expected newest snapshot last by default")
}
}
}
+31 -1
View File
@@ -1,14 +1,18 @@
package main
import (
"bytes"
"strings"
"testing"
"time"
"github.com/restic/restic/internal/data"
rtest "github.com/restic/restic/internal/test"
"github.com/spf13/pflag"
)
// Regression test for #2979: no snapshots should print as [], not null.
func TestEmptySnapshotGroupJSON(t *testing.T) {
func TestSnapshotsEmptySnapshotGroupJSON(t *testing.T) {
for _, grouped := range []bool{false, true} {
var w strings.Builder
err := printSnapshotGroupJSON(&w, nil, grouped)
@@ -17,3 +21,29 @@ func TestEmptySnapshotGroupJSON(t *testing.T) {
rtest.Equals(t, "[]", strings.TrimSpace(w.String()))
}
}
// TestSnapshotsPrintSnapshotsSortsAscending verifies that PrintSnapshots always sorts its
// input so that the oldest snapshot is listed first, regardless of the given order.
func TestSnapshotsPrintSnapshotsSortsAscending(t *testing.T) {
now := time.Now()
list := data.Snapshots{
{Time: now.Add(2 * time.Hour), Hostname: "host1", Paths: []string{"/data"}},
{Time: now, Hostname: "host1", Paths: []string{"/data"}},
{Time: now.Add(1 * time.Hour), Hostname: "host1", Paths: []string{"/data"}},
}
var buf bytes.Buffer
err := PrintSnapshots(&buf, list, nil, true)
rtest.OK(t, err)
rtest.Assert(t, list[0].Time.Equal(now), "expected oldest snapshot first, got %v", list[0].Time)
rtest.Assert(t, list[1].Time.Equal(now.Add(1*time.Hour)), "expected second-oldest snapshot second, got %v", list[1].Time)
rtest.Assert(t, list[2].Time.Equal(now.Add(2*time.Hour)), "expected newest snapshot last, got %v", list[2].Time)
}
// TestSnapshotsReverseFlag verifies that the --reverse flag defaults to false.
func TestSnapshotsReverseFlag(t *testing.T) {
var opts SnapshotOptions
opts.AddFlags(pflag.NewFlagSet("snapshots", pflag.ContinueOnError))
rtest.Equals(t, false, opts.Reverse)
}