| 1 |
package filesystem |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-git/go-billy/v5" |
| 12 |
"github.com/go-git/go-billy/v5/osfs" |
| 13 |
"github.com/go-git/go-git/v5/plumbing" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 15 |
"github.com/go-git/go-git/v5/storage/filesystem/dotgit" |
| 16 |
|
| 17 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 18 |
. "gopkg.in/check.v1" |
| 19 |
) |
| 20 |
|
| 21 |
type FsSuite struct { |
| 22 |
fixtures.Suite |
| 23 |
} |
| 24 |
|
| 25 |
var objectTypes = []plumbing.ObjectType{ |
| 26 |
plumbing.CommitObject, |
| 27 |
plumbing.TagObject, |
| 28 |
plumbing.TreeObject, |
| 29 |
plumbing.BlobObject, |
| 30 |
} |
| 31 |
|
| 32 |
var _ = Suite(&FsSuite{}) |
| 33 |
|
| 34 |
func (s *FsSuite) TestGetFromObjectFile(c *C) { |
| 35 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 36 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 37 |
|
| 38 |
expected := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") |
| 39 |
obj, err := o.EncodedObject(plumbing.AnyObject, expected) |
| 40 |
c.Assert(err, IsNil) |
| 41 |
c.Assert(obj.Hash(), Equals, expected) |
| 42 |
} |
| 43 |
|
| 44 |
func (s *FsSuite) TestGetFromPackfile(c *C) { |
| 45 |
fixtures.Basic().ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 46 |
fs := f.DotGit() |
| 47 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 48 |
|
| 49 |
expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 50 |
obj, err := o.EncodedObject(plumbing.AnyObject, expected) |
| 51 |
c.Assert(err, IsNil) |
| 52 |
c.Assert(obj.Hash(), Equals, expected) |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func firstNonMatching(packfileHash string) *fixtures.Fixture { |
| 57 |
for _, fix := range fixtures.ByTag(".git") { |
| 58 |
if fix.PackfileHash != packfileHash { |
| 59 |
return fix |
| 60 |
} |
| 61 |
} |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
func (s *FsSuite) TestMismatchIdxFile(c *C) { |
| 66 |
f := fixtures.Basic().ByTag(".git").One() |
| 67 |
fs := f.DotGit() |
| 68 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 69 |
|
| 70 |
fix2 := firstNonMatching(f.PackfileHash) |
| 71 |
c.Assert(fix2, NotNil) |
| 72 |
|
| 73 |
idx, err := fs.OpenFile(fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash), os.O_TRUNC|os.O_WRONLY, 0o600) |
| 74 |
c.Assert(err, IsNil) |
| 75 |
|
| 76 |
idx2 := fix2.Idx() |
| 77 |
_, err = io.Copy(idx, idx2) |
| 78 |
c.Assert(err, IsNil) |
| 79 |
|
| 80 |
err = idx.Close() |
| 81 |
c.Assert(err, IsNil) |
| 82 |
err = idx2.Close() |
| 83 |
c.Assert(err, IsNil) |
| 84 |
|
| 85 |
expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 86 |
obj, err := o.EncodedObject(plumbing.AnyObject, expected) |
| 87 |
c.Assert(obj, IsNil) |
| 88 |
c.Assert(err, ErrorMatches, "malformed IDX file: packfile mismatch: .*") |
| 89 |
} |
| 90 |
|
| 91 |
func (s *FsSuite) TestGetFromPackfileKeepDescriptors(c *C) { |
| 92 |
fixtures.Basic().ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 93 |
fs := f.DotGit() |
| 94 |
dg := dotgit.NewWithOptions(fs, dotgit.Options{KeepDescriptors: true}) |
| 95 |
o := NewObjectStorageWithOptions(dg, cache.NewObjectLRUDefault(), Options{KeepDescriptors: true}) |
| 96 |
|
| 97 |
expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 98 |
obj, err := o.EncodedObject(plumbing.AnyObject, expected) |
| 99 |
c.Assert(err, IsNil) |
| 100 |
c.Assert(obj.Hash(), Equals, expected) |
| 101 |
|
| 102 |
packfiles, err := dg.ObjectPacks() |
| 103 |
c.Assert(err, IsNil) |
| 104 |
|
| 105 |
pack1, err := dg.ObjectPack(packfiles[0]) |
| 106 |
c.Assert(err, IsNil) |
| 107 |
|
| 108 |
pack1.Seek(42, io.SeekStart) |
| 109 |
|
| 110 |
err = o.Close() |
| 111 |
c.Assert(err, IsNil) |
| 112 |
|
| 113 |
pack2, err := dg.ObjectPack(packfiles[0]) |
| 114 |
c.Assert(err, IsNil) |
| 115 |
|
| 116 |
offset, err := pack2.Seek(0, io.SeekCurrent) |
| 117 |
c.Assert(err, IsNil) |
| 118 |
c.Assert(offset, Equals, int64(0)) |
| 119 |
|
| 120 |
err = o.Close() |
| 121 |
c.Assert(err, IsNil) |
| 122 |
}) |
| 123 |
} |
| 124 |
|
| 125 |
func (s *FsSuite) TestGetFromPackfileMaxOpenDescriptors(c *C) { |
| 126 |
fs := fixtures.ByTag(".git").ByTag("multi-packfile").One().DotGit() |
| 127 |
o := NewObjectStorageWithOptions(dotgit.New(fs), cache.NewObjectLRUDefault(), Options{MaxOpenDescriptors: 1}) |
| 128 |
|
| 129 |
expected := plumbing.NewHash("8d45a34641d73851e01d3754320b33bb5be3c4d3") |
| 130 |
obj, err := o.getFromPackfile(expected, false) |
| 131 |
c.Assert(err, IsNil) |
| 132 |
c.Assert(obj.Hash(), Equals, expected) |
| 133 |
|
| 134 |
expected = plumbing.NewHash("e9cfa4c9ca160546efd7e8582ec77952a27b17db") |
| 135 |
obj, err = o.getFromPackfile(expected, false) |
| 136 |
c.Assert(err, IsNil) |
| 137 |
c.Assert(obj.Hash(), Equals, expected) |
| 138 |
|
| 139 |
err = o.Close() |
| 140 |
c.Assert(err, IsNil) |
| 141 |
} |
| 142 |
|
| 143 |
func (s *FsSuite) TestGetFromPackfileMaxOpenDescriptorsLargeObjectThreshold(c *C) { |
| 144 |
fs := fixtures.ByTag(".git").ByTag("multi-packfile").One().DotGit() |
| 145 |
o := NewObjectStorageWithOptions(dotgit.New(fs), cache.NewObjectLRUDefault(), Options{ |
| 146 |
MaxOpenDescriptors: 1, |
| 147 |
LargeObjectThreshold: 1, |
| 148 |
}) |
| 149 |
|
| 150 |
expected := plumbing.NewHash("8d45a34641d73851e01d3754320b33bb5be3c4d3") |
| 151 |
obj, err := o.getFromPackfile(expected, false) |
| 152 |
c.Assert(err, IsNil) |
| 153 |
c.Assert(obj.Hash(), Equals, expected) |
| 154 |
|
| 155 |
expected = plumbing.NewHash("e9cfa4c9ca160546efd7e8582ec77952a27b17db") |
| 156 |
obj, err = o.getFromPackfile(expected, false) |
| 157 |
c.Assert(err, IsNil) |
| 158 |
c.Assert(obj.Hash(), Equals, expected) |
| 159 |
|
| 160 |
err = o.Close() |
| 161 |
c.Assert(err, IsNil) |
| 162 |
} |
| 163 |
|
| 164 |
func (s *FsSuite) TestGetSizeOfObjectFile(c *C) { |
| 165 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 166 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 167 |
|
| 168 |
// Get the size of `tree_walker.go`. |
| 169 |
expected := plumbing.NewHash("cbd81c47be12341eb1185b379d1c82675aeded6a") |
| 170 |
size, err := o.EncodedObjectSize(expected) |
| 171 |
c.Assert(err, IsNil) |
| 172 |
c.Assert(size, Equals, int64(2412)) |
| 173 |
} |
| 174 |
|
| 175 |
func (s *FsSuite) TestGetSizeFromPackfile(c *C) { |
| 176 |
fixtures.Basic().ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 177 |
fs := f.DotGit() |
| 178 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 179 |
|
| 180 |
// Get the size of `binary.jpg`. |
| 181 |
expected := plumbing.NewHash("d5c0f4ab811897cadf03aec358ae60d21f91c50d") |
| 182 |
size, err := o.EncodedObjectSize(expected) |
| 183 |
c.Assert(err, IsNil) |
| 184 |
c.Assert(size, Equals, int64(76110)) |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
func (s *FsSuite) TestGetSizeOfAllObjectFiles(c *C) { |
| 189 |
fs := fixtures.ByTag(".git").One().DotGit() |
| 190 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 191 |
|
| 192 |
// Get the size of `tree_walker.go`. |
| 193 |
err := o.ForEachObjectHash(func(h plumbing.Hash) error { |
| 194 |
size, err := o.EncodedObjectSize(h) |
| 195 |
c.Assert(err, IsNil) |
| 196 |
c.Assert(size, Not(Equals), int64(0)) |
| 197 |
return nil |
| 198 |
}) |
| 199 |
c.Assert(err, IsNil) |
| 200 |
} |
| 201 |
|
| 202 |
func (s *FsSuite) TestGetFromPackfileMultiplePackfiles(c *C) { |
| 203 |
fs := fixtures.ByTag(".git").ByTag("multi-packfile").One().DotGit() |
| 204 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 205 |
|
| 206 |
expected := plumbing.NewHash("8d45a34641d73851e01d3754320b33bb5be3c4d3") |
| 207 |
obj, err := o.getFromPackfile(expected, false) |
| 208 |
c.Assert(err, IsNil) |
| 209 |
c.Assert(obj.Hash(), Equals, expected) |
| 210 |
|
| 211 |
expected = plumbing.NewHash("e9cfa4c9ca160546efd7e8582ec77952a27b17db") |
| 212 |
obj, err = o.getFromPackfile(expected, false) |
| 213 |
c.Assert(err, IsNil) |
| 214 |
c.Assert(obj.Hash(), Equals, expected) |
| 215 |
} |
| 216 |
|
| 217 |
func (s *FsSuite) TestGetFromPackfileMultiplePackfilesLargeObjectThreshold(c *C) { |
| 218 |
fs := fixtures.ByTag(".git").ByTag("multi-packfile").One().DotGit() |
| 219 |
o := NewObjectStorageWithOptions(dotgit.New(fs), cache.NewObjectLRUDefault(), Options{LargeObjectThreshold: 1}) |
| 220 |
|
| 221 |
expected := plumbing.NewHash("8d45a34641d73851e01d3754320b33bb5be3c4d3") |
| 222 |
obj, err := o.getFromPackfile(expected, false) |
| 223 |
c.Assert(err, IsNil) |
| 224 |
c.Assert(obj.Hash(), Equals, expected) |
| 225 |
|
| 226 |
expected = plumbing.NewHash("e9cfa4c9ca160546efd7e8582ec77952a27b17db") |
| 227 |
obj, err = o.getFromPackfile(expected, false) |
| 228 |
c.Assert(err, IsNil) |
| 229 |
c.Assert(obj.Hash(), Equals, expected) |
| 230 |
} |
| 231 |
|
| 232 |
func (s *FsSuite) TestIter(c *C) { |
| 233 |
fixtures.ByTag(".git").ByTag("packfile").Test(c, func(f *fixtures.Fixture) { |
| 234 |
fs := f.DotGit() |
| 235 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 236 |
|
| 237 |
iter, err := o.IterEncodedObjects(plumbing.AnyObject) |
| 238 |
c.Assert(err, IsNil) |
| 239 |
|
| 240 |
var count int32 |
| 241 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 242 |
count++ |
| 243 |
return nil |
| 244 |
}) |
| 245 |
|
| 246 |
c.Assert(err, IsNil) |
| 247 |
c.Assert(count, Equals, f.ObjectsCount) |
| 248 |
}) |
| 249 |
} |
| 250 |
|
| 251 |
func (s *FsSuite) TestIterLargeObjectThreshold(c *C) { |
| 252 |
fixtures.ByTag(".git").ByTag("packfile").Test(c, func(f *fixtures.Fixture) { |
| 253 |
fs := f.DotGit() |
| 254 |
o := NewObjectStorageWithOptions(dotgit.New(fs), cache.NewObjectLRUDefault(), Options{LargeObjectThreshold: 1}) |
| 255 |
|
| 256 |
iter, err := o.IterEncodedObjects(plumbing.AnyObject) |
| 257 |
c.Assert(err, IsNil) |
| 258 |
|
| 259 |
var count int32 |
| 260 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 261 |
count++ |
| 262 |
return nil |
| 263 |
}) |
| 264 |
|
| 265 |
c.Assert(err, IsNil) |
| 266 |
c.Assert(count, Equals, f.ObjectsCount) |
| 267 |
}) |
| 268 |
} |
| 269 |
|
| 270 |
func (s *FsSuite) TestIterWithType(c *C) { |
| 271 |
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 272 |
for _, t := range objectTypes { |
| 273 |
fs := f.DotGit() |
| 274 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 275 |
|
| 276 |
iter, err := o.IterEncodedObjects(t) |
| 277 |
c.Assert(err, IsNil) |
| 278 |
|
| 279 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 280 |
c.Assert(o.Type(), Equals, t) |
| 281 |
return nil |
| 282 |
}) |
| 283 |
|
| 284 |
c.Assert(err, IsNil) |
| 285 |
} |
| 286 |
}) |
| 287 |
} |
| 288 |
|
| 289 |
func (s *FsSuite) TestPackfileIter(c *C) { |
| 290 |
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 291 |
fs := f.DotGit() |
| 292 |
dg := dotgit.New(fs) |
| 293 |
|
| 294 |
for _, t := range objectTypes { |
| 295 |
ph, err := dg.ObjectPacks() |
| 296 |
c.Assert(err, IsNil) |
| 297 |
|
| 298 |
for _, h := range ph { |
| 299 |
f, err := dg.ObjectPack(h) |
| 300 |
c.Assert(err, IsNil) |
| 301 |
|
| 302 |
idxf, err := dg.ObjectPackIdx(h) |
| 303 |
c.Assert(err, IsNil) |
| 304 |
|
| 305 |
iter, err := NewPackfileIter(fs, f, idxf, t, false, 0) |
| 306 |
c.Assert(err, IsNil) |
| 307 |
|
| 308 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 309 |
c.Assert(o.Type(), Equals, t) |
| 310 |
return nil |
| 311 |
}) |
| 312 |
c.Assert(err, IsNil) |
| 313 |
} |
| 314 |
} |
| 315 |
}) |
| 316 |
} |
| 317 |
|
| 318 |
func copyFile(c *C, dstDir, dstFilename string, srcFile billy.File) { |
| 319 |
_, err := srcFile.Seek(0, 0) |
| 320 |
c.Assert(err, IsNil) |
| 321 |
|
| 322 |
err = osfs.Default.MkdirAll(dstDir, 0750|os.ModeDir) |
| 323 |
c.Assert(err, IsNil) |
| 324 |
|
| 325 |
dst, err := osfs.Default.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666) |
| 326 |
c.Assert(err, IsNil) |
| 327 |
defer dst.Close() |
| 328 |
|
| 329 |
_, err = io.Copy(dst, srcFile) |
| 330 |
c.Assert(err, IsNil) |
| 331 |
} |
| 332 |
|
| 333 |
// TestPackfileReindex tests that externally-added packfiles are considered by go-git |
| 334 |
// after calling the Reindex method |
| 335 |
func (s *FsSuite) TestPackfileReindex(c *C) { |
| 336 |
// obtain a standalone packfile that is not part of any other repository |
| 337 |
// in the fixtures: |
| 338 |
packFixture := fixtures.ByTag("packfile").ByTag("standalone").One() |
| 339 |
packFile := packFixture.Packfile() |
| 340 |
idxFile := packFixture.Idx() |
| 341 |
packFilename := packFixture.PackfileHash |
| 342 |
testObjectHash := plumbing.NewHash("a771b1e94141480861332fd0e4684d33071306c6") // this is an object we know exists in the standalone packfile |
| 343 |
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 344 |
fs := f.DotGit() |
| 345 |
storer := NewStorage(fs, cache.NewObjectLRUDefault()) |
| 346 |
|
| 347 |
// check that our test object is NOT found |
| 348 |
_, err := storer.EncodedObject(plumbing.CommitObject, testObjectHash) |
| 349 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 350 |
|
| 351 |
// add the external packfile+idx to the packs folder |
| 352 |
// this simulates a git bundle unbundle command, or a repack, for example. |
| 353 |
copyFile(c, filepath.Join(storer.Filesystem().Root(), "objects", "pack"), |
| 354 |
fmt.Sprintf("pack-%s.pack", packFilename), packFile) |
| 355 |
copyFile(c, filepath.Join(storer.Filesystem().Root(), "objects", "pack"), |
| 356 |
fmt.Sprintf("pack-%s.idx", packFilename), idxFile) |
| 357 |
|
| 358 |
// check that we cannot still retrieve the test object |
| 359 |
_, err = storer.EncodedObject(plumbing.CommitObject, testObjectHash) |
| 360 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 361 |
|
| 362 |
storer.Reindex() // actually reindex |
| 363 |
|
| 364 |
// Now check that the test object can be retrieved |
| 365 |
_, err = storer.EncodedObject(plumbing.CommitObject, testObjectHash) |
| 366 |
c.Assert(err, IsNil) |
| 367 |
}) |
| 368 |
} |
| 369 |
|
| 370 |
func (s *FsSuite) TestPackfileIterKeepDescriptors(c *C) { |
| 371 |
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 372 |
fs := f.DotGit() |
| 373 |
ops := dotgit.Options{KeepDescriptors: true} |
| 374 |
dg := dotgit.NewWithOptions(fs, ops) |
| 375 |
|
| 376 |
for _, t := range objectTypes { |
| 377 |
ph, err := dg.ObjectPacks() |
| 378 |
c.Assert(err, IsNil) |
| 379 |
|
| 380 |
for _, h := range ph { |
| 381 |
f, err := dg.ObjectPack(h) |
| 382 |
c.Assert(err, IsNil) |
| 383 |
|
| 384 |
idxf, err := dg.ObjectPackIdx(h) |
| 385 |
c.Assert(err, IsNil) |
| 386 |
|
| 387 |
iter, err := NewPackfileIter(fs, f, idxf, t, true, 0) |
| 388 |
c.Assert(err, IsNil) |
| 389 |
|
| 390 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 391 |
c.Assert(o.Type(), Equals, t) |
| 392 |
return nil |
| 393 |
}) |
| 394 |
c.Assert(err, IsNil) |
| 395 |
|
| 396 |
// test twice to check that packfiles are not closed |
| 397 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 398 |
c.Assert(o.Type(), Equals, t) |
| 399 |
return nil |
| 400 |
}) |
| 401 |
c.Assert(err, IsNil) |
| 402 |
} |
| 403 |
} |
| 404 |
}) |
| 405 |
} |
| 406 |
|
| 407 |
func (s *FsSuite) TestGetFromObjectFileSharedCache(c *C) { |
| 408 |
f1 := fixtures.ByTag("worktree").One().DotGit() |
| 409 |
f2 := fixtures.ByTag("worktree").ByTag("submodule").One().DotGit() |
| 410 |
|
| 411 |
ch := cache.NewObjectLRUDefault() |
| 412 |
o1 := NewObjectStorage(dotgit.New(f1), ch) |
| 413 |
o2 := NewObjectStorage(dotgit.New(f2), ch) |
| 414 |
|
| 415 |
expected := plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a") |
| 416 |
obj, err := o1.EncodedObject(plumbing.CommitObject, expected) |
| 417 |
c.Assert(err, IsNil) |
| 418 |
c.Assert(obj.Hash(), Equals, expected) |
| 419 |
|
| 420 |
_, err = o2.EncodedObject(plumbing.CommitObject, expected) |
| 421 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 422 |
} |
| 423 |
|
| 424 |
func (s *FsSuite) TestHashesWithPrefix(c *C) { |
| 425 |
// Same setup as TestGetFromObjectFile. |
| 426 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 427 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 428 |
expected := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") |
| 429 |
obj, err := o.EncodedObject(plumbing.AnyObject, expected) |
| 430 |
c.Assert(err, IsNil) |
| 431 |
c.Assert(obj.Hash(), Equals, expected) |
| 432 |
|
| 433 |
prefix, _ := hex.DecodeString("f3dfe2") |
| 434 |
hashes, err := o.HashesWithPrefix(prefix) |
| 435 |
c.Assert(err, IsNil) |
| 436 |
c.Assert(hashes, HasLen, 1) |
| 437 |
c.Assert(hashes[0].String(), Equals, "f3dfe29d268303fc6e1bbce268605fc99573406e") |
| 438 |
} |
| 439 |
|
| 440 |
func (s *FsSuite) TestHashesWithPrefixFromPackfile(c *C) { |
| 441 |
// Same setup as TestGetFromPackfile |
| 442 |
fixtures.Basic().ByTag(".git").Test(c, func(f *fixtures.Fixture) { |
| 443 |
fs := f.DotGit() |
| 444 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 445 |
|
| 446 |
expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 447 |
// Only pass the first 8 bytes |
| 448 |
hashes, err := o.HashesWithPrefix(expected[:8]) |
| 449 |
c.Assert(err, IsNil) |
| 450 |
c.Assert(hashes, HasLen, 1) |
| 451 |
c.Assert(hashes[0], Equals, expected) |
| 452 |
}) |
| 453 |
} |
| 454 |
|
| 455 |
func BenchmarkPackfileIter(b *testing.B) { |
| 456 |
defer fixtures.Clean() |
| 457 |
|
| 458 |
for _, f := range fixtures.ByTag(".git") { |
| 459 |
b.Run(f.URL, func(b *testing.B) { |
| 460 |
fs := f.DotGit() |
| 461 |
dg := dotgit.New(fs) |
| 462 |
|
| 463 |
for i := 0; i < b.N; i++ { |
| 464 |
for _, t := range objectTypes { |
| 465 |
ph, err := dg.ObjectPacks() |
| 466 |
if err != nil { |
| 467 |
b.Fatal(err) |
| 468 |
} |
| 469 |
|
| 470 |
for _, h := range ph { |
| 471 |
f, err := dg.ObjectPack(h) |
| 472 |
if err != nil { |
| 473 |
b.Fatal(err) |
| 474 |
} |
| 475 |
|
| 476 |
idxf, err := dg.ObjectPackIdx(h) |
| 477 |
if err != nil { |
| 478 |
b.Fatal(err) |
| 479 |
} |
| 480 |
|
| 481 |
iter, err := NewPackfileIter(fs, f, idxf, t, false, 0) |
| 482 |
if err != nil { |
| 483 |
b.Fatal(err) |
| 484 |
} |
| 485 |
|
| 486 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 487 |
if o.Type() != t { |
| 488 |
b.Errorf("expecting %s, got %s", t, o.Type()) |
| 489 |
} |
| 490 |
return nil |
| 491 |
}) |
| 492 |
if err != nil { |
| 493 |
b.Fatal(err) |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
}) |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
func BenchmarkPackfileIterReadContent(b *testing.B) { |
| 503 |
defer fixtures.Clean() |
| 504 |
|
| 505 |
for _, f := range fixtures.ByTag(".git") { |
| 506 |
b.Run(f.URL, func(b *testing.B) { |
| 507 |
fs := f.DotGit() |
| 508 |
dg := dotgit.New(fs) |
| 509 |
|
| 510 |
for i := 0; i < b.N; i++ { |
| 511 |
for _, t := range objectTypes { |
| 512 |
ph, err := dg.ObjectPacks() |
| 513 |
if err != nil { |
| 514 |
b.Fatal(err) |
| 515 |
} |
| 516 |
|
| 517 |
for _, h := range ph { |
| 518 |
f, err := dg.ObjectPack(h) |
| 519 |
if err != nil { |
| 520 |
b.Fatal(err) |
| 521 |
} |
| 522 |
|
| 523 |
idxf, err := dg.ObjectPackIdx(h) |
| 524 |
if err != nil { |
| 525 |
b.Fatal(err) |
| 526 |
} |
| 527 |
|
| 528 |
iter, err := NewPackfileIter(fs, f, idxf, t, false, 0) |
| 529 |
if err != nil { |
| 530 |
b.Fatal(err) |
| 531 |
} |
| 532 |
|
| 533 |
err = iter.ForEach(func(o plumbing.EncodedObject) error { |
| 534 |
if o.Type() != t { |
| 535 |
b.Errorf("expecting %s, got %s", t, o.Type()) |
| 536 |
} |
| 537 |
|
| 538 |
r, err := o.Reader() |
| 539 |
if err != nil { |
| 540 |
b.Fatal(err) |
| 541 |
} |
| 542 |
|
| 543 |
if _, err := io.ReadAll(r); err != nil { |
| 544 |
b.Fatal(err) |
| 545 |
} |
| 546 |
|
| 547 |
return r.Close() |
| 548 |
}) |
| 549 |
if err != nil { |
| 550 |
b.Fatal(err) |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
}) |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
func BenchmarkGetObjectFromPackfile(b *testing.B) { |
| 560 |
defer fixtures.Clean() |
| 561 |
|
| 562 |
for _, f := range fixtures.Basic() { |
| 563 |
b.Run(f.URL, func(b *testing.B) { |
| 564 |
fs := f.DotGit() |
| 565 |
o := NewObjectStorage(dotgit.New(fs), cache.NewObjectLRUDefault()) |
| 566 |
for i := 0; i < b.N; i++ { |
| 567 |
expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 568 |
obj, err := o.EncodedObject(plumbing.AnyObject, expected) |
| 569 |
if err != nil { |
| 570 |
b.Fatal(err) |
| 571 |
} |
| 572 |
|
| 573 |
if obj.Hash() != expected { |
| 574 |
b.Errorf("expecting %s, got %s", expected, obj.Hash()) |
| 575 |
} |
| 576 |
} |
| 577 |
}) |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
func (s *FsSuite) TestGetFromUnpackedCachesObjects(c *C) { |
| 582 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 583 |
objectCache := cache.NewObjectLRUDefault() |
| 584 |
objectStorage := NewObjectStorage(dotgit.New(fs), objectCache) |
| 585 |
hash := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") |
| 586 |
|
| 587 |
// Assert the cache is empty initially |
| 588 |
_, ok := objectCache.Get(hash) |
| 589 |
c.Assert(ok, Equals, false) |
| 590 |
|
| 591 |
// Load the object |
| 592 |
obj, err := objectStorage.EncodedObject(plumbing.AnyObject, hash) |
| 593 |
c.Assert(err, IsNil) |
| 594 |
c.Assert(obj.Hash(), Equals, hash) |
| 595 |
|
| 596 |
// The object should've been cached during the load |
| 597 |
cachedObj, ok := objectCache.Get(hash) |
| 598 |
c.Assert(ok, Equals, true) |
| 599 |
c.Assert(cachedObj, DeepEquals, obj) |
| 600 |
|
| 601 |
// Assert that both objects can be read and that they both produce the same bytes |
| 602 |
|
| 603 |
objReader, err := obj.Reader() |
| 604 |
c.Assert(err, IsNil) |
| 605 |
objBytes, err := io.ReadAll(objReader) |
| 606 |
c.Assert(err, IsNil) |
| 607 |
c.Assert(len(objBytes), Not(Equals), 0) |
| 608 |
err = objReader.Close() |
| 609 |
c.Assert(err, IsNil) |
| 610 |
|
| 611 |
cachedObjReader, err := cachedObj.Reader() |
| 612 |
c.Assert(err, IsNil) |
| 613 |
cachedObjBytes, err := io.ReadAll(cachedObjReader) |
| 614 |
c.Assert(len(cachedObjBytes), Not(Equals), 0) |
| 615 |
c.Assert(err, IsNil) |
| 616 |
err = cachedObjReader.Close() |
| 617 |
c.Assert(err, IsNil) |
| 618 |
|
| 619 |
c.Assert(cachedObjBytes, DeepEquals, objBytes) |
| 620 |
} |
| 621 |
|
| 622 |
func (s *FsSuite) TestGetFromUnpackedDoesNotCacheLargeObjects(c *C) { |
| 623 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 624 |
objectCache := cache.NewObjectLRUDefault() |
| 625 |
objectStorage := NewObjectStorageWithOptions(dotgit.New(fs), objectCache, Options{LargeObjectThreshold: 1}) |
| 626 |
hash := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") |
| 627 |
|
| 628 |
// Assert the cache is empty initially |
| 629 |
_, ok := objectCache.Get(hash) |
| 630 |
c.Assert(ok, Equals, false) |
| 631 |
|
| 632 |
// Load the object |
| 633 |
obj, err := objectStorage.EncodedObject(plumbing.AnyObject, hash) |
| 634 |
c.Assert(err, IsNil) |
| 635 |
c.Assert(obj.Hash(), Equals, hash) |
| 636 |
|
| 637 |
// The object should not have been cached during the load |
| 638 |
_, ok = objectCache.Get(hash) |
| 639 |
c.Assert(ok, Equals, false) |
| 640 |
} |