Index: Add DuplicateBlobs()

This commit is contained in:
Alexander Neumann 2016-08-07 21:57:31 +02:00
parent f5daf33322
commit 4bdd59b4ad
2 changed files with 34 additions and 0 deletions

View file

@ -147,3 +147,22 @@ func Load(repo *repository.Repository) (*Index, error) {
return idx, nil
}
// DuplicateBlobs returns a list of blobs that are stored more than once in the
// repo.
func (idx *Index) DuplicateBlobs() (dups map[pack.Handle]int) {
dups = make(map[pack.Handle]int)
seen := pack.NewBlobSet()
for _, p := range idx.Packs {
for _, entry := range p.Entries {
h := pack.Handle{ID: entry.ID, Type: entry.Type}
if seen.Has(h) {
dups[h]++
}
seen.Insert(h)
}
}
return dups
}

View file

@ -154,3 +154,18 @@ func BenchmarkIndexNew(b *testing.B) {
}
}
}
func TestIndexDuplicateBlobs(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0.05)
defer cleanup()
idx, err := New(repo)
if err != nil {
t.Fatal(err)
}
dups := idx.DuplicateBlobs()
if len(dups) == 0 {
t.Errorf("no duplicate blobs found")
}
}