Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 2431109

Browse files
authored
Merge pull request #1019 from epiclabs-io/reindex
storage/filesystem: Added reindex method to reindex packfiles
2 parents 7853ab6 + ff47322 commit 2431109

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

storage/filesystem/object.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func (s *ObjectStorage) requireIndex() error {
6161
return nil
6262
}
6363

64+
// Reindex indexes again all packfiles. Useful if git changed packfiles externally
65+
func (s *ObjectStorage) Reindex() {
66+
s.index = nil
67+
}
68+
6469
func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) (err error) {
6570
f, err := s.dir.ObjectPackIdx(h)
6671
if err != nil {

storage/filesystem/object_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package filesystem
22

33
import (
4+
"fmt"
5+
"io"
46
"io/ioutil"
57
"os"
8+
"path/filepath"
69
"testing"
710

811
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -204,6 +207,59 @@ func (s *FsSuite) TestPackfileIter(c *C) {
204207
})
205208
}
206209

210+
func copyFile(c *C, dstDir, dstFilename string, srcFile *os.File) {
211+
_, err := srcFile.Seek(0, 0)
212+
c.Assert(err, IsNil)
213+
214+
err = os.MkdirAll(dstDir, 0750|os.ModeDir)
215+
c.Assert(err, IsNil)
216+
217+
dst, err := os.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666)
218+
c.Assert(err, IsNil)
219+
defer dst.Close()
220+
221+
_, err = io.Copy(dst, srcFile)
222+
c.Assert(err, IsNil)
223+
}
224+
225+
// TestPackfileReindex tests that externally-added packfiles are considered by go-git
226+
// after calling the Reindex method
227+
func (s *FsSuite) TestPackfileReindex(c *C) {
228+
// obtain a standalone packfile that is not part of any other repository
229+
// in the fixtures:
230+
packFixture := fixtures.ByTag("packfile").ByTag("standalone").One()
231+
packFile := packFixture.Packfile()
232+
idxFile := packFixture.Idx()
233+
packFilename := packFixture.PackfileHash.String()
234+
testObjectHash := plumbing.NewHash("a771b1e94141480861332fd0e4684d33071306c6") // this is an object we know exists in the standalone packfile
235+
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) {
236+
fs := f.DotGit()
237+
storer := NewStorage(fs, cache.NewObjectLRUDefault())
238+
239+
// check that our test object is NOT found
240+
_, err := storer.EncodedObject(plumbing.CommitObject, testObjectHash)
241+
c.Assert(err, Equals, plumbing.ErrObjectNotFound)
242+
243+
// add the external packfile+idx to the packs folder
244+
// this simulates a git bundle unbundle command, or a repack, for example.
245+
copyFile(c, filepath.Join(storer.Filesystem().Root(), "objects", "pack"),
246+
fmt.Sprintf("pack-%s.pack", packFilename), packFile)
247+
copyFile(c, filepath.Join(storer.Filesystem().Root(), "objects", "pack"),
248+
fmt.Sprintf("pack-%s.idx", packFilename), idxFile)
249+
250+
// check that we cannot still retrieve the test object
251+
_, err = storer.EncodedObject(plumbing.CommitObject, testObjectHash)
252+
c.Assert(err, Equals, plumbing.ErrObjectNotFound)
253+
254+
storer.Reindex() // actually reindex
255+
256+
// Now check that the test object can be retrieved
257+
_, err = storer.EncodedObject(plumbing.CommitObject, testObjectHash)
258+
c.Assert(err, IsNil)
259+
260+
})
261+
}
262+
207263
func (s *FsSuite) TestPackfileIterKeepDescriptors(c *C) {
208264
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) {
209265
fs := f.DotGit()

0 commit comments

Comments
 (0)