mirror of https://github.com/restic/restic.git
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:
parent
e60c5b2d7f
commit
691c01963b
|
@ -1,6 +1,10 @@
|
|||
package restic
|
||||
|
||||
import "sort"
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// BlobSet is a set of blobs.
|
||||
type BlobSet map[BlobHandle]struct{}
|
||||
|
@ -103,11 +107,27 @@ func (s BlobSet) List() BlobHandles {
|
|||
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 {
|
||||
str := s.List().String()
|
||||
if len(str) < 2 {
|
||||
return "{}"
|
||||
}
|
||||
const maxelems = 10
|
||||
|
||||
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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue