-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGitPackIndex.test.ts
126 lines (106 loc) · 4.97 KB
/
GitPackIndex.test.ts
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
import { GitPackIndex } from 'git-essentials/models/GitPackIndex'
import { GitObject } from 'git-essentials/models/GitObject'
import { shasum } from 'git-essentials/utils/shasum'
import { makeFsFixture, FsFixtureData } from './helpers/makeFsFixture'
import gitPackIndexFsFixtureData from './fixtures/fs/GitPackIndex.json'
describe('GitPackIndex', () => {
it('from .idx', async () => {
// arrange
const { fs, dir } = await makeFsFixture(gitPackIndexFsFixtureData as FsFixtureData)
const idx = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.idx`
))
// act
const p = (await GitPackIndex.fromIdx({ idx }))!
// assert
expect(await shasum(Buffer.from(JSON.stringify(p.hashes)))).toBe('fd2404a29d1e5dc72066541366d5f75bc9d51c9b')
expect(p.packfileSha).toBe('1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888')
// Test a handful of known offsets.
expect(p.offsets.get('0b8faa11b353db846b40eb064dfb299816542a46')).toEqual(40077)
expect(p.offsets.get('637c4e69d85e0dcc18898ec251377453d0891585')).toEqual(39860)
expect(p.offsets.get('98e9fde3ee878fa985a143fc5fe05d4e6d8e637b')).toEqual(39036)
expect(p.offsets.get('43c49edb213748626fc363c890c01a9e55a1b8da')).toEqual(38202)
expect(p.offsets.get('5f1f014326b1d7e8079d00b87fa7a9913bd91324')).toEqual(20855)
})
it('from .pack', async () => {
// arrange
const { fs, dir } = await makeFsFixture(gitPackIndexFsFixtureData as FsFixtureData)
const pack = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.pack`
))
// act
const p = await GitPackIndex.fromPack({ pack })
// assert
expect(await shasum(Buffer.from(JSON.stringify(p.hashes)))).toBe('fd2404a29d1e5dc72066541366d5f75bc9d51c9b')
expect(p.packfileSha).toBe('1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888')
// Test a handful of known offsets.
expect(p.offsets.get('0b8faa11b353db846b40eb064dfb299816542a46')).toEqual(40077)
expect(p.offsets.get('637c4e69d85e0dcc18898ec251377453d0891585')).toEqual(39860)
expect(p.offsets.get('98e9fde3ee878fa985a143fc5fe05d4e6d8e637b')).toEqual(39036)
expect(p.offsets.get('43c49edb213748626fc363c890c01a9e55a1b8da')).toEqual(38202)
expect(p.offsets.get('5f1f014326b1d7e8079d00b87fa7a9913bd91324')).toEqual(20855)
})
it('to .idx file from .pack', async () => {
// arrange
const { fs, dir } = await makeFsFixture(gitPackIndexFsFixtureData as FsFixtureData)
const idx = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.idx`
))
const pack = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.pack`
))
// act
const p = await GitPackIndex.fromPack({ pack })
const idxbuffer = await p.toBuffer()
// assert
expect(idxbuffer.byteLength).toBe(idx.byteLength)
expect(idxbuffer.equals(idx)).toBe(true)
})
it('read undeltified object', async () => {
// arrange
const { fs, dir } = await makeFsFixture(gitPackIndexFsFixtureData as FsFixtureData)
const idx = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.idx`
))
const pack = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.pack`
))
// act
const p = (await GitPackIndex.fromIdx({ idx }))!
await p.load({ pack: Promise.resolve(pack) })
const { type, object } = await p.read({
oid: '637c4e69d85e0dcc18898ec251377453d0891585',
})
// assert
expect(type).toBe('commit')
const oid = await shasum(GitObject.wrap({ type, object }))
expect(oid).toBe('637c4e69d85e0dcc18898ec251377453d0891585')
expect(object.toString('utf8')).toBe(
`tree cbd2a3d7e00a972faaf0ef59d9b421de9f1a7532
parent fbd56b49d400a19ee185ae735417bdb34c084621
parent 0b8faa11b353db846b40eb064dfb299816542a46
author William Hilton <wmhilton@gmail.com> 1508204014 -0400
committer William Hilton <wmhilton@gmail.com> 1508204014 -0400
WIP on master: fbd56b4 Add 'unpkg' key to package.json\n`)
})
it('read deltified object', async () => {
// arrange
const { fs, dir } = await makeFsFixture(gitPackIndexFsFixtureData as FsFixtureData)
const idx = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.idx`
))
const pack = Buffer.from(await fs.readFile(
`${dir}/.git/objects/pack/pack-1a1e70d2f116e8cb0cb42d26019e5c7d0eb01888.pack`
))
// act
const p = (await GitPackIndex.fromIdx({ idx }))!
await p.load({ pack: Promise.resolve(pack) })
const { type, object } = await p.read({
oid: '7fb539a8e8488c3fd2793e7dda8a44693e25cce1', // 9 levels deep of deltification.
})
// assert
expect(type).toBe('blob')
const oid = await shasum(GitObject.wrap({ type, object }))
expect(oid).toBe('7fb539a8e8488c3fd2793e7dda8a44693e25cce1')
})
})