Skip to content

Commit

Permalink
Precompute Blob CID, fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
sameer committed Dec 18, 2018
1 parent 479420c commit c2e898f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
49 changes: 22 additions & 27 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,62 @@ import (

cid "github.com/ipfs/go-cid"
node "github.com/ipfs/go-ipld-format"
mh "github.com/multiformats/go-multihash"
)

type Blob []byte
type Blob struct {
rawData []byte
cid cid.Cid
}

func (b Blob) Cid() cid.Cid {
c, _ := cid.Prefix{
MhType: mh.SHA1,
MhLength: -1,
Codec: cid.GitRaw,
Version: 1,
}.Sum([]byte(b))
return c
func (b *Blob) Cid() cid.Cid {
return b.cid
}

func (b Blob) Copy() node.Node {
out := make([]byte, len(b))
copy(out, b)
return Blob(out)
func (b *Blob) Copy() node.Node {
nb := *b
return &nb
}

func (b Blob) Links() []*node.Link {
func (b *Blob) Links() []*node.Link {
return nil
}

func (b Blob) Resolve(_ []string) (interface{}, []string, error) {
func (b *Blob) Resolve(_ []string) (interface{}, []string, error) {
return nil, nil, errors.New("no such link")
}

func (b Blob) ResolveLink(_ []string) (*node.Link, []string, error) {
func (b *Blob) ResolveLink(_ []string) (*node.Link, []string, error) {
return nil, nil, errors.New("no such link")
}

func (b Blob) Loggable() map[string]interface{} {
func (b *Blob) Loggable() map[string]interface{} {
return map[string]interface{}{
"type": "git_blob",
}
}

func (b Blob) RawData() []byte {
return []byte(b)
func (b *Blob) RawData() []byte {
return []byte(b.rawdata)
}

func (b Blob) Size() (uint64, error) {
return uint64(len(b)), nil
func (b *Blob) Size() (uint64, error) {
return uint64(len(b.rawdata)), nil
}

func (b Blob) Stat() (*node.NodeStat, error) {
func (b *Blob) Stat() (*node.NodeStat, error) {
return &node.NodeStat{}, nil
}

func (b Blob) String() string {
func (b *Blob) String() string {
return "[git blob]"
}

func (b Blob) Tree(p string, depth int) []string {
func (b *Blob) Tree(p string, depth int) []string {
return nil
}

func (b Blob) GitSha() []byte {
func (b *Blob) GitSha() []byte {
return cidToSha(b.Cid())
}

var _ node.Node = (Blob)(nil)
var _ node.Node = (*Blob)(nil)
8 changes: 6 additions & 2 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func ParseObject(r io.Reader) (node.Node, error) {
}
}

func ReadBlob(rd *bufio.Reader) (Blob, error) {
func ReadBlob(rd *bufio.Reader) (*Blob, error) {
size, err := rd.ReadString(0)
if err != nil {
return nil, err
Expand All @@ -90,7 +90,11 @@ func ReadBlob(rd *bufio.Reader) (Blob, error) {
return nil, fmt.Errorf("blob size was not accurate")
}

return Blob(buf.Bytes()), nil
out := &Blob{}
out.rawData = buf.Bytes()
out.cid = hashObject(out.RawData())

return out, nil
}

func ReadCommit(rd *bufio.Reader) (*Commit, error) {
Expand Down
3 changes: 2 additions & 1 deletion git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestArchiveObjectParse(t *testing.T) {
func testNode(t *testing.T, nd node.Node) error {
switch nd.String() {
case "[git blob]":
blob, ok := nd.(Blob)
blob, ok := nd.(*Blob)
if !ok {
t.Fatalf("Blob is not a blob")
}
Expand All @@ -163,6 +163,7 @@ func testNode(t *testing.T, nd node.Node) error {

s, _ := blob.Size()
assert(t, len(blob.RawData()) == int(s))
assert(t, blob.Cid() == blob.Cid())
case "[git commit object]":
commit, ok := nd.(*Commit)
if !ok {
Expand Down

0 comments on commit c2e898f

Please sign in to comment.