diff: convert to termstatus

This commit is contained in:
Michael Eischer
2025-09-14 00:12:22 +02:00
parent 3e7aad8916
commit 69186350fc
2 changed files with 37 additions and 27 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/ui"
"github.com/restic/restic/internal/ui/termstatus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
@@ -52,7 +53,9 @@ Exit status is 12 if the password is incorrect.
GroupID: cmdGroupDefault, GroupID: cmdGroupDefault,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runDiff(cmd.Context(), opts, globalOptions, args) term, cancel := setupTermstatus()
defer cancel()
return runDiff(cmd.Context(), opts, globalOptions, args, term)
}, },
} }
@@ -82,6 +85,7 @@ type Comparer struct {
repo restic.BlobLoader repo restic.BlobLoader
opts DiffOptions opts DiffOptions
printChange func(change *Change) printChange func(change *Change)
printError func(string, ...interface{})
} }
type Change struct { type Change struct {
@@ -155,7 +159,7 @@ type DiffStatsContainer struct {
} }
// updateBlobs updates the blob counters in the stats struct. // updateBlobs updates the blob counters in the stats struct.
func updateBlobs(repo restic.Loader, blobs restic.BlobSet, stats *DiffStat) { func updateBlobs(repo restic.Loader, blobs restic.BlobSet, stats *DiffStat, printError func(string, ...interface{})) {
for h := range blobs { for h := range blobs {
switch h.Type { switch h.Type {
case restic.DataBlob: case restic.DataBlob:
@@ -166,7 +170,7 @@ func updateBlobs(repo restic.Loader, blobs restic.BlobSet, stats *DiffStat) {
size, found := repo.LookupBlobSize(h.Type, h.ID) size, found := repo.LookupBlobSize(h.Type, h.ID)
if !found { if !found {
Warnf("unable to find blob size for %v\n", h) printError("unable to find blob size for %v", h)
continue continue
} }
@@ -197,7 +201,7 @@ func (c *Comparer) printDir(ctx context.Context, mode string, stats *DiffStat, b
if node.Type == restic.NodeTypeDir { if node.Type == restic.NodeTypeDir {
err := c.printDir(ctx, mode, stats, blobs, name, *node.Subtree) err := c.printDir(ctx, mode, stats, blobs, name, *node.Subtree)
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
Warnf("error: %v\n", err) c.printError("error: %v", err)
} }
} }
} }
@@ -222,7 +226,7 @@ func (c *Comparer) collectDir(ctx context.Context, blobs restic.BlobSet, id rest
if node.Type == restic.NodeTypeDir { if node.Type == restic.NodeTypeDir {
err := c.collectDir(ctx, blobs, *node.Subtree) err := c.collectDir(ctx, blobs, *node.Subtree)
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
Warnf("error: %v\n", err) c.printError("error: %v", err)
} }
} }
} }
@@ -322,7 +326,7 @@ func (c *Comparer) diffTree(ctx context.Context, stats *DiffStatsContainer, pref
err = c.diffTree(ctx, stats, name, *node1.Subtree, *node2.Subtree) err = c.diffTree(ctx, stats, name, *node1.Subtree, *node2.Subtree)
} }
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
Warnf("error: %v\n", err) c.printError("error: %v", err)
} }
} }
case t1 && !t2: case t1 && !t2:
@@ -336,7 +340,7 @@ func (c *Comparer) diffTree(ctx context.Context, stats *DiffStatsContainer, pref
if node1.Type == restic.NodeTypeDir { if node1.Type == restic.NodeTypeDir {
err := c.printDir(ctx, "-", &stats.Removed, stats.BlobsBefore, prefix, *node1.Subtree) err := c.printDir(ctx, "-", &stats.Removed, stats.BlobsBefore, prefix, *node1.Subtree)
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
Warnf("error: %v\n", err) c.printError("error: %v", err)
} }
} }
case !t1 && t2: case !t1 && t2:
@@ -350,7 +354,7 @@ func (c *Comparer) diffTree(ctx context.Context, stats *DiffStatsContainer, pref
if node2.Type == restic.NodeTypeDir { if node2.Type == restic.NodeTypeDir {
err := c.printDir(ctx, "+", &stats.Added, stats.BlobsAfter, prefix, *node2.Subtree) err := c.printDir(ctx, "+", &stats.Added, stats.BlobsAfter, prefix, *node2.Subtree)
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
Warnf("error: %v\n", err) c.printError("error: %v", err)
} }
} }
} }
@@ -359,11 +363,13 @@ func (c *Comparer) diffTree(ctx context.Context, stats *DiffStatsContainer, pref
return ctx.Err() return ctx.Err()
} }
func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []string) error { func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []string, term *termstatus.Terminal) error {
if len(args) != 2 { if len(args) != 2 {
return errors.Fatalf("specify two snapshot IDs") return errors.Fatalf("specify two snapshot IDs")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
if err != nil { if err != nil {
return err return err
@@ -386,9 +392,9 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
} }
if !gopts.JSON { if !gopts.JSON {
Verbosef("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str()) printer.P("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str())
} }
bar := newIndexProgress(gopts.Quiet, gopts.JSON) bar := newIndexTerminalProgress(printer)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }
@@ -412,10 +418,11 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
} }
c := &Comparer{ c := &Comparer{
repo: repo, repo: repo,
opts: opts, opts: opts,
printError: printer.E,
printChange: func(change *Change) { printChange: func(change *Change) {
Printf("%-5s%v\n", change.Modifier, change.Path) printer.S("%-5s%v", change.Modifier, change.Path)
}, },
} }
@@ -424,7 +431,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
c.printChange = func(change *Change) { c.printChange = func(change *Change) {
err := enc.Encode(change) err := enc.Encode(change)
if err != nil { if err != nil {
Warnf("JSON encode failed: %v\n", err) printer.E("JSON encode failed: %v", err)
} }
} }
} }
@@ -450,23 +457,23 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
} }
both := stats.BlobsBefore.Intersect(stats.BlobsAfter) both := stats.BlobsBefore.Intersect(stats.BlobsAfter)
updateBlobs(repo, stats.BlobsBefore.Sub(both).Sub(stats.BlobsCommon), &stats.Removed) updateBlobs(repo, stats.BlobsBefore.Sub(both).Sub(stats.BlobsCommon), &stats.Removed, printer.E)
updateBlobs(repo, stats.BlobsAfter.Sub(both).Sub(stats.BlobsCommon), &stats.Added) updateBlobs(repo, stats.BlobsAfter.Sub(both).Sub(stats.BlobsCommon), &stats.Added, printer.E)
if gopts.JSON { if gopts.JSON {
err := json.NewEncoder(globalOptions.stdout).Encode(stats) err := json.NewEncoder(globalOptions.stdout).Encode(stats)
if err != nil { if err != nil {
Warnf("JSON encode failed: %v\n", err) printer.E("JSON encode failed: %v", err)
} }
} else { } else {
Printf("\n") printer.S("")
Printf("Files: %5d new, %5d removed, %5d changed\n", stats.Added.Files, stats.Removed.Files, stats.ChangedFiles) printer.S("Files: %5d new, %5d removed, %5d changed", stats.Added.Files, stats.Removed.Files, stats.ChangedFiles)
Printf("Dirs: %5d new, %5d removed\n", stats.Added.Dirs, stats.Removed.Dirs) printer.S("Dirs: %5d new, %5d removed", stats.Added.Dirs, stats.Removed.Dirs)
Printf("Others: %5d new, %5d removed\n", stats.Added.Others, stats.Removed.Others) printer.S("Others: %5d new, %5d removed", stats.Added.Others, stats.Removed.Others)
Printf("Data Blobs: %5d new, %5d removed\n", stats.Added.DataBlobs, stats.Removed.DataBlobs) printer.S("Data Blobs: %5d new, %5d removed", stats.Added.DataBlobs, stats.Removed.DataBlobs)
Printf("Tree Blobs: %5d new, %5d removed\n", stats.Added.TreeBlobs, stats.Removed.TreeBlobs) printer.S("Tree Blobs: %5d new, %5d removed", stats.Added.TreeBlobs, stats.Removed.TreeBlobs)
Printf(" Added: %-5s\n", ui.FormatBytes(stats.Added.Bytes)) printer.S(" Added: %-5s", ui.FormatBytes(stats.Added.Bytes))
Printf(" Removed: %-5s\n", ui.FormatBytes(stats.Removed.Bytes)) printer.S(" Removed: %-5s", ui.FormatBytes(stats.Removed.Bytes))
} }
return nil return nil

View File

@@ -12,6 +12,7 @@ import (
"testing" "testing"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui/termstatus"
) )
func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapshotID string) (string, error) { func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapshotID string) (string, error) {
@@ -19,7 +20,9 @@ func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapsh
opts := DiffOptions{ opts := DiffOptions{
ShowMetadata: false, ShowMetadata: false,
} }
return runDiff(context.TODO(), opts, gopts, []string{firstSnapshotID, secondSnapshotID}) return withTermStatus(gopts, func(ctx context.Context, term *termstatus.Terminal) error {
return runDiff(ctx, opts, gopts, []string{firstSnapshotID, secondSnapshotID}, term)
})
}) })
return buf.String(), err return buf.String(), err
} }