This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommit.js
87 lines (77 loc) · 2.48 KB
/
commit.js
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
'use strict'
const setImmediate = require('async/setImmediate')
const SmartBuffer = require('smart-buffer').SmartBuffer
const gitUtil = require('./util')
exports = module.exports
exports.serialize = (dagNode, callback) => {
let lines = []
lines.push('tree ' + gitUtil.cidToSha(dagNode.tree['/']).toString('hex'))
dagNode.parents.forEach((parent) => {
lines.push('parent ' + gitUtil.cidToSha(parent['/']).toString('hex'))
})
lines.push('author ' + gitUtil.serializePersonLine(dagNode.author))
lines.push('committer ' + gitUtil.serializePersonLine(dagNode.committer))
if (dagNode.encoding) {
lines.push('encoding ' + dagNode.encoding)
}
if (dagNode.signature) {
lines.push('gpgsig -----BEGIN PGP SIGNATURE-----')
lines.push(dagNode.signature.text)
}
lines.push('')
lines.push(dagNode.message)
let data = lines.join('\n')
let outBuf = new SmartBuffer()
outBuf.writeString('commit ')
outBuf.writeString(data.length.toString())
outBuf.writeUInt8(0)
outBuf.writeString(data)
setImmediate(() => callback(null, outBuf.toBuffer()))
}
exports.deserialize = (data, callback) => {
let lines = data.toString().split('\n')
let res = {gitType: 'commit', parents: []}
for (let line = 0; line < lines.length; line++) {
let m = lines[line].match(/^([^ ]+) (.+)$/)
if (!m) {
if (lines[line] !== '') {
setImmediate(() => callback(new Error('Invalid commit line ' + line)))
}
res.message = lines.slice(line + 1).join('\n')
break
}
let key = m[1]
let value = m[2]
switch (key) {
case 'tree':
res.tree = {'/': gitUtil.shaToCid(Buffer.from(value, 'hex'))}
break
case 'committer':
res.committer = gitUtil.parsePersonLine(value)
break
case 'author':
res.author = gitUtil.parsePersonLine(value)
break
case 'parent':
res.parents.push({'/': gitUtil.shaToCid(Buffer.from(value, 'hex'))})
break
case 'gpgsig': {
if (value !== '-----BEGIN PGP SIGNATURE-----') {
setImmediate(() => callback(new Error('Invalid commit line ' + line)))
}
res.signature = {}
let startLine = line
for (; line < lines.length - 1; line++) {
if (lines[line + 1][0] !== ' ') {
res.signature.text = lines.slice(startLine + 1, line + 1).join('\n')
break
}
}
break
}
default:
res[key] = value
}
}
setImmediate(() => callback(null, res))
}