From ff47322984fc3d5ba2738ee4a996b88f744a2fe7 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Mon, 12 Nov 2018 01:35:10 +0100 Subject: storage/filesystem: Added reindex method to reindex packfiles Signed-off-by: Javier Peletier --- storage/filesystem/object_test.go | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'storage/filesystem/object_test.go') diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 4e6bbfb..77eb31d 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -1,8 +1,11 @@ package filesystem import ( + "fmt" + "io" "io/ioutil" "os" + "path/filepath" "testing" "gopkg.in/src-d/go-git.v4/plumbing" @@ -204,6 +207,59 @@ func (s *FsSuite) TestPackfileIter(c *C) { }) } +func copyFile(c *C, dstDir, dstFilename string, srcFile *os.File) { + _, err := srcFile.Seek(0, 0) + c.Assert(err, IsNil) + + err = os.MkdirAll(dstDir, 0750|os.ModeDir) + c.Assert(err, IsNil) + + dst, err := os.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666) + c.Assert(err, IsNil) + defer dst.Close() + + _, err = io.Copy(dst, srcFile) + c.Assert(err, IsNil) +} + +// TestPackfileReindex tests that externally-added packfiles are considered by go-git +// after calling the Reindex method +func (s *FsSuite) TestPackfileReindex(c *C) { + // obtain a standalone packfile that is not part of any other repository + // in the fixtures: + packFixture := fixtures.ByTag("packfile").ByTag("standalone").One() + packFile := packFixture.Packfile() + idxFile := packFixture.Idx() + packFilename := packFixture.PackfileHash.String() + testObjectHash := plumbing.NewHash("a771b1e94141480861332fd0e4684d33071306c6") // this is an object we know exists in the standalone packfile + fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) { + fs := f.DotGit() + storer := NewStorage(fs, cache.NewObjectLRUDefault()) + + // check that our test object is NOT found + _, err := storer.EncodedObject(plumbing.CommitObject, testObjectHash) + c.Assert(err, Equals, plumbing.ErrObjectNotFound) + + // add the external packfile+idx to the packs folder + // this simulates a git bundle unbundle command, or a repack, for example. + copyFile(c, filepath.Join(storer.Filesystem().Root(), "objects", "pack"), + fmt.Sprintf("pack-%s.pack", packFilename), packFile) + copyFile(c, filepath.Join(storer.Filesystem().Root(), "objects", "pack"), + fmt.Sprintf("pack-%s.idx", packFilename), idxFile) + + // check that we cannot still retrieve the test object + _, err = storer.EncodedObject(plumbing.CommitObject, testObjectHash) + c.Assert(err, Equals, plumbing.ErrObjectNotFound) + + storer.Reindex() // actually reindex + + // Now check that the test object can be retrieved + _, err = storer.EncodedObject(plumbing.CommitObject, testObjectHash) + c.Assert(err, IsNil) + + }) +} + func (s *FsSuite) TestPackfileIterKeepDescriptors(c *C) { fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) { fs := f.DotGit() -- cgit