forked from git-lfs/gitobj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_db_test.go
387 lines (295 loc) · 10 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
package gitobj
import (
"bytes"
"compress/zlib"
"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) {
sha := "af5626b4a114abcb82d63db7c8082c3c4756e51b"
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{
sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
shaHex, _ := hex.DecodeString(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) {
sha := "af5626b4a114abcb82d63db7c8082c3c4756e51b"
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{
sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
shaHex, _ := hex.DecodeString(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) {
sha := "fcb545d5746547a597811b7441ed8eba307be1ff"
hexSha, err := hex.DecodeString(sha)
require.Nil(t, err)
blobSha := "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
hexBlobSha, err := hex.DecodeString(blobSha)
require.Nil(t, err)
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "tree 37\x00")
fmt.Fprintf(zw, "100644 hello.txt\x00")
zw.Write(hexBlobSha)
zw.Close()
b, err := NewMemoryBackend(map[string]io.ReadWriter{
sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b)
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) {
sha := "d7283480bb6dc90be621252e1001a93871dcf511"
commitShaHex, err := hex.DecodeString(sha)
assert.Nil(t, err)
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
fmt.Fprintf(zw, "commit 173\x00")
fmt.Fprintf(zw, "tree fcb545d5746547a597811b7441ed8eba307be1ff\n")
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{
sha: &buf,
})
require.NoError(t, err)
odb, err := FromBackend(b)
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, "fcb545d5746547a597811b7441ed8eba307be1ff", hex.EncodeToString(commit.TreeID))
}
func TestWriteBlob(t *testing.T) {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
sha, err := odb.WriteBlob(&Blob{
Size: 14,
Contents: strings.NewReader("Hello, world!\n"),
})
expected := "af5626b4a114abcb82d63db7c8082c3c4756e51b"
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, expected, hex.EncodeToString(sha))
assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
}
func TestWriteTree(t *testing.T) {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
blobSha := "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
hexBlobSha, err := hex.DecodeString(blobSha)
require.Nil(t, err)
sha, err := odb.WriteTree(&Tree{Entries: []*TreeEntry{
{
Name: "hello.txt",
Oid: hexBlobSha,
Filemode: 0100644,
},
}})
expected := "fcb545d5746547a597811b7441ed8eba307be1ff"
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, expected, hex.EncodeToString(sha))
assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
}
func TestWriteCommit(t *testing.T) {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b)
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}
tree := "fcb545d5746547a597811b7441ed8eba307be1ff"
treeHex, err := hex.DecodeString(tree)
assert.Nil(t, err)
sha, err := odb.WriteCommit(&Commit{
Author: author.String(),
Committer: committer.String(),
TreeID: treeHex,
Message: "initial commit",
})
expected := "fee8a35c2890cd6e0e28d24cc457fcecbd460962"
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, expected, 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(
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) {
b, err := NewMemoryBackend(nil)
require.NoError(t, err)
odb, err := FromBackend(b)
require.NoError(t, err)
sha, err := odb.WriteTag(&Tag{
Object: []byte("aaaaaaaaaaaaaaaaaaaa"),
ObjectType: CommitObjectType,
Name: "v2.4.0",
Tagger: "A U Thor <[email protected]>",
Message: "The quick brown fox jumps over the lazy dog.",
})
expected := "e614dda21829f4176d3db27fe62fb4aee2e2475d"
_, s := b.Storage()
assert.Nil(t, err)
assert.Equal(t, expected, 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)
}