Skip to content

Commit 6b07b01

Browse files
committed
object_db.go: replace decode with openDecode
In a subsequent commit, we will introduce a function to produce an implementation of Object the concrete type of which is determined by the type associated with the object named "SHA". In order to do so, we would like to be able to call "o.decode()" without having to open the object referred to above more than once (for example, once to check the type, and the second time to read the object's contents). To make this possible, let's teach decode to accept an `*ObjectReader` instead of an `sha []byte`, and defer the responsibility of calling `o.open()` (which roughly translates a `[]byte` into an `*ObjectReader, error)`), to an external caller. To make calling `decode()` as easy as it was before this change, we'll introduce `decodeOpen()`, which first calls `o.open()`, and then passes on either (1) the resulting error, or (2) the result of calling `o.decode()` with the newly opened object.
1 parent 5aa0c18 commit 6b07b01

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

object_db.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (o *ObjectDatabase) Close() error {
7171
func (o *ObjectDatabase) Blob(sha []byte) (*Blob, error) {
7272
var b Blob
7373

74-
if err := o.decode(sha, &b); err != nil {
74+
if err := o.openDecode(sha, &b); err != nil {
7575
return nil, err
7676
}
7777
return &b, nil
@@ -81,7 +81,7 @@ func (o *ObjectDatabase) Blob(sha []byte) (*Blob, error) {
8181
// encountered.
8282
func (o *ObjectDatabase) Tree(sha []byte) (*Tree, error) {
8383
var t Tree
84-
if err := o.decode(sha, &t); err != nil {
84+
if err := o.openDecode(sha, &t); err != nil {
8585
return nil, err
8686
}
8787
return &t, nil
@@ -92,7 +92,7 @@ func (o *ObjectDatabase) Tree(sha []byte) (*Tree, error) {
9292
func (o *ObjectDatabase) Commit(sha []byte) (*Commit, error) {
9393
var c Commit
9494

95-
if err := o.decode(sha, &c); err != nil {
95+
if err := o.openDecode(sha, &c); err != nil {
9696
return nil, err
9797
}
9898
return &c, nil
@@ -103,7 +103,7 @@ func (o *ObjectDatabase) Commit(sha []byte) (*Commit, error) {
103103
func (o *ObjectDatabase) Tag(sha []byte) (*Tag, error) {
104104
var t Tag
105105

106-
if err := o.decode(sha, &t); err != nil {
106+
if err := o.openDecode(sha, &t); err != nil {
107107
return nil, err
108108
}
109109
return &t, nil
@@ -272,6 +272,16 @@ func (o *ObjectDatabase) open(sha []byte) (*ObjectReader, error) {
272272
return NewObjectReadCloser(f)
273273
}
274274

275+
// openDecode calls decode (see: below) on the object named "sha" after openin
276+
// it.
277+
func (o *ObjectDatabase) openDecode(sha []byte, into Object) error {
278+
r, err := o.open(sha)
279+
if err != nil {
280+
return err
281+
}
282+
return o.decode(r, into)
283+
}
284+
275285
// decode decodes an object given by the sha "sha []byte" into the given object
276286
// "into", or returns an error if one was encountered.
277287
//
@@ -280,12 +290,7 @@ func (o *ObjectDatabase) open(sha []byte) (*ObjectReader, error) {
280290
// BlobObjectType. Blob's don't exhaust the buffer completely (they instead
281291
// maintain a handle on the blob's contents via an io.LimitedReader) and
282292
// therefore cannot be closed until signaled explicitly by gitobj.Blob.Close().
283-
func (o *ObjectDatabase) decode(sha []byte, into Object) error {
284-
r, err := o.open(sha)
285-
if err != nil {
286-
return err
287-
}
288-
293+
func (o *ObjectDatabase) decode(r *ObjectReader, into Object) error {
289294
typ, size, err := r.Header()
290295
if err != nil {
291296
return err

0 commit comments

Comments
 (0)