| 1 |
package packfile_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"math/rand" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 11 |
"github.com/go-git/go-git/v5/plumbing/format/idxfile" |
| 12 |
. "github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 14 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 15 |
|
| 16 |
"github.com/go-git/go-billy/v5/memfs" |
| 17 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 18 |
. "gopkg.in/check.v1" |
| 19 |
) |
| 20 |
|
| 21 |
type EncoderAdvancedSuite struct { |
| 22 |
fixtures.Suite |
| 23 |
} |
| 24 |
|
| 25 |
var _ = Suite(&EncoderAdvancedSuite{}) |
| 26 |
|
| 27 |
func (s *EncoderAdvancedSuite) TestEncodeDecode(c *C) { |
| 28 |
if testing.Short() { |
| 29 |
c.Skip("skipping test in short mode.") |
| 30 |
} |
| 31 |
|
| 32 |
fixs := fixtures.Basic().ByTag("packfile").ByTag(".git") |
| 33 |
fixs = append(fixs, fixtures.ByURL("https://github.com/src-d/go-git.git"). |
| 34 |
ByTag("packfile").ByTag(".git").One()) |
| 35 |
fixs.Test(c, func(f *fixtures.Fixture) { |
| 36 |
storage := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) |
| 37 |
s.testEncodeDecode(c, storage, 10) |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
func (s *EncoderAdvancedSuite) TestEncodeDecodeNoDeltaCompression(c *C) { |
| 42 |
if testing.Short() { |
| 43 |
c.Skip("skipping test in short mode.") |
| 44 |
} |
| 45 |
|
| 46 |
fixs := fixtures.Basic().ByTag("packfile").ByTag(".git") |
| 47 |
fixs = append(fixs, fixtures.ByURL("https://github.com/src-d/go-git.git"). |
| 48 |
ByTag("packfile").ByTag(".git").One()) |
| 49 |
fixs.Test(c, func(f *fixtures.Fixture) { |
| 50 |
storage := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) |
| 51 |
s.testEncodeDecode(c, storage, 0) |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func (s *EncoderAdvancedSuite) testEncodeDecode( |
| 56 |
c *C, |
| 57 |
storage storer.Storer, |
| 58 |
packWindow uint, |
| 59 |
) { |
| 60 |
objIter, err := storage.IterEncodedObjects(plumbing.AnyObject) |
| 61 |
c.Assert(err, IsNil) |
| 62 |
|
| 63 |
expectedObjects := map[plumbing.Hash]bool{} |
| 64 |
var hashes []plumbing.Hash |
| 65 |
err = objIter.ForEach(func(o plumbing.EncodedObject) error { |
| 66 |
expectedObjects[o.Hash()] = true |
| 67 |
hashes = append(hashes, o.Hash()) |
| 68 |
return err |
| 69 |
|
| 70 |
}) |
| 71 |
c.Assert(err, IsNil) |
| 72 |
|
| 73 |
// Shuffle hashes to avoid delta selector getting order right just because |
| 74 |
// the initial order is correct. |
| 75 |
auxHashes := make([]plumbing.Hash, len(hashes)) |
| 76 |
for i, j := range rand.Perm(len(hashes)) { |
| 77 |
auxHashes[j] = hashes[i] |
| 78 |
} |
| 79 |
hashes = auxHashes |
| 80 |
|
| 81 |
buf := bytes.NewBuffer(nil) |
| 82 |
enc := NewEncoder(buf, storage, false) |
| 83 |
encodeHash, err := enc.Encode(hashes, packWindow) |
| 84 |
c.Assert(err, IsNil) |
| 85 |
|
| 86 |
fs := memfs.New() |
| 87 |
f, err := fs.Create("packfile") |
| 88 |
c.Assert(err, IsNil) |
| 89 |
|
| 90 |
_, err = f.Write(buf.Bytes()) |
| 91 |
c.Assert(err, IsNil) |
| 92 |
|
| 93 |
_, err = f.Seek(0, io.SeekStart) |
| 94 |
c.Assert(err, IsNil) |
| 95 |
|
| 96 |
w := new(idxfile.Writer) |
| 97 |
parser, err := NewParser(NewScanner(f), w) |
| 98 |
c.Assert(err, IsNil) |
| 99 |
|
| 100 |
_, err = parser.Parse() |
| 101 |
c.Assert(err, IsNil) |
| 102 |
index, err := w.Index() |
| 103 |
c.Assert(err, IsNil) |
| 104 |
|
| 105 |
_, err = f.Seek(0, io.SeekStart) |
| 106 |
c.Assert(err, IsNil) |
| 107 |
|
| 108 |
p := NewPackfile(index, fs, f, 0) |
| 109 |
|
| 110 |
decodeHash, err := p.ID() |
| 111 |
c.Assert(err, IsNil) |
| 112 |
c.Assert(encodeHash, Equals, decodeHash) |
| 113 |
|
| 114 |
objIter, err = p.GetAll() |
| 115 |
c.Assert(err, IsNil) |
| 116 |
obtainedObjects := map[plumbing.Hash]bool{} |
| 117 |
err = objIter.ForEach(func(o plumbing.EncodedObject) error { |
| 118 |
obtainedObjects[o.Hash()] = true |
| 119 |
return nil |
| 120 |
}) |
| 121 |
c.Assert(err, IsNil) |
| 122 |
c.Assert(obtainedObjects, DeepEquals, expectedObjects) |
| 123 |
|
| 124 |
for h := range obtainedObjects { |
| 125 |
if !expectedObjects[h] { |
| 126 |
c.Errorf("obtained unexpected object: %s", h) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
for h := range expectedObjects { |
| 131 |
if !obtainedObjects[h] { |
| 132 |
c.Errorf("missing object: %s", h) |
| 133 |
} |
| 134 |
} |
| 135 |
} |