aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/packfile
diff options
context:
space:
mode:
authorJavi Fontan <jfontan@gmail.com>2018-07-27 18:31:40 +0200
committerJavi Fontan <jfontan@gmail.com>2018-07-27 18:31:40 +0200
commitb4cd0899e24e0e8c7910bcdc33c96dc463dcb1e4 (patch)
treedb17b0a6c92b1f9120f02ba56760f6106cdcf4d8 /plumbing/format/packfile
parent6f8f2ed229cc88a175d6ea47a53135b6dcef6912 (diff)
downloadgo-git-b4cd0899e24e0e8c7910bcdc33c96dc463dcb1e4.tar.gz
plumbing/packfile: add index generation to decoder
Signed-off-by: Javi Fontan <jfontan@gmail.com>
Diffstat (limited to 'plumbing/format/packfile')
-rw-r--r--plumbing/format/packfile/decoder.go32
1 files changed, 25 insertions, 7 deletions
diff --git a/plumbing/format/packfile/decoder.go b/plumbing/format/packfile/decoder.go
index edf386b..d6bc0ef 100644
--- a/plumbing/format/packfile/decoder.go
+++ b/plumbing/format/packfile/decoder.go
@@ -66,6 +66,7 @@ type Decoder struct {
// will be built incrementally while decoding.
hasBuiltIndex bool
idx idxfile.Index
+ writer *idxfile.Writer
offsetToType map[int64]plumbing.ObjectType
decoderType plumbing.ObjectType
@@ -144,7 +145,17 @@ func (d *Decoder) Decode() (checksum plumbing.Hash, err error) {
return plumbing.ZeroHash, err
}
- return d.s.Checksum()
+ checksum, err = d.s.Checksum()
+ if err != nil {
+ return plumbing.ZeroHash, err
+ }
+
+ if !d.hasBuiltIndex {
+ d.writer.OnFooter(checksum)
+ d.idx = d.Index()
+ }
+
+ return checksum, err
}
func (d *Decoder) fillOffsetsToHashes() error {
@@ -177,6 +188,8 @@ func (d *Decoder) doDecode() error {
if !d.hasBuiltIndex {
// TODO: MemoryIndex is not writable, change to something else
d.idx = idxfile.NewMemoryIndex()
+ d.writer = new(idxfile.Writer)
+ d.writer.OnHeader(count)
}
defer func() { d.hasBuiltIndex = true }()
@@ -329,14 +342,15 @@ func (d *Decoder) decodeByHeader(h *ObjectHeader) (plumbing.EncodedObject, error
obj.SetSize(h.Length)
obj.SetType(h.Type)
+ var crc uint32
var err error
switch h.Type {
case plumbing.CommitObject, plumbing.TreeObject, plumbing.BlobObject, plumbing.TagObject:
- _, err = d.fillRegularObjectContent(obj)
+ crc, err = d.fillRegularObjectContent(obj)
case plumbing.REFDeltaObject:
- _, err = d.fillREFDeltaObjectContent(obj, h.Reference)
+ crc, err = d.fillREFDeltaObjectContent(obj, h.Reference)
case plumbing.OFSDeltaObject:
- _, err = d.fillOFSDeltaObjectContent(obj, h.OffsetReference)
+ crc, err = d.fillOFSDeltaObjectContent(obj, h.OffsetReference)
default:
err = ErrInvalidObject.AddDetails("type %q", h.Type)
}
@@ -345,6 +359,10 @@ func (d *Decoder) decodeByHeader(h *ObjectHeader) (plumbing.EncodedObject, error
return obj, err
}
+ if !d.hasBuiltIndex {
+ d.writer.Add(obj.Hash(), uint64(h.Offset), crc)
+ }
+
d.offsetToHash[h.Offset] = obj.Hash()
return obj, nil
@@ -468,9 +486,9 @@ func (d *Decoder) recallByOffset(o int64) (plumbing.EncodedObject, error) {
return d.DecodeObjectAt(o)
}
- hash, err := d.idx.FindHash(o)
- if err != nil {
- return nil, err
+ hash, ok := d.offsetToHash[o]
+ if !ok {
+ return nil, plumbing.ErrObjectNotFound
}
return d.recallByHashNonSeekable(hash)