Skip to content

Commit 17f1dfc

Browse files
committed
Cache RawData for Commit, Tag, & Tree, fixes #6
1 parent 479420c commit 17f1dfc

File tree

4 files changed

+84
-57
lines changed

4 files changed

+84
-57
lines changed

Diff for: commit.go

+39-31
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77
"errors"
88
"fmt"
99
"strconv"
10+
"sync"
1011

1112
cid "github.com/ipfs/go-cid"
1213
node "github.com/ipfs/go-ipld-format"
1314
)
1415

1516
type Commit struct {
1617
DataSize string `json:"-"`
17-
GitTree cid.Cid `json:"tree"`
18-
Parents []cid.Cid `json:"parents"`
18+
GitTree cid.Cid `json:"tree"`
19+
Parents []cid.Cid `json:"parents"`
1920
Message string `json:"message"`
2021
Author *PersonInfo `json:"author"`
2122
Committer *PersonInfo `json:"committer"`
@@ -27,6 +28,9 @@ type Commit struct {
2728
Other []string `json:"other,omitempty"`
2829

2930
cid cid.Cid
31+
32+
rawData []byte
33+
rawDataOnce sync.Once
3034
}
3135

3236
type PersonInfo struct {
@@ -80,7 +84,7 @@ func (pi *PersonInfo) resolve(p []string) (interface{}, []string, error) {
8084
}
8185

8286
type MergeTag struct {
83-
Object cid.Cid `json:"object"`
87+
Object cid.Cid `json:"object"`
8488
Type string `json:"type"`
8589
Tag string `json:"tag"`
8690
Tagger *PersonInfo `json:"tagger"`
@@ -118,34 +122,38 @@ func (c *Commit) Loggable() map[string]interface{} {
118122
}
119123

120124
func (c *Commit) RawData() []byte {
121-
buf := new(bytes.Buffer)
122-
fmt.Fprintf(buf, "commit %s\x00", c.DataSize)
123-
fmt.Fprintf(buf, "tree %s\n", hex.EncodeToString(cidToSha(c.GitTree)))
124-
for _, p := range c.Parents {
125-
fmt.Fprintf(buf, "parent %s\n", hex.EncodeToString(cidToSha(p)))
126-
}
127-
fmt.Fprintf(buf, "author %s\n", c.Author.String())
128-
fmt.Fprintf(buf, "committer %s\n", c.Committer.String())
129-
if len(c.Encoding) > 0 {
130-
fmt.Fprintf(buf, "encoding %s\n", c.Encoding)
131-
}
132-
for _, mtag := range c.MergeTag {
133-
fmt.Fprintf(buf, "mergetag object %s\n", hex.EncodeToString(cidToSha(mtag.Object)))
134-
fmt.Fprintf(buf, " type %s\n", mtag.Type)
135-
fmt.Fprintf(buf, " tag %s\n", mtag.Tag)
136-
fmt.Fprintf(buf, " tagger %s\n \n", mtag.Tagger.String())
137-
fmt.Fprintf(buf, "%s", mtag.Text)
138-
}
139-
if c.Sig != nil {
140-
fmt.Fprintln(buf, "gpgsig -----BEGIN PGP SIGNATURE-----")
141-
fmt.Fprint(buf, c.Sig.Text)
142-
fmt.Fprintln(buf, " -----END PGP SIGNATURE-----")
143-
}
144-
for _, line := range c.Other {
145-
fmt.Fprintln(buf, line)
146-
}
147-
fmt.Fprintf(buf, "\n%s", c.Message)
148-
return buf.Bytes()
125+
c.rawDataOnce.Do(func() {
126+
buf := new(bytes.Buffer)
127+
fmt.Fprintf(buf, "commit %s\x00", c.DataSize)
128+
fmt.Fprintf(buf, "tree %s\n", hex.EncodeToString(cidToSha(c.GitTree)))
129+
for _, p := range c.Parents {
130+
fmt.Fprintf(buf, "parent %s\n", hex.EncodeToString(cidToSha(p)))
131+
}
132+
fmt.Fprintf(buf, "author %s\n", c.Author.String())
133+
fmt.Fprintf(buf, "committer %s\n", c.Committer.String())
134+
if len(c.Encoding) > 0 {
135+
fmt.Fprintf(buf, "encoding %s\n", c.Encoding)
136+
}
137+
for _, mtag := range c.MergeTag {
138+
fmt.Fprintf(buf, "mergetag object %s\n", hex.EncodeToString(cidToSha(mtag.Object)))
139+
fmt.Fprintf(buf, " type %s\n", mtag.Type)
140+
fmt.Fprintf(buf, " tag %s\n", mtag.Tag)
141+
fmt.Fprintf(buf, " tagger %s\n \n", mtag.Tagger.String())
142+
fmt.Fprintf(buf, "%s", mtag.Text)
143+
}
144+
if c.Sig != nil {
145+
fmt.Fprintln(buf, "gpgsig -----BEGIN PGP SIGNATURE-----")
146+
fmt.Fprint(buf, c.Sig.Text)
147+
fmt.Fprintln(buf, " -----END PGP SIGNATURE-----")
148+
}
149+
for _, line := range c.Other {
150+
fmt.Fprintln(buf, line)
151+
}
152+
fmt.Fprintf(buf, "\n%s", c.Message)
153+
c.rawData = buf.Bytes()
154+
})
155+
156+
return c.rawData
149157
}
150158

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

Diff for: git_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10+
"reflect"
1011
"strings"
1112
"testing"
1213

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

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

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

249+
assert(t, reflect.DeepEqual(tree.RawData(), tree.RawData()))
246250
assert(t, tree.entries != nil)
247251
assert(t, tree.Tree("", 0) == nil)
248252
}

Diff for: tag.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@ package ipldgit
33
import (
44
"bytes"
55
"encoding/hex"
6+
"errors"
67
"fmt"
8+
"sync"
79

8-
"errors"
910
cid "github.com/ipfs/go-cid"
1011
node "github.com/ipfs/go-ipld-format"
1112
)
1213

1314
type Tag struct {
14-
Object cid.Cid `json:"object"`
15+
Object cid.Cid `json:"object"`
1516
Type string `json:"type"`
1617
Tag string `json:"tag"`
1718
Tagger *PersonInfo `json:"tagger"`
1819
Message string `json:"message"`
1920
dataSize string
2021

2122
cid cid.Cid
23+
24+
rawData []byte
25+
rawDataOnce sync.Once
2226
}
2327

2428
func (t *Tag) Cid() cid.Cid {
@@ -41,18 +45,22 @@ func (t *Tag) Loggable() map[string]interface{} {
4145
}
4246

4347
func (t *Tag) RawData() []byte {
44-
buf := new(bytes.Buffer)
45-
fmt.Fprintf(buf, "tag %s\x00", t.dataSize)
46-
fmt.Fprintf(buf, "object %s\n", hex.EncodeToString(cidToSha(t.Object)))
47-
fmt.Fprintf(buf, "type %s\n", t.Type)
48-
fmt.Fprintf(buf, "tag %s\n", t.Tag)
49-
if t.Tagger != nil {
50-
fmt.Fprintf(buf, "tagger %s\n", t.Tagger.String())
51-
}
52-
if t.Message != "" {
53-
fmt.Fprintf(buf, "\n%s", t.Message)
54-
}
55-
return buf.Bytes()
48+
t.rawDataOnce.Do(func() {
49+
buf := new(bytes.Buffer)
50+
fmt.Fprintf(buf, "tag %s\x00", t.dataSize)
51+
fmt.Fprintf(buf, "object %s\n", hex.EncodeToString(cidToSha(t.Object)))
52+
fmt.Fprintf(buf, "type %s\n", t.Type)
53+
fmt.Fprintf(buf, "tag %s\n", t.Tag)
54+
if t.Tagger != nil {
55+
fmt.Fprintf(buf, "tagger %s\n", t.Tagger.String())
56+
}
57+
if t.Message != "" {
58+
fmt.Fprintf(buf, "\n%s", t.Message)
59+
}
60+
t.rawData = buf.Bytes()
61+
})
62+
63+
return t.rawData
5664
}
5765

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

Diff for: tree.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@ package ipldgit
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
9+
"sync"
810

9-
"errors"
1011
cid "github.com/ipfs/go-cid"
1112
node "github.com/ipfs/go-ipld-format"
1213
)
1314

1415
type Tree struct {
15-
entries map[string]*TreeEntry
16-
size int
17-
order []string
18-
cid cid.Cid
16+
entries map[string]*TreeEntry
17+
size int
18+
order []string
19+
cid cid.Cid
20+
rawData []byte
21+
rawDataOnce sync.Once
1922
}
2023

2124
type TreeEntry struct {
2225
name string
23-
Mode string `json:"mode"`
26+
Mode string `json:"mode"`
2427
Hash cid.Cid `json:"hash"`
2528
}
2629

@@ -95,13 +98,17 @@ func (t *Tree) Loggable() map[string]interface{} {
9598
}
9699

97100
func (t *Tree) RawData() []byte {
98-
buf := new(bytes.Buffer)
101+
t.rawDataOnce.Do(func() {
102+
buf := new(bytes.Buffer)
99103

100-
fmt.Fprintf(buf, "tree %d\x00", t.size)
101-
for _, s := range t.order {
102-
t.entries[s].WriteTo(buf)
103-
}
104-
return buf.Bytes()
104+
fmt.Fprintf(buf, "tree %d\x00", t.size)
105+
for _, s := range t.order {
106+
t.entries[s].WriteTo(buf)
107+
}
108+
t.rawData = buf.Bytes()
109+
})
110+
111+
return t.rawData
105112
}
106113

107114
func (t *Tree) Resolve(p []string) (interface{}, []string, error) {

0 commit comments

Comments
 (0)