|
1 | 1 | package filesystem
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io" |
4 | 6 | "io/ioutil"
|
5 | 7 | "os"
|
| 8 | + "path/filepath" |
6 | 9 | "testing"
|
7 | 10 |
|
8 | 11 | "gopkg.in/src-d/go-git.v4/plumbing"
|
@@ -204,6 +207,59 @@ func (s *FsSuite) TestPackfileIter(c *C) {
|
204 | 207 | })
|
205 | 208 | }
|
206 | 209 |
|
| 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 | + |
207 | 263 | func (s *FsSuite) TestPackfileIterKeepDescriptors(c *C) {
|
208 | 264 | fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) {
|
209 | 265 | fs := f.DotGit()
|
|
0 commit comments