aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plumbing/format/idxfile/idxfile.go2
-rw-r--r--plumbing/format/packfile/decoder.go2
-rw-r--r--plumbing/format/packfile/index_test.go133
-rw-r--r--storage/filesystem/index.go47
4 files changed, 49 insertions, 135 deletions
diff --git a/plumbing/format/idxfile/idxfile.go b/plumbing/format/idxfile/idxfile.go
index f8debb1..f57df2e 100644
--- a/plumbing/format/idxfile/idxfile.go
+++ b/plumbing/format/idxfile/idxfile.go
@@ -87,7 +87,7 @@ func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) int {
low = mid + 1
}
- if low > high {
+ if low >= high {
break
}
}
diff --git a/plumbing/format/packfile/decoder.go b/plumbing/format/packfile/decoder.go
index 69aef2d..87c347f 100644
--- a/plumbing/format/packfile/decoder.go
+++ b/plumbing/format/packfile/decoder.go
@@ -457,7 +457,7 @@ func (d *Decoder) recallByOffset(o int64) (plumbing.EncodedObject, error) {
func (d *Decoder) recallByHash(h plumbing.Hash) (plumbing.EncodedObject, error) {
if d.s.IsSeekable {
- if offset, err := d.idx.FindOffset(h); err != nil {
+ if offset, err := d.idx.FindOffset(h); err == nil {
return d.DecodeObjectAt(offset)
}
}
diff --git a/plumbing/format/packfile/index_test.go b/plumbing/format/packfile/index_test.go
deleted file mode 100644
index 8de886d..0000000
--- a/plumbing/format/packfile/index_test.go
+++ /dev/null
@@ -1,133 +0,0 @@
-package packfile
-
-import (
- "strconv"
- "strings"
- "testing"
-
- "gopkg.in/src-d/go-git.v4/plumbing"
-
- . "gopkg.in/check.v1"
-)
-
-type IndexSuite struct{}
-
-var _ = Suite(&IndexSuite{})
-
-func (s *IndexSuite) TestLookupOffset(c *C) {
- idx := NewIndex(0)
-
- for o1 := 0; o1 < 10000; o1 += 100 {
- for o2 := 0; o2 < 10000; o2 += 100 {
- if o2 >= o1 {
- e, ok := idx.LookupOffset(uint64(o2))
- c.Assert(ok, Equals, false)
- c.Assert(e, IsNil)
- } else {
- e, ok := idx.LookupOffset(uint64(o2))
- c.Assert(ok, Equals, true)
- c.Assert(e, NotNil)
- c.Assert(e.Hash, Equals, toHash(o2))
- c.Assert(e.Offset, Equals, uint64(o2))
- }
- }
-
- h1 := toHash(o1)
- idx.Add(h1, uint64(o1), 0)
-
- for o2 := 0; o2 < 10000; o2 += 100 {
- if o2 > o1 {
- e, ok := idx.LookupOffset(uint64(o2))
- c.Assert(ok, Equals, false)
- c.Assert(e, IsNil)
- } else {
- e, ok := idx.LookupOffset(uint64(o2))
- c.Assert(ok, Equals, true)
- c.Assert(e, NotNil)
- c.Assert(e.Hash, Equals, toHash(o2))
- c.Assert(e.Offset, Equals, uint64(o2))
- }
- }
- }
-}
-
-func (s *IndexSuite) TestLookupHash(c *C) {
- idx := NewIndex(0)
-
- for o1 := 0; o1 < 10000; o1 += 100 {
- for o2 := 0; o2 < 10000; o2 += 100 {
- if o2 >= o1 {
- e, ok := idx.LookupHash(toHash(o2))
- c.Assert(ok, Equals, false)
- c.Assert(e, IsNil)
- } else {
- e, ok := idx.LookupHash(toHash(o2))
- c.Assert(ok, Equals, true)
- c.Assert(e, NotNil)
- c.Assert(e.Hash, Equals, toHash(o2))
- c.Assert(e.Offset, Equals, uint64(o2))
- }
- }
-
- h1 := toHash(o1)
- idx.Add(h1, uint64(o1), 0)
-
- for o2 := 0; o2 < 10000; o2 += 100 {
- if o2 > o1 {
- e, ok := idx.LookupHash(toHash(o2))
- c.Assert(ok, Equals, false)
- c.Assert(e, IsNil)
- } else {
- e, ok := idx.LookupHash(toHash(o2))
- c.Assert(ok, Equals, true)
- c.Assert(e, NotNil)
- c.Assert(e.Hash, Equals, toHash(o2))
- c.Assert(e.Offset, Equals, uint64(o2))
- }
- }
- }
-}
-
-func (s *IndexSuite) TestSize(c *C) {
- idx := NewIndex(0)
-
- for o1 := 0; o1 < 1000; o1++ {
- c.Assert(idx.Size(), Equals, o1)
- h1 := toHash(o1)
- idx.Add(h1, uint64(o1), 0)
- }
-}
-
-func (s *IndexSuite) TestIdxFileEmpty(c *C) {
- idx := NewIndex(0)
- idxf := idx.ToIdxFile()
- idx2 := NewIndexFromIdxFile(idxf)
- c.Assert(idx, DeepEquals, idx2)
-}
-
-func (s *IndexSuite) TestIdxFile(c *C) {
- idx := NewIndex(0)
- for o1 := 0; o1 < 1000; o1++ {
- h1 := toHash(o1)
- idx.Add(h1, uint64(o1), 0)
- }
-
- idx2 := NewIndexFromIdxFile(idx.ToIdxFile())
- c.Assert(idx, DeepEquals, idx2)
-}
-
-func toHash(i int) plumbing.Hash {
- is := strconv.Itoa(i)
- padding := strings.Repeat("a", 40-len(is))
- return plumbing.NewHash(padding + is)
-}
-
-func BenchmarkIndexConstruction(b *testing.B) {
- b.ReportAllocs()
-
- idx := NewIndex(0)
- for o := 0; o < 1e6*b.N; o += 100 {
- h1 := toHash(o)
- idx.Add(h1, uint64(o), 0)
- }
-}
diff --git a/storage/filesystem/index.go b/storage/filesystem/index.go
new file mode 100644
index 0000000..2ebf57e
--- /dev/null
+++ b/storage/filesystem/index.go
@@ -0,0 +1,47 @@
+package filesystem
+
+import (
+ "os"
+
+ "gopkg.in/src-d/go-git.v4/plumbing/format/index"
+ "gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
+ "gopkg.in/src-d/go-git.v4/utils/ioutil"
+)
+
+type IndexStorage struct {
+ dir *dotgit.DotGit
+}
+
+func (s *IndexStorage) SetIndex(idx *index.Index) (err error) {
+ f, err := s.dir.IndexWriter()
+ if err != nil {
+ return err
+ }
+
+ defer ioutil.CheckClose(f, &err)
+
+ e := index.NewEncoder(f)
+ err = e.Encode(idx)
+ return err
+}
+
+func (s *IndexStorage) Index() (i *index.Index, err error) {
+ idx := &index.Index{
+ Version: 2,
+ }
+
+ f, err := s.dir.Index()
+ if err != nil {
+ if os.IsNotExist(err) {
+ return idx, nil
+ }
+
+ return nil, err
+ }
+
+ defer ioutil.CheckClose(f, &err)
+
+ d := index.NewDecoder(f)
+ err = d.Decode(idx)
+ return idx, err
+}