Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit eec77c3

Browse files
authored
Merge pull request #807 from keybase/strib/src-d-ignore-non-hash-files
dotgit: ignore filenames that don't match a hash
2 parents a042efa + 79db8cf commit eec77c3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

storage/filesystem/internal/dotgit/dotgit.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ func (d *DotGit) ObjectPacks() ([]plumbing.Hash, error) {
162162

163163
n := f.Name()
164164
h := plumbing.NewHash(n[5 : len(n)-5]) //pack-(hash).pack
165+
if h.IsZero() {
166+
// Ignore files with badly-formatted names.
167+
continue
168+
}
165169
packs = append(packs, h)
166-
167170
}
168171

169172
return packs, nil
@@ -255,7 +258,12 @@ func (d *DotGit) ForEachObjectHash(fun func(plumbing.Hash) error) error {
255258
}
256259

257260
for _, o := range d {
258-
err = fun(plumbing.NewHash(base + o.Name()))
261+
h := plumbing.NewHash(base + o.Name())
262+
if h.IsZero() {
263+
// Ignore files with badly-formatted names.
264+
continue
265+
}
266+
err = fun(h)
259267
if err != nil {
260268
return err
261269
}

storage/filesystem/internal/dotgit/dotgit_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func findReference(refs []*plumbing.Reference, name string) *plumbing.Reference
419419
return nil
420420
}
421421

422-
func (s *SuiteDotGit) TestObjectsPack(c *C) {
422+
func (s *SuiteDotGit) TestObjectPacks(c *C) {
423423
f := fixtures.Basic().ByTag(".git").One()
424424
fs := f.DotGit()
425425
dir := New(fs)
@@ -428,6 +428,18 @@ func (s *SuiteDotGit) TestObjectsPack(c *C) {
428428
c.Assert(err, IsNil)
429429
c.Assert(hashes, HasLen, 1)
430430
c.Assert(hashes[0], Equals, f.PackfileHash)
431+
432+
// Make sure that a random file in the pack directory doesn't
433+
// break everything.
434+
badFile, err := fs.Create("objects/pack/OOPS_THIS_IS_NOT_RIGHT.pack")
435+
c.Assert(err, IsNil)
436+
err = badFile.Close()
437+
c.Assert(err, IsNil)
438+
439+
hashes2, err := dir.ObjectPacks()
440+
c.Assert(err, IsNil)
441+
c.Assert(hashes2, HasLen, 1)
442+
c.Assert(hashes[0], Equals, hashes2[0])
431443
}
432444

433445
func (s *SuiteDotGit) TestObjectPack(c *C) {

0 commit comments

Comments
 (0)