| 1 |
package packfile_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"math" |
| 6 |
|
| 7 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 8 |
"github.com/go-git/go-git/v5/plumbing" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/format/idxfile" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 11 |
. "gopkg.in/check.v1" |
| 12 |
) |
| 13 |
|
| 14 |
type PackfileSuite struct { |
| 15 |
fixtures.Suite |
| 16 |
p *packfile.Packfile |
| 17 |
idx *idxfile.MemoryIndex |
| 18 |
f *fixtures.Fixture |
| 19 |
} |
| 20 |
|
| 21 |
var _ = Suite(&PackfileSuite{}) |
| 22 |
|
| 23 |
func (s *PackfileSuite) TestGet(c *C) { |
| 24 |
for h := range expectedEntries { |
| 25 |
obj, err := s.p.Get(h) |
| 26 |
c.Assert(err, IsNil) |
| 27 |
c.Assert(obj, Not(IsNil)) |
| 28 |
c.Assert(obj.Hash(), Equals, h) |
| 29 |
} |
| 30 |
|
| 31 |
_, err := s.p.Get(plumbing.ZeroHash) |
| 32 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 33 |
} |
| 34 |
|
| 35 |
func (s *PackfileSuite) TestGetByOffset(c *C) { |
| 36 |
for h, o := range expectedEntries { |
| 37 |
obj, err := s.p.GetByOffset(o) |
| 38 |
c.Assert(err, IsNil) |
| 39 |
c.Assert(obj, Not(IsNil)) |
| 40 |
c.Assert(obj.Hash(), Equals, h) |
| 41 |
} |
| 42 |
|
| 43 |
_, err := s.p.GetByOffset(math.MaxInt64) |
| 44 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 45 |
} |
| 46 |
|
| 47 |
func (s *PackfileSuite) TestID(c *C) { |
| 48 |
id, err := s.p.ID() |
| 49 |
c.Assert(err, IsNil) |
| 50 |
c.Assert(id.String(), Equals, s.f.PackfileHash) |
| 51 |
} |
| 52 |
|
| 53 |
func (s *PackfileSuite) TestGetAll(c *C) { |
| 54 |
iter, err := s.p.GetAll() |
| 55 |
c.Assert(err, IsNil) |
| 56 |
|
| 57 |
var objects int |
| 58 |
for { |
| 59 |
o, err := iter.Next() |
| 60 |
if err == io.EOF { |
| 61 |
break |
| 62 |
} |
| 63 |
c.Assert(err, IsNil) |
| 64 |
|
| 65 |
objects++ |
| 66 |
_, ok := expectedEntries[o.Hash()] |
| 67 |
c.Assert(ok, Equals, true) |
| 68 |
} |
| 69 |
|
| 70 |
c.Assert(objects, Equals, len(expectedEntries)) |
| 71 |
} |
| 72 |
|
| 73 |
var expectedEntries = map[plumbing.Hash]int64{ |
| 74 |
plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea"): 615, |
| 75 |
plumbing.NewHash("32858aad3c383ed1ff0a0f9bdf231d54a00c9e88"): 1524, |
| 76 |
plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"): 1063, |
| 77 |
plumbing.NewHash("49c6bb89b17060d7b4deacb7b338fcc6ea2352a9"): 78882, |
| 78 |
plumbing.NewHash("4d081c50e250fa32ea8b1313cf8bb7c2ad7627fd"): 84688, |
| 79 |
plumbing.NewHash("586af567d0bb5e771e49bdd9434f5e0fb76d25fa"): 84559, |
| 80 |
plumbing.NewHash("5a877e6a906a2743ad6e45d99c1793642aaf8eda"): 84479, |
| 81 |
plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"): 186, |
| 82 |
plumbing.NewHash("7e59600739c96546163833214c36459e324bad0a"): 84653, |
| 83 |
plumbing.NewHash("880cd14280f4b9b6ed3986d6671f907d7cc2a198"): 78050, |
| 84 |
plumbing.NewHash("8dcef98b1d52143e1e2dbc458ffe38f925786bf2"): 84741, |
| 85 |
plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"): 286, |
| 86 |
plumbing.NewHash("9a48f23120e880dfbe41f7c9b7b708e9ee62a492"): 80998, |
| 87 |
plumbing.NewHash("9dea2395f5403188298c1dabe8bdafe562c491e3"): 84032, |
| 88 |
plumbing.NewHash("a39771a7651f97faf5c72e08224d857fc35133db"): 84430, |
| 89 |
plumbing.NewHash("a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69"): 838, |
| 90 |
plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c"): 84375, |
| 91 |
plumbing.NewHash("aa9b383c260e1d05fbbf6b30a02914555e20c725"): 84760, |
| 92 |
plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"): 449, |
| 93 |
plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"): 1392, |
| 94 |
plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"): 1230, |
| 95 |
plumbing.NewHash("c192bd6a24ea1ab01d78686e417c8bdc7c3d197f"): 1713, |
| 96 |
plumbing.NewHash("c2d30fa8ef288618f65f6eed6e168e0d514886f4"): 84725, |
| 97 |
plumbing.NewHash("c8f1d8c61f9da76f4cb49fd86322b6e685dba956"): 80725, |
| 98 |
plumbing.NewHash("cf4aa3b38974fb7d81f367c0830f7d78d65ab86b"): 84608, |
| 99 |
plumbing.NewHash("d3ff53e0564a9f87d8e84b6e28e5060e517008aa"): 1685, |
| 100 |
plumbing.NewHash("d5c0f4ab811897cadf03aec358ae60d21f91c50d"): 2351, |
| 101 |
plumbing.NewHash("dbd3641b371024f44d0e469a9c8f5457b0660de1"): 84115, |
| 102 |
plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"): 12, |
| 103 |
plumbing.NewHash("eba74343e2f15d62adedfd8c883ee0262b5c8021"): 84708, |
| 104 |
plumbing.NewHash("fb72698cab7617ac416264415f13224dfd7a165e"): 84671, |
| 105 |
} |
| 106 |
|
| 107 |
func (s *PackfileSuite) SetUpTest(c *C) { |
| 108 |
s.f = fixtures.Basic().One() |
| 109 |
|
| 110 |
s.idx = idxfile.NewMemoryIndex() |
| 111 |
c.Assert(idxfile.NewDecoder(s.f.Idx()).Decode(s.idx), IsNil) |
| 112 |
|
| 113 |
s.p = packfile.NewPackfile(s.idx, fixtures.Filesystem, s.f.Packfile(), 0) |
| 114 |
} |
| 115 |
|
| 116 |
func (s *PackfileSuite) TearDownTest(c *C) { |
| 117 |
c.Assert(s.p.Close(), IsNil) |
| 118 |
} |
| 119 |
|
| 120 |
func (s *PackfileSuite) TestDecode(c *C) { |
| 121 |
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) { |
| 122 |
index := getIndexFromIdxFile(f.Idx()) |
| 123 |
|
| 124 |
p := packfile.NewPackfile(index, fixtures.Filesystem, f.Packfile(), 0) |
| 125 |
defer p.Close() |
| 126 |
|
| 127 |
for _, h := range expectedHashes { |
| 128 |
obj, err := p.Get(plumbing.NewHash(h)) |
| 129 |
c.Assert(err, IsNil) |
| 130 |
c.Assert(obj.Hash().String(), Equals, h) |
| 131 |
} |
| 132 |
}) |
| 133 |
} |
| 134 |
|
| 135 |
func (s *PackfileSuite) TestDecodeByTypeRefDelta(c *C) { |
| 136 |
f := fixtures.Basic().ByTag("ref-delta").One() |
| 137 |
|
| 138 |
index := getIndexFromIdxFile(f.Idx()) |
| 139 |
|
| 140 |
packfile := packfile.NewPackfile(index, fixtures.Filesystem, f.Packfile(), 0) |
| 141 |
defer packfile.Close() |
| 142 |
|
| 143 |
iter, err := packfile.GetByType(plumbing.CommitObject) |
| 144 |
c.Assert(err, IsNil) |
| 145 |
|
| 146 |
var count int |
| 147 |
for { |
| 148 |
obj, err := iter.Next() |
| 149 |
if err == io.EOF { |
| 150 |
break |
| 151 |
} |
| 152 |
|
| 153 |
count++ |
| 154 |
c.Assert(err, IsNil) |
| 155 |
c.Assert(obj.Type(), Equals, plumbing.CommitObject) |
| 156 |
} |
| 157 |
|
| 158 |
c.Assert(count > 0, Equals, true) |
| 159 |
} |
| 160 |
|
| 161 |
func (s *PackfileSuite) TestDecodeByType(c *C) { |
| 162 |
ts := []plumbing.ObjectType{ |
| 163 |
plumbing.CommitObject, |
| 164 |
plumbing.TagObject, |
| 165 |
plumbing.TreeObject, |
| 166 |
plumbing.BlobObject, |
| 167 |
} |
| 168 |
|
| 169 |
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) { |
| 170 |
for _, t := range ts { |
| 171 |
index := getIndexFromIdxFile(f.Idx()) |
| 172 |
|
| 173 |
packfile := packfile.NewPackfile(index, fixtures.Filesystem, f.Packfile(), 0) |
| 174 |
defer packfile.Close() |
| 175 |
|
| 176 |
iter, err := packfile.GetByType(t) |
| 177 |
c.Assert(err, IsNil) |
| 178 |
|
| 179 |
c.Assert(iter.ForEach(func(obj plumbing.EncodedObject) error { |
| 180 |
c.Assert(obj.Type(), Equals, t) |
| 181 |
return nil |
| 182 |
}), IsNil) |
| 183 |
} |
| 184 |
}) |
| 185 |
} |
| 186 |
|
| 187 |
func (s *PackfileSuite) TestDecodeByTypeConstructor(c *C) { |
| 188 |
f := fixtures.Basic().ByTag("packfile").One() |
| 189 |
index := getIndexFromIdxFile(f.Idx()) |
| 190 |
|
| 191 |
packfile := packfile.NewPackfile(index, fixtures.Filesystem, f.Packfile(), 0) |
| 192 |
defer packfile.Close() |
| 193 |
|
| 194 |
_, err := packfile.GetByType(plumbing.OFSDeltaObject) |
| 195 |
c.Assert(err, Equals, plumbing.ErrInvalidType) |
| 196 |
|
| 197 |
_, err = packfile.GetByType(plumbing.REFDeltaObject) |
| 198 |
c.Assert(err, Equals, plumbing.ErrInvalidType) |
| 199 |
|
| 200 |
_, err = packfile.GetByType(plumbing.InvalidObject) |
| 201 |
c.Assert(err, Equals, plumbing.ErrInvalidType) |
| 202 |
} |
| 203 |
|
| 204 |
var expectedHashes = []string{ |
| 205 |
"918c48b83bd081e863dbe1b80f8998f058cd8294", |
| 206 |
"af2d6a6954d532f8ffb47615169c8fdf9d383a1a", |
| 207 |
"1669dce138d9b841a518c64b10914d88f5e488ea", |
| 208 |
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", |
| 209 |
"b8e471f58bcbca63b07bda20e428190409c2db47", |
| 210 |
"35e85108805c84807bc66a02d91535e1e24b38b9", |
| 211 |
"b029517f6300c2da0f4b651b8642506cd6aaf45d", |
| 212 |
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", |
| 213 |
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", |
| 214 |
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", |
| 215 |
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", |
| 216 |
"49c6bb89b17060d7b4deacb7b338fcc6ea2352a9", |
| 217 |
"cf4aa3b38974fb7d81f367c0830f7d78d65ab86b", |
| 218 |
"9dea2395f5403188298c1dabe8bdafe562c491e3", |
| 219 |
"586af567d0bb5e771e49bdd9434f5e0fb76d25fa", |
| 220 |
"9a48f23120e880dfbe41f7c9b7b708e9ee62a492", |
| 221 |
"5a877e6a906a2743ad6e45d99c1793642aaf8eda", |
| 222 |
"c8f1d8c61f9da76f4cb49fd86322b6e685dba956", |
| 223 |
"a8d315b2b1c615d43042c3a62402b8a54288cf5c", |
| 224 |
"a39771a7651f97faf5c72e08224d857fc35133db", |
| 225 |
"880cd14280f4b9b6ed3986d6671f907d7cc2a198", |
| 226 |
"fb72698cab7617ac416264415f13224dfd7a165e", |
| 227 |
"4d081c50e250fa32ea8b1313cf8bb7c2ad7627fd", |
| 228 |
"eba74343e2f15d62adedfd8c883ee0262b5c8021", |
| 229 |
"c2d30fa8ef288618f65f6eed6e168e0d514886f4", |
| 230 |
"8dcef98b1d52143e1e2dbc458ffe38f925786bf2", |
| 231 |
"aa9b383c260e1d05fbbf6b30a02914555e20c725", |
| 232 |
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 233 |
"dbd3641b371024f44d0e469a9c8f5457b0660de1", |
| 234 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 235 |
"7e59600739c96546163833214c36459e324bad0a", |
| 236 |
} |
| 237 |
|
| 238 |
func getIndexFromIdxFile(r io.Reader) idxfile.Index { |
| 239 |
idx := idxfile.NewMemoryIndex() |
| 240 |
if err := idxfile.NewDecoder(r).Decode(idx); err != nil { |
| 241 |
panic(err) |
| 242 |
} |
| 243 |
|
| 244 |
return idx |
| 245 |
} |
| 246 |
|
| 247 |
func (s *PackfileSuite) TestSize(c *C) { |
| 248 |
f := fixtures.Basic().ByTag("ref-delta").One() |
| 249 |
|
| 250 |
index := getIndexFromIdxFile(f.Idx()) |
| 251 |
|
| 252 |
packfile := packfile.NewPackfile(index, fixtures.Filesystem, f.Packfile(), 0) |
| 253 |
defer packfile.Close() |
| 254 |
|
| 255 |
// Get the size of binary.jpg, which is not delta-encoded. |
| 256 |
offset, err := packfile.FindOffset(plumbing.NewHash("d5c0f4ab811897cadf03aec358ae60d21f91c50d")) |
| 257 |
c.Assert(err, IsNil) |
| 258 |
size, err := packfile.GetSizeByOffset(offset) |
| 259 |
c.Assert(err, IsNil) |
| 260 |
c.Assert(size, Equals, int64(76110)) |
| 261 |
|
| 262 |
// Get the size of the root commit, which is delta-encoded. |
| 263 |
offset, err = packfile.FindOffset(plumbing.NewHash(f.Head)) |
| 264 |
c.Assert(err, IsNil) |
| 265 |
size, err = packfile.GetSizeByOffset(offset) |
| 266 |
c.Assert(err, IsNil) |
| 267 |
c.Assert(size, Equals, int64(245)) |
| 268 |
} |