forked from TrueCloudLab/restic
Merge pull request #4451 from greatroar/bh-string
internal/restic: Return summary from BlobSet.String
This commit is contained in:
commit
c7b5ddc012
2 changed files with 58 additions and 6 deletions
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
32
internal/restic/blob_set_test.go
Normal file
32
internal/restic/blob_set_test.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue