aboutsummaryrefslogtreecommitdiffstats
path: root/storage
diff options
context:
space:
mode:
Diffstat (limited to 'storage')
-rw-r--r--storage/filesystem/object.go5
-rw-r--r--storage/filesystem/object_test.go56
2 files changed, 61 insertions, 0 deletions
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go
index 6cd2d4c..57dcbb4 100644
--- a/storage/filesystem/object.go
+++ b/storage/filesystem/object.go
@@ -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 {
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()