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

storage/filesystem: Added reindex method to reindex packfiles #1019

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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