Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precompute Blob CID, fixes #21 #27

Merged
merged 1 commit into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 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 Down