Skip to content

Commit

Permalink
Merge pull request src-d#1019 from epiclabs-io/reindex
Browse files Browse the repository at this point in the history
storage/filesystem: Added reindex method to  reindex packfiles
  • Loading branch information
mcuadros committed Nov 19, 2018
2 parents 7853ab6 + ff47322 commit 2431109
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions storage/filesystem/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (s *ObjectStorage) requireIndex() error {
return nil
}

// Reindex indexes again all packfiles. Useful if git changed packfiles externally
func (s *ObjectStorage) Reindex() {
s.index = nil
}

func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) (err error) {
f, err := s.dir.ObjectPackIdx(h)
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions storage/filesystem/object_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package filesystem

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"gopkg.in/src-d/go-git.v4/plumbing"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2431109

Please sign in to comment.