aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/idxfile
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing/format/idxfile')
-rw-r--r--plumbing/format/idxfile/idxfile.go8
-rw-r--r--plumbing/format/idxfile/writer.go11
2 files changed, 18 insertions, 1 deletions
diff --git a/plumbing/format/idxfile/idxfile.go b/plumbing/format/idxfile/idxfile.go
index d4a9365..71c7630 100644
--- a/plumbing/format/idxfile/idxfile.go
+++ b/plumbing/format/idxfile/idxfile.go
@@ -67,6 +67,10 @@ func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) int {
return -1
}
+ if len(idx.Names) <= k {
+ return -1
+ }
+
data := idx.Names[k]
high := uint64(len(idx.Offset32[k])) >> 2
if high == 0 {
@@ -103,6 +107,10 @@ func (idx *MemoryIndex) Contains(h plumbing.Hash) (bool, error) {
// FindOffset implements the Index interface.
func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
+ if len(idx.FanoutMapping) <= int(h[0]) {
+ return 0, plumbing.ErrObjectNotFound
+ }
+
k := idx.FanoutMapping[h[0]]
i := idx.findHashIndex(h)
if i < 0 {
diff --git a/plumbing/format/idxfile/writer.go b/plumbing/format/idxfile/writer.go
index efcdcc6..a22cf16 100644
--- a/plumbing/format/idxfile/writer.go
+++ b/plumbing/format/idxfile/writer.go
@@ -25,6 +25,7 @@ type Writer struct {
offset64 uint32
finished bool
index *MemoryIndex
+ added map[plumbing.Hash]struct{}
}
// Index returns a previously created MemoryIndex or creates a new one if
@@ -45,7 +46,15 @@ func (w *Writer) Add(h plumbing.Hash, pos uint64, crc uint32) {
w.m.Lock()
defer w.m.Unlock()
- w.objects = append(w.objects, Entry{h, crc, pos})
+ if w.added == nil {
+ w.added = make(map[plumbing.Hash]struct{})
+ }
+
+ if _, ok := w.added[h]; !ok {
+ w.added[h] = struct{}{}
+ w.objects = append(w.objects, Entry{h, crc, pos})
+ }
+
}
func (w *Writer) Finished() bool {