Skip to content

Commit 6324dd6

Browse files
committed
pack: instantiate all packs and indexes with a hash
Whenever we instantiate or parse a pack or an index, pass or include the relevant hash parameter. Update the tests accordingly. Use SHA-256 in one case where the hash makes no actual difference to demonstrate that we parse both algorithms correctly.
1 parent dc977b2 commit 6324dd6

File tree

9 files changed

+47
-28
lines changed

9 files changed

+47
-28
lines changed

pack/index_decode.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/binary"
66
"fmt"
7+
"hash"
78
"io"
89
)
910

@@ -69,8 +70,8 @@ var (
6970
// parse index entries.
7071
//
7172
// If there was an error parsing, it will be returned immediately.
72-
func DecodeIndex(r io.ReaderAt) (*Index, error) {
73-
version, err := decodeIndexHeader(r)
73+
func DecodeIndex(r io.ReaderAt, hash hash.Hash) (*Index, error) {
74+
version, err := decodeIndexHeader(r, hash)
7475
if err != nil {
7576
return nil, err
7677
}
@@ -89,7 +90,7 @@ func DecodeIndex(r io.ReaderAt) (*Index, error) {
8990
}
9091

9192
// decodeIndexHeader determines which version the index given by "r" is.
92-
func decodeIndexHeader(r io.ReaderAt) (IndexVersion, error) {
93+
func decodeIndexHeader(r io.ReaderAt, hash hash.Hash) (IndexVersion, error) {
9394
hdr := make([]byte, 4)
9495
if _, err := r.ReadAt(hdr, 0); err != nil {
9596
return nil, err
@@ -104,13 +105,13 @@ func decodeIndexHeader(r io.ReaderAt) (IndexVersion, error) {
104105
version := binary.BigEndian.Uint32(vb)
105106
switch version {
106107
case 1:
107-
return new(V1), nil
108+
return &V1{hash: hash}, nil
108109
case 2:
109-
return new(V2), nil
110+
return &V2{hash: hash}, nil
110111
}
111112
return nil, &UnsupportedVersionErr{uint32(version)}
112113
}
113-
return new(V1), nil
114+
return &V1{hash: hash}, nil
114115
}
115116

116117
// decodeIndexFanout decodes the fanout table given by "r" and beginning at the

pack/index_decode_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"encoding/binary"
67
"io"
78
"testing"
@@ -21,7 +22,7 @@ func TestDecodeIndexV2(t *testing.T) {
2122
buf = append(buf, x...)
2223
}
2324

24-
idx, err := DecodeIndex(bytes.NewReader(buf))
25+
idx, err := DecodeIndex(bytes.NewReader(buf), sha1.New())
2526

2627
assert.NoError(t, err)
2728
assert.EqualValues(t, 3, idx.Count())
@@ -33,21 +34,21 @@ func TestDecodeIndexV2InvalidFanout(t *testing.T) {
3334
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
3435
buf = append(buf, make([]byte, indexFanoutWidth-1)...)
3536

36-
idx, err := DecodeIndex(bytes.NewReader(buf))
37+
idx, err := DecodeIndex(bytes.NewReader(buf), sha1.New())
3738

3839
assert.Equal(t, ErrShortFanout, err)
3940
assert.Nil(t, idx)
4041
}
4142

4243
func TestDecodeIndexV1(t *testing.T) {
43-
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth)))
44+
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth)), sha1.New())
4445

4546
assert.NoError(t, err)
4647
assert.EqualValues(t, 0, idx.Count())
4748
}
4849

4950
func TestDecodeIndexV1InvalidFanout(t *testing.T) {
50-
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth-1)))
51+
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth-1)), sha1.New())
5152

5253
assert.Equal(t, ErrShortFanout, err)
5354
assert.Nil(t, idx)
@@ -58,14 +59,14 @@ func TestDecodeIndexUnsupportedVersion(t *testing.T) {
5859
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
5960
buf = append(buf, 0x0, 0x0, 0x0, 0x3)
6061

61-
idx, err := DecodeIndex(bytes.NewReader(buf))
62+
idx, err := DecodeIndex(bytes.NewReader(buf), sha1.New())
6263

6364
assert.EqualError(t, err, "gitobj/pack: unsupported version: 3")
6465
assert.Nil(t, idx)
6566
}
6667

6768
func TestDecodeIndexEmptyContents(t *testing.T) {
68-
idx, err := DecodeIndex(bytes.NewReader(make([]byte, 0)))
69+
idx, err := DecodeIndex(bytes.NewReader(make([]byte, 0)), sha1.New())
6970

7071
assert.Equal(t, io.EOF, err)
7172
assert.Nil(t, idx)

pack/index_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"encoding/binary"
67
"fmt"
78
"testing"
@@ -165,7 +166,7 @@ func init() {
165166
fanout: fanout,
166167
// version is unimportant here, use V2 since it's more common in
167168
// the wild.
168-
version: new(V2),
169+
version: &V2{hash: sha1.New()},
169170

170171
// *bytes.Buffer does not implement io.ReaderAt, but
171172
// *bytes.Reader does.

pack/index_v1_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"encoding/binary"
67
"testing"
78

@@ -37,19 +38,21 @@ var (
3738

3839
V1Index = &Index{
3940
fanout: V1IndexFanout,
40-
version: new(V1),
41+
version: &V1{hash: sha1.New()},
4142
}
4243
)
4344

4445
func TestIndexV1SearchExact(t *testing.T) {
45-
e, err := new(V1).Entry(V1Index, 1)
46+
v := &V1{hash: sha1.New()}
47+
e, err := v.Entry(V1Index, 1)
4648

4749
assert.NoError(t, err)
4850
assert.EqualValues(t, 2, e.PackOffset)
4951
}
5052

5153
func TestIndexVersionWidthV1(t *testing.T) {
52-
assert.EqualValues(t, 0, new(V1).Width())
54+
v := &V1{hash: sha1.New()}
55+
assert.EqualValues(t, 0, v.Width())
5356
}
5457

5558
func init() {

pack/index_v2_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"encoding/binary"
67
"testing"
78

@@ -46,26 +47,29 @@ var (
4647

4748
V2Index = &Index{
4849
fanout: V2IndexFanout,
49-
version: new(V2),
50+
version: &V2{hash: sha1.New()},
5051
}
5152
)
5253

5354
func TestIndexV2EntryExact(t *testing.T) {
54-
e, err := new(V2).Entry(V2Index, 1)
55+
v := &V2{hash: sha1.New()}
56+
e, err := v.Entry(V2Index, 1)
5557

5658
assert.NoError(t, err)
5759
assert.EqualValues(t, 2, e.PackOffset)
5860
}
5961

6062
func TestIndexV2EntryExtendedOffset(t *testing.T) {
61-
e, err := new(V2).Entry(V2Index, 2)
63+
v := &V2{hash: sha1.New()}
64+
e, err := v.Entry(V2Index, 2)
6265

6366
assert.NoError(t, err)
6467
assert.EqualValues(t, 3, e.PackOffset)
6568
}
6669

6770
func TestIndexVersionWidthV2(t *testing.T) {
68-
assert.EqualValues(t, 8, new(V2).Width())
71+
v := &V2{hash: sha1.New()}
72+
assert.EqualValues(t, 8, v.Width())
6973
}
7074

7175
func init() {

pack/packfile_decode.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/binary"
66
"errors"
7+
"hash"
78
"io"
89
)
910

@@ -22,7 +23,7 @@ var (
2223
//
2324
// If the header is malformed, or otherwise cannot be read, an error will be
2425
// returned without a corresponding packfile.
25-
func DecodePackfile(r io.ReaderAt) (*Packfile, error) {
26+
func DecodePackfile(r io.ReaderAt, hash hash.Hash) (*Packfile, error) {
2627
header := make([]byte, 12)
2728
if _, err := r.ReadAt(header[:], 0); err != nil {
2829
return nil, err
@@ -40,5 +41,6 @@ func DecodePackfile(r io.ReaderAt) (*Packfile, error) {
4041
Objects: objects,
4142

4243
r: r,
44+
hash: hash,
4345
}, nil
4446
}

pack/packfile_decode_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package pack
22

33
import (
44
"bytes"
5+
"crypto/sha1"
6+
"crypto/sha256"
57
"testing"
68

79
"github.com/stretchr/testify/assert"
@@ -12,7 +14,7 @@ func TestDecodePackfileDecodesIntegerVersion(t *testing.T) {
1214
'P', 'A', 'C', 'K', // Pack header.
1315
0x0, 0x0, 0x0, 0x2, // Pack version.
1416
0x0, 0x0, 0x0, 0x0, // Number of packed objects.
15-
}))
17+
}), sha1.New())
1618

1719
assert.NoError(t, err)
1820
assert.EqualValues(t, 2, p.Version)
@@ -23,7 +25,7 @@ func TestDecodePackfileDecodesIntegerCount(t *testing.T) {
2325
'P', 'A', 'C', 'K', // Pack header.
2426
0x0, 0x0, 0x0, 0x2, // Pack version.
2527
0x0, 0x0, 0x1, 0x2, // Number of packed objects.
26-
}))
28+
}), sha256.New())
2729

2830
assert.NoError(t, err)
2931
assert.EqualValues(t, 258, p.Objects)
@@ -34,7 +36,7 @@ func TestDecodePackfileReportsBadHeaders(t *testing.T) {
3436
'W', 'R', 'O', 'N', 'G', // Malformed pack header.
3537
0x0, 0x0, 0x0, 0x0, // Pack version.
3638
0x0, 0x0, 0x0, 0x0, // Number of packed objects.
37-
}))
39+
}), sha1.New())
3840

3941
assert.Equal(t, errBadPackHeader, err)
4042
assert.Nil(t, p)

pack/packfile_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"encoding/binary"
67
"encoding/hex"
78
"fmt"
@@ -30,6 +31,7 @@ func TestPackObjectReturnsObjectWithSingleBaseAtLowOffset(t *testing.T) {
3031
// (0001 1000) (msb=0, type=commit, size=14)
3132
0x1e}, compressed...),
3233
),
34+
hash: sha1.New(),
3335
}
3436

3537
o, err := p.Object(DecodeHex(t, "cccccccccccccccccccccccccccccccccccccccc"))
@@ -63,6 +65,7 @@ func TestPackObjectReturnsObjectWithSingleBaseAtHighOffset(t *testing.T) {
6365

6466
compressed...,
6567
)),
68+
hash: sha1.New(),
6669
}
6770

6871
o, err := p.Object(DecodeHex(t, "cccccccccccccccccccccccccccccccccccccccc"))
@@ -108,6 +111,7 @@ func TestPackObjectReturnsObjectWithDeltaBaseOffset(t *testing.T) {
108111
0x6e, // (0110 1010) (msb=0, type=obj_ofs_delta, size=10)
109112
0x12, // (0001 0001) (ofs_delta=-17, len(compressed))
110113
}, delta...)...)),
114+
hash: sha1.New(),
111115
}
112116

113117
o, err := p.Object(DecodeHex(t, "cccccccccccccccccccccccccccccccccccccccc"))
@@ -160,6 +164,7 @@ func TestPackfileObjectReturnsObjectWithDeltaBaseReference(t *testing.T) {
160164
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
161165
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
162166
}, delta...)...)),
167+
hash: sha1.New(),
163168
}
164169

165170
o, err := p.Object(DecodeHex(t, "dddddddddddddddddddddddddddddddddddddddd"))
@@ -261,7 +266,7 @@ func IndexWith(offsets map[string]uint32) *Index {
261266
fanout: fanout,
262267
r: bytes.NewReader(buf),
263268

264-
version: new(V2),
269+
version: &V2{hash: sha1.New()},
265270
}
266271
}
267272

pack/set.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func NewSet(db string, algo hash.Hash) (*Set, error) {
7676
return nil, err
7777
}
7878

79-
pack, err := DecodePackfile(packf)
79+
pack, err := DecodePackfile(packf, algo)
8080
if err != nil {
8181
return nil, err
8282
}
8383

84-
idx, err := DecodeIndex(idxf)
84+
idx, err := DecodeIndex(idxf, algo)
8585
if err != nil {
8686
return nil, err
8787
}
@@ -139,7 +139,7 @@ func NewSetPacks(packs ...*Packfile) *Set {
139139
}
140140

141141
return &Set{
142-
m: m,
142+
m: m,
143143
closeFn: func() error {
144144
for _, pack := range packs {
145145
if err := pack.Close(); err != nil {

0 commit comments

Comments
 (0)