2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

internal/restic: Return summary from BlobSet.String

Fixes #4449: error messages from prune were too long to fit in scroll
buffers.
This commit is contained in:
greatroar 2023-08-25 15:14:48 +02:00
parent e60c5b2d7f
commit 691c01963b
2 changed files with 58 additions and 6 deletions

View File

@ -1,6 +1,10 @@
package restic package restic
import "sort" import (
"fmt"
"sort"
"strings"
)
// BlobSet is a set of blobs. // BlobSet is a set of blobs.
type BlobSet map[BlobHandle]struct{} type BlobSet map[BlobHandle]struct{}
@ -103,11 +107,27 @@ func (s BlobSet) List() BlobHandles {
return list return list
} }
// String produces a human-readable representation of ids.
// It is meant for producing error messages,
// so it only returns a summary if ids is long.
func (s BlobSet) String() string { func (s BlobSet) String() string {
str := s.List().String() const maxelems = 10
if len(str) < 2 {
return "{}"
}
return "{" + str[1:len(str)-1] + "}" sb := new(strings.Builder)
sb.WriteByte('{')
n := 0
for k := range s {
if n != 0 {
sb.WriteByte(' ')
}
sb.WriteString(k.String())
if n++; n == maxelems {
fmt.Fprintf(sb, " (%d more)", len(s)-n-1)
break
}
}
sb.WriteByte('}')
return sb.String()
} }

View File

@ -0,0 +1,32 @@
package restic
import (
"math/rand"
"regexp"
"testing"
rtest "github.com/restic/restic/internal/test"
)
func TestBlobSetString(t *testing.T) {
s := NewBlobSet()
rtest.Equals(t, "{}", s.String())
id, _ := ParseID(
"1111111111111111111111111111111111111111111111111111111111111111")
s.Insert(BlobHandle{ID: id, Type: TreeBlob})
rtest.Equals(t, "{<tree/11111111>}", s.String())
var h BlobHandle
for i := 0; i < 100; i++ {
h.Type = DataBlob
_, _ = rand.Read(h.ID[:])
s.Insert(h)
}
r := regexp.MustCompile(
`^{(?:<(?:data|tree)/[0-9a-f]{8}> ){10}\(90 more\)}$`)
str := s.String()
rtest.Assert(t, r.MatchString(str), "%q doesn't match pattern", str)
}