Skip to content

Commit

Permalink
Cache RawData for Commit, Tag, & Tree, fixes ipfs#6
Browse files Browse the repository at this point in the history
  • Loading branch information
sameer committed Dec 18, 2018
1 parent 479420c commit 86cf198
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
16 changes: 12 additions & 4 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

type Commit struct {
DataSize string `json:"-"`
GitTree cid.Cid `json:"tree"`
Parents []cid.Cid `json:"parents"`
GitTree cid.Cid `json:"tree"`
Parents []cid.Cid `json:"parents"`
Message string `json:"message"`
Author *PersonInfo `json:"author"`
Committer *PersonInfo `json:"committer"`
Expand All @@ -27,6 +27,8 @@ type Commit struct {
Other []string `json:"other,omitempty"`

cid cid.Cid

rawdata []byte
}

type PersonInfo struct {
Expand Down Expand Up @@ -80,7 +82,7 @@ func (pi *PersonInfo) resolve(p []string) (interface{}, []string, error) {
}

type MergeTag struct {
Object cid.Cid `json:"object"`
Object cid.Cid `json:"object"`
Type string `json:"type"`
Tag string `json:"tag"`
Tagger *PersonInfo `json:"tagger"`
Expand Down Expand Up @@ -118,6 +120,10 @@ func (c *Commit) Loggable() map[string]interface{} {
}

func (c *Commit) RawData() []byte {
if c.rawdata != nil {
return c.rawdata
}

buf := new(bytes.Buffer)
fmt.Fprintf(buf, "commit %s\x00", c.DataSize)
fmt.Fprintf(buf, "tree %s\n", hex.EncodeToString(cidToSha(c.GitTree)))
Expand Down Expand Up @@ -145,7 +151,9 @@ func (c *Commit) RawData() []byte {
fmt.Fprintln(buf, line)
}
fmt.Fprintf(buf, "\n%s", c.Message)
return buf.Bytes()
c.rawdata = buf.Bytes()

return c.rawdata
}

func (c *Commit) Resolve(path []string) (interface{}, []string, error) {
Expand Down
4 changes: 4 additions & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -171,6 +172,7 @@ func testNode(t *testing.T, nd node.Node) error {

/*s, _ := commit.Size()
assert.Equal(t, len(commit.RawData()), int(s))*/ //TODO: Known breakage
assert(t, reflect.DeepEqual(commit.RawData(), commit.RawData()))
assert(t, commit.GitTree.Defined())
assert(t, commit.Links() != nil)
assert(t, commit.Loggable()["type"] == "git_commit")
Expand Down Expand Up @@ -228,6 +230,7 @@ func testNode(t *testing.T, nd node.Node) error {
}

assert(t, tag.Type == "commit" || tag.Type == "tree" || tag.Type == "blob" || tag.Type == "tag")
assert(t, reflect.DeepEqual(tag.RawData(), tag.RawData()))
assert(t, tag.Object.Defined())
assert(t, tag.Loggable()["type"] == "git_tag")
assert(t, tag.Tree("", -1) != nil)
Expand All @@ -243,6 +246,7 @@ func testNode(t *testing.T, nd node.Node) error {
t.Fatalf("Tree is not a tree")
}

assert(t, reflect.DeepEqual(tree.RawData(), tree.RawData()))
assert(t, tree.entries != nil)
assert(t, tree.Tree("", 0) == nil)
}
Expand Down
14 changes: 11 additions & 3 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ package ipldgit
import (
"bytes"
"encoding/hex"
"errors"
"fmt"

"errors"
cid "github.com/ipfs/go-cid"
node "github.com/ipfs/go-ipld-format"
)

type Tag struct {
Object cid.Cid `json:"object"`
Object cid.Cid `json:"object"`
Type string `json:"type"`
Tag string `json:"tag"`
Tagger *PersonInfo `json:"tagger"`
Message string `json:"message"`
dataSize string

cid cid.Cid

rawdata []byte
}

func (t *Tag) Cid() cid.Cid {
Expand All @@ -41,6 +43,10 @@ func (t *Tag) Loggable() map[string]interface{} {
}

func (t *Tag) RawData() []byte {
if t.rawdata != nil {
return t.rawdata
}

buf := new(bytes.Buffer)
fmt.Fprintf(buf, "tag %s\x00", t.dataSize)
fmt.Fprintf(buf, "object %s\n", hex.EncodeToString(cidToSha(t.Object)))
Expand All @@ -52,7 +58,9 @@ func (t *Tag) RawData() []byte {
if t.Message != "" {
fmt.Fprintf(buf, "\n%s", t.Message)
}
return buf.Bytes()
t.rawdata = buf.Bytes()

return t.rawdata
}

func (t *Tag) Resolve(path []string) (interface{}, []string, error) {
Expand Down
5 changes: 3 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ipldgit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"

"errors"
cid "github.com/ipfs/go-cid"
node "github.com/ipfs/go-ipld-format"
)
Expand All @@ -16,11 +16,12 @@ type Tree struct {
size int
order []string
cid cid.Cid
rawdata []byte
}

type TreeEntry struct {
name string
Mode string `json:"mode"`
Mode string `json:"mode"`
Hash cid.Cid `json:"hash"`
}

Expand Down

0 comments on commit 86cf198

Please sign in to comment.