forked from git-lfs/gitobj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_db_test.go
517 lines (421 loc) · 12.9 KB
/
object_db_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package gitobj
import (
"bytes"
"compress/zlib"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const roundTripCommitSha string = `561ed224a6bd39232d902ad8023c0ebe44fbf6c5`
const roundTripCommit string = `tree f2ebdf9c967f69d57b370901f9344596ec47e51c
parent fe8fbf7de1cd9f08ae642e502bf5de94e523cc08
author brian m. carlson <[email protected]> 1543506816 +0000
committer brian m. carlson <[email protected]> 1543506816 +0000
gpgsig -----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.2.9 (Darwin)
iQIGBAABCgAwFiEETbktHYzuflTwZxNFLQybwS+Cs6EFAlwAC4cSHGJrMjIwNEBn
aXRodWIuY29tAAoJEC0Mm8EvgrOhiRMN/2rTxkBb5BeQQeq7rPiIW8+29FzuvPeD
/DhxlRKwKut9h4qhtxNQszTezxhP4PLOkuMvUax2pGXCQ8cjkSswagmycev+AB4d
s0loG4SrEwvH8nAdr6qfNx4ZproRJ8QaEJqyN9SqF7PCWrUAoJKehdgA38WtYFws
ON+nIwzDIvgpoNI+DzgWrx16SOTp87xt8RaJOVK9JNZQk8zBh7rR2viS9CWLysmz
wOh3j4XI1TZ5IFJfpCxZzUDFgb6K3wpAX6Vux5F1f3cN5MsJn6WUJCmYCvwofeeZ
6LMqKgry7EA12l7Tv/JtmMeh+rbT5WLdMIsjascUaHRhpJDNqqHCKMEj1zh3QZNY
Hycdcs24JouVAtPwg07f1ncPU3aE624LnNRA9A6Ih6SkkKE4tgMVA5qkObDfwzLE
lWyBj2QKySaIdSlU2EcoH3UK33v/ofrRr3+bUkDgxdqeV/RkBVvfpeMwFVSFWseE
bCcotryLCZF7vBQU+pKC+EaZxQV9L5+McGzcDYxUmqrhwtR+azRBYFOw+lOT4sYD
FxdLFWCtmDhKPX5Ajci2gmyfgCwdIeDhSuOf2iQQGRpE6y7aka4AlaE=
=UyqL
-----END PGP SIGNATURE-----
pack/set: ignore packs without indices
When we look for packs to read, we look for a pack file, and then an
index, and fail if either one is missing. When Git looks for packs to
read, it looks only for indices and then checks if the pack is present.
The Git approach handles the case when there is an extra pack that lacks
an index, while our approach does not. Consequently, we can get various
errors (showing up so far only on Windows) when an index is missing.
If the index file cannot be read for any reason, simply skip the entire
pack altogether and continue on. This leaves us no more or less
functional than Git in terms of discovering objects and makes our error
handling more robust.
`
func TestDecodeObject(t *testing.T) {
testCases := []struct {
options []Option
sha string
}{
{
[]Option{}, "af5626b4a114abcb82d63db7c8082c3c4756e51b",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)}, "7506cbcf4c572be9e06a1fed35ac5b1df8b5a74d26c07f022648e5d95a9f6f2a",
},
}
for _, test := range testCases {
contents := "Hello, world!\n"
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "blob 14\x00%s", contents)
zw.Close()
b, err := NewMemoryBackend(map[string]io.ReadWriter{
test.sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
shaHex, _ := hex.DecodeString(test.sha)
obj, err := odb.Object(shaHex)
blob, ok := obj.(*Blob)
require.NoError(t, err)
require.True(t, ok)
got, err := ioutil.ReadAll(blob.Contents)
assert.Nil(t, err)
assert.Equal(t, contents, string(got))
}
}
func TestDecodeBlob(t *testing.T) {
testCases := []struct {
options []Option
sha string
}{
{
[]Option{}, "af5626b4a114abcb82d63db7c8082c3c4756e51b",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)}, "7506cbcf4c572be9e06a1fed35ac5b1df8b5a74d26c07f022648e5d95a9f6f2a",
},
}
for _, test := range testCases {
contents := "Hello, world!\n"
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "blob 14\x00%s", contents)
zw.Close()
b, err := NewMemoryBackend(map[string]io.ReadWriter{
test.sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
shaHex, _ := hex.DecodeString(test.sha)
blob, err := odb.Blob(shaHex)
assert.Nil(t, err)
assert.EqualValues(t, 14, blob.Size)
got, err := ioutil.ReadAll(blob.Contents)
assert.Nil(t, err)
assert.Equal(t, contents, string(got))
}
}
func TestDecodeTree(t *testing.T) {
testCases := []struct {
options []Option
size int64
treeSha string
blobSha string
}{
{
[]Option{},
37,
"fcb545d5746547a597811b7441ed8eba307be1ff",
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)},
49,
"eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
"473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813",
},
}
for _, test := range testCases {
hexSha, err := hex.DecodeString(test.treeSha)
require.Nil(t, err)
hexBlobSha, err := hex.DecodeString(test.blobSha)
require.Nil(t, err)
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "tree %d\x00", test.size)
fmt.Fprintf(zw, "100644 hello.txt\x00")
zw.Write(hexBlobSha)
zw.Close()
b, err := NewMemoryBackend(map[string]io.ReadWriter{
test.treeSha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
tree, err := odb.Tree(hexSha)
assert.Nil(t, err)
require.Equal(t, 1, len(tree.Entries))
assert.Equal(t, &TreeEntry{
Name: "hello.txt",
Oid: hexBlobSha,
Filemode: 0100644,
}, tree.Entries[0])
}
}
func TestDecodeCommit(t *testing.T) {
testCases := []struct {
options []Option
size int64
treeSha string
commitSha string
}{
{
[]Option{},
173,
"fcb545d5746547a597811b7441ed8eba307be1ff",
"d7283480bb6dc90be621252e1001a93871dcf511",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)},
197,
"eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
"9b03a791a98a2c35621ea6870061fb17299b22e2bb5e9f6a7d5afd7dc0c23915",
},
}
for _, test := range testCases {
commitShaHex, err := hex.DecodeString(test.commitSha)
assert.Nil(t, err)
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "commit %d\x00", test.size)
fmt.Fprintf(zw, "tree %s\n", test.treeSha)
fmt.Fprintf(zw, "author Taylor Blau <[email protected]> 1494620424 -0600\n")
fmt.Fprintf(zw, "committer Taylor Blau <[email protected]> 1494620424 -0600\n")
fmt.Fprintf(zw, "\ninitial commit\n")
zw.Close()
b, err := NewMemoryBackend(map[string]io.ReadWriter{
test.commitSha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
commit, err := odb.Commit(commitShaHex)
assert.Nil(t, err)
assert.Equal(t, "Taylor Blau <[email protected]> 1494620424 -0600", commit.Author)
assert.Equal(t, "Taylor Blau <[email protected]> 1494620424 -0600", commit.Committer)
assert.Equal(t, "initial commit", commit.Message)
assert.Equal(t, 0, len(commit.ParentIDs))
assert.Equal(t, test.treeSha, hex.EncodeToString(commit.TreeID))
}
}
func TestWriteBlob(t *testing.T) {
testCases := []struct {
options []Option
sha string
}{
{
[]Option{}, "af5626b4a114abcb82d63db7c8082c3c4756e51b",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)}, "7506cbcf4c572be9e06a1fed35ac5b1df8b5a74d26c07f022648e5d95a9f6f2a",
},
}
for _, test := range testCases {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
sha, err := odb.WriteBlob(&Blob{
Size: 14,
Contents: strings.NewReader("Hello, world!\n"),
})
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, test.sha, hex.EncodeToString(sha))
assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
}
}
func TestWriteTree(t *testing.T) {
testCases := []struct {
options []Option
treeSha string
blobSha string
}{
{
[]Option{},
"fcb545d5746547a597811b7441ed8eba307be1ff",
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)},
"eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
"473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813",
},
}
for _, test := range testCases {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
hexBlobSha, err := hex.DecodeString(test.blobSha)
require.Nil(t, err)
sha, err := odb.WriteTree(&Tree{Entries: []*TreeEntry{
{
Name: "hello.txt",
Oid: hexBlobSha,
Filemode: 0100644,
},
}})
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, test.treeSha, hex.EncodeToString(sha))
assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
}
}
func TestWriteCommit(t *testing.T) {
testCases := []struct {
options []Option
treeSha string
commitSha string
}{
{
[]Option{},
"fcb545d5746547a597811b7441ed8eba307be1ff",
"fee8a35c2890cd6e0e28d24cc457fcecbd460962",
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)},
"eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
"fcafabe9e00f4e1375b2ba688edf30b96afe7a20c6176fefbc3f371b298f69d6",
},
}
for _, test := range testCases {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
when := time.Unix(1257894000, 0).UTC()
author := &Signature{Name: "John Doe", Email: "[email protected]", When: when}
committer := &Signature{Name: "Jane Doe", Email: "[email protected]", When: when}
treeHex, err := hex.DecodeString(test.treeSha)
assert.Nil(t, err)
sha, err := odb.WriteCommit(&Commit{
Author: author.String(),
Committer: committer.String(),
TreeID: treeHex,
Message: "initial commit",
})
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, test.commitSha, hex.EncodeToString(sha))
assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
}
}
func TestWriteCommitWithGPGSignature(t *testing.T) {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
commit := new(Commit)
_, err = commit.Decode(
sha1.New(),
strings.NewReader(roundTripCommit), int64(len(roundTripCommit)))
require.NoError(t, err)
buf := new(bytes.Buffer)
commit.Encode(buf)
assert.Equal(t, roundTripCommit, buf.String())
sha, err := odb.WriteCommit(commit)
assert.Nil(t, err)
assert.Equal(t, roundTripCommitSha, hex.EncodeToString(sha))
}
func TestDecodeTag(t *testing.T) {
const sha = "7639ba293cd2c457070e8446ecdea56682af0f48"
tagShaHex, err := hex.DecodeString(sha)
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "tag 165\x00")
fmt.Fprintf(zw, "object 6161616161616161616161616161616161616161\n")
fmt.Fprintf(zw, "type commit\n")
fmt.Fprintf(zw, "tag v2.4.0\n")
fmt.Fprintf(zw, "tagger A U Thor <[email protected]>\n")
fmt.Fprintf(zw, "\n")
fmt.Fprintf(zw, "The quick brown fox jumps over the lazy dog.\n")
zw.Close()
b, err := NewMemoryBackend(map[string]io.ReadWriter{
sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
tag, err := odb.Tag(tagShaHex)
assert.Nil(t, err)
assert.Equal(t, []byte("aaaaaaaaaaaaaaaaaaaa"), tag.Object)
assert.Equal(t, CommitObjectType, tag.ObjectType)
assert.Equal(t, "v2.4.0", tag.Name)
assert.Equal(t, "A U Thor <[email protected]>", tag.Tagger)
assert.Equal(t, "The quick brown fox jumps over the lazy dog.", tag.Message)
}
func TestWriteTag(t *testing.T) {
testCases := []struct {
options []Option
tagSha string
commitSha []byte
}{
{
[]Option{},
"e614dda21829f4176d3db27fe62fb4aee2e2475d",
[]byte("aaaaaaaaaaaaaaaaaaaa"),
},
{
[]Option{ObjectFormat(ObjectFormatSHA256)},
"a297d8b92e8be21fbe1c96a64acc596f26c8b204eb291c71e371c832d3584651",
[]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
},
}
for _, test := range testCases {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b, test.options...)
require.NoError(t, err)
sha, err := odb.WriteTag(&Tag{
Object: test.commitSha,
ObjectType: CommitObjectType,
Name: "v2.4.0",
Tagger: "A U Thor <[email protected]>",
Message: "The quick brown fox jumps over the lazy dog.",
})
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, test.tagSha, hex.EncodeToString(sha))
assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
}
}
func TestReadingAMissingObjectAfterClose(t *testing.T) {
sha, _ := hex.DecodeString("af5626b4a114abcb82d63db7c8082c3c4756e51b")
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
ro, rw := b.Storage()
db := &ObjectDatabase{
ro: ro,
rw: rw,
closed: 1,
}
blob, err := db.Blob(sha)
assert.EqualError(t, err, "gitobj: cannot use closed *pack.Set")
assert.Nil(t, blob)
}
func TestClosingAnObjectDatabaseMoreThanOnce(t *testing.T) {
db, err := FromFilesystem("/tmp", "")
assert.Nil(t, err)
assert.Nil(t, db.Close())
assert.EqualError(t, db.Close(), "gitobj: *ObjectDatabase already closed")
}
func TestObjectDatabaseRootWithRoot(t *testing.T) {
db, err := FromFilesystem("/foo/bar/baz", "")
assert.Nil(t, err)
root, ok := db.Root()
assert.Equal(t, "/foo/bar/baz", root)
assert.True(t, ok)
}
func TestObjectDatabaseRootWithoutRoot(t *testing.T) {
root, ok := new(ObjectDatabase).Root()
assert.Equal(t, "", root)
assert.False(t, ok)
}