Skip to content

Commit 28e989f

Browse files
committed
Cache RawData for Commit, Tag, & Tree, fixes ipfs#6
1 parent 479420c commit 28e989f

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

commit.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
type Commit struct {
1616
DataSize string `json:"-"`
17-
GitTree cid.Cid `json:"tree"`
18-
Parents []cid.Cid `json:"parents"`
17+
GitTree cid.Cid `json:"tree"`
18+
Parents []cid.Cid `json:"parents"`
1919
Message string `json:"message"`
2020
Author *PersonInfo `json:"author"`
2121
Committer *PersonInfo `json:"committer"`
@@ -27,6 +27,8 @@ type Commit struct {
2727
Other []string `json:"other,omitempty"`
2828

2929
cid cid.Cid
30+
31+
rawdata []byte
3032
}
3133

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

8284
type MergeTag struct {
83-
Object cid.Cid `json:"object"`
85+
Object cid.Cid `json:"object"`
8486
Type string `json:"type"`
8587
Tag string `json:"tag"`
8688
Tagger *PersonInfo `json:"tagger"`
@@ -118,6 +120,10 @@ func (c *Commit) Loggable() map[string]interface{} {
118120
}
119121

120122
func (c *Commit) RawData() []byte {
123+
if c.rawdata != nil {
124+
return c.rawdata
125+
}
126+
121127
buf := new(bytes.Buffer)
122128
fmt.Fprintf(buf, "commit %s\x00", c.DataSize)
123129
fmt.Fprintf(buf, "tree %s\n", hex.EncodeToString(cidToSha(c.GitTree)))
@@ -145,7 +151,9 @@ func (c *Commit) RawData() []byte {
145151
fmt.Fprintln(buf, line)
146152
}
147153
fmt.Fprintf(buf, "\n%s", c.Message)
148-
return buf.Bytes()
154+
c.rawdata = buf.Bytes()
155+
156+
return c.rawdata
149157
}
150158

151159
func (c *Commit) Resolve(path []string) (interface{}, []string, error) {

tag.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ package ipldgit
33
import (
44
"bytes"
55
"encoding/hex"
6+
"errors"
67
"fmt"
78

8-
"errors"
99
cid "github.com/ipfs/go-cid"
1010
node "github.com/ipfs/go-ipld-format"
1111
)
1212

1313
type Tag struct {
14-
Object cid.Cid `json:"object"`
14+
Object cid.Cid `json:"object"`
1515
Type string `json:"type"`
1616
Tag string `json:"tag"`
1717
Tagger *PersonInfo `json:"tagger"`
1818
Message string `json:"message"`
1919
dataSize string
2020

2121
cid cid.Cid
22+
23+
rawdata []byte
2224
}
2325

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

4345
func (t *Tag) RawData() []byte {
46+
if t.rawdata != nil {
47+
return t.rawdata
48+
}
49+
4450
buf := new(bytes.Buffer)
4551
fmt.Fprintf(buf, "tag %s\x00", t.dataSize)
4652
fmt.Fprintf(buf, "object %s\n", hex.EncodeToString(cidToSha(t.Object)))
@@ -52,7 +58,9 @@ func (t *Tag) RawData() []byte {
5258
if t.Message != "" {
5359
fmt.Fprintf(buf, "\n%s", t.Message)
5460
}
55-
return buf.Bytes()
61+
t.rawdata = buf.Bytes()
62+
63+
return t.rawdata
5664
}
5765

5866
func (t *Tag) Resolve(path []string) (interface{}, []string, error) {

tree.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package ipldgit
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89

9-
"errors"
1010
cid "github.com/ipfs/go-cid"
1111
node "github.com/ipfs/go-ipld-format"
1212
)
@@ -16,11 +16,12 @@ type Tree struct {
1616
size int
1717
order []string
1818
cid cid.Cid
19+
rawdata []byte
1920
}
2021

2122
type TreeEntry struct {
2223
name string
23-
Mode string `json:"mode"`
24+
Mode string `json:"mode"`
2425
Hash cid.Cid `json:"hash"`
2526
}
2627

0 commit comments

Comments
 (0)