-
Notifications
You must be signed in to change notification settings - Fork 13
/
encoders.js
271 lines (246 loc) · 7.26 KB
/
encoders.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
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
"use strict";
var sha1 = require('git-sha1');
var binary = require('bodec');
var modes = require('./modes.js');
// Run sanity tests at startup.
test();
module.exports = {
frame: frame,
normalizeAs: normalizeAs,
normalizeBlob: normalizeBlob,
normalizeTree: normalizeTree,
normalizeCommit: normalizeCommit,
normalizeTag: normalizeTag,
encodeAs: encodeAs,
encodeBlob: encodeBlob,
encodeTree: encodeTree,
encodeCommit: encodeCommit,
encodeTag: encodeTag,
hashAs: hashAs,
pathCmp: pathCmp
};
function test() {
// Test blob encoding
var normalized = normalizeBlob("Hello World\n");
var expected = "557db03de997c86a4a028e1ebd3a1ceb225be238";
var hash = hashAs("blob", normalized);
if (hash !== expected) {
console.log({expected: expected, actual: hash, normalized: normalized});
throw new Error("Invalid body hash");
}
// Test tree encoding
hash = hashAs("tree", normalizeTree({ "greeting.txt": { mode: modes.file, hash: hash } }));
if (hash !== "648fc86e8557bdabbc2c828a19535f833727fa62") {
throw new Error("Invalid tree hash");
}
var date = new Date(1391790884000);
date.timezoneOffset = 7 * 60;
// Test commit encoding
hash = hashAs("commit", normalizeCommit({
tree: hash,
author: {
name: "Tim Caswell",
email: "tim@creationix.com",
date: date
},
message: "Test Commit\n"
}));
if (hash !== "500c37fc17988b90c82d812a2d6fc25b15354bf2") {
throw new Error("Invalid commit hash");
}
// Test annotated tag encoding
date = new Date(1391790910000);
date.timezoneOffset = 7 * 60;
hash = hashAs("tag", normalizeTag({
object: hash,
type: "commit",
tag: "mytag",
tagger: {
name: "Tim Caswell",
email: "tim@creationix.com",
date: date,
},
message: "Tag it!\n"
}));
if (hash !== "49522787662a0183652dc9cafa5c008b5a0e0c2a") {
throw new Error("Invalid annotated tag hash");
}
}
function encodeAs(type, body) {
if (type === "blob") return encodeBlob(body);
if (type === "tree") return encodeTree(body);
if (type === "commit") return encodeCommit(body);
if (type === "tag") return encodeTag(body);
}
function normalizeAs(type, body) {
if (type === "blob") return normalizeBlob(body);
if (type === "tree") return normalizeTree(body);
if (type === "commit") return normalizeCommit(body);
if (type === "tag") return normalizeTag(body);
}
// Calculate a git compatable hash by git encoding the body and prepending a
// git style frame header and calculating the sha1 sum of that.
function hashAs(type, body) {
var encoded = encodeAs(type, body);
var sum = sha1();
sum.update(frame(type, encoded.length));
sum.update(encoded);
return sum.digest();
}
function frame(type, length) {
return type + " " + length + "\0";
}
function normalizeBlob(body) {
var type = typeof body;
if (type === "string") {
return binary.fromRaw(body);
}
if (body && type === "object") {
if (body.constructor.name === "ArrayBuffer") body = new Uint8Array(body);
if (typeof body.length === "number") {
return body;//binary.toRaw(body);
}
}
throw new TypeError("Blob body must be raw string, ArrayBuffer or byte array");
}
function encodeBlob(body) {
return body;
}
function normalizeTree(body) {
var type = body && typeof body;
if (type !== "object") {
throw new TypeError("Tree body must be array or object");
}
var tree = {}, i, l, entry;
// If array form is passed in, convert to object form.
if (Array.isArray(body)) {
for (i = 0, l = body.length; i < l; i++) {
entry = body[i];
tree[entry.name] = {
mode: entry.mode,
hash: entry.hash
};
}
}
else {
var names = Object.keys(body);
for (i = 0, l = names.length; i < l; i++) {
var name = names[i];
entry = body[name];
tree[name] = {
mode: entry.mode,
hash: entry.hash
};
}
}
return tree;
}
function encodeTree(body) {
var tree = "";
var names = Object.keys(body).sort(pathCmp);
for (var i = 0, l = names.length; i < l; i++) {
var name = names[i];
var entry = body[name];
tree += entry.mode.toString(8) + " " + name +
"\0" + binary.decodeHex(entry.hash);
}
return tree;
}
function normalizeCommit(body) {
if (!body || typeof body !== "object") {
throw new TypeError("Commit body must be an object");
}
if (!(body.tree && body.author && body.message)) {
throw new TypeError("Tree, author, and message are required for commits");
}
var parents = body.parents || (body.parent ? [ body.parent ] : []);
if (!Array.isArray(parents)) {
throw new TypeError("Parents must be an array");
}
var author = normalizePerson(body.author);
var committer = body.committer ? normalizePerson(body.committer) : author;
return {
tree: body.tree,
parents: parents,
author: author,
committer: committer,
message: body.message
};
}
function encodeCommit(body) {
var str = "tree " + body.tree;
for (var i = 0, l = body.parents.length; i < l; ++i) {
str += "\nparent " + body.parents[i];
}
str += "\nauthor " + formatPerson(body.author) +
"\ncommitter " + formatPerson(body.committer) +
"\n\n" + body.message;
return binary.encodeUtf8(str);
}
function normalizeTag(body) {
if (!body || typeof body !== "object") {
throw new TypeError("Tag body must be an object");
}
if (!(body.object && body.type && body.tag && body.tagger && body.message)) {
throw new TypeError("Object, type, tag, tagger, and message required");
}
return {
object: body.object,
type: body.type,
tag: body.tag,
tagger: normalizePerson(body.tagger),
message: body.message
};
}
function encodeTag(body) {
var str = "object " + body.object +
"\ntype " + body.type +
"\ntag " + body.tag +
"\ntagger " + formatPerson(body.tagger) +
"\n\n" + body.message;
return binary.encodeUtf8(str);
}
// Tree entries are sorted by the byte sequence that comprises
// the entry name. However, for the purposes of the sort
// comparison, entries for tree objects are compared as if the
// entry name byte sequence has a trailing ASCII '/' (0x2f).
function pathCmp(a, b) {
// TODO: this spec seems to be wrong. It doesn't match the sort order used
// by real git.
// a = binary.encodeUtf8(a) + "/";
// b = binary.encodeUtf8(b) + "/";
return a < b ? -1 : a > b ? 1 : 0;
}
function normalizePerson(person) {
if (!person || typeof person !== "object") {
throw new TypeError("Person must be an object");
}
if (!person.name || !person.email) {
throw new TypeError("Name and email are required for person fields");
}
return {
name: person.name,
email: person.email,
date: person.date || new Date()
};
}
function formatPerson(person) {
return safe(person.name) +
" <" + safe(person.email) + "> " +
formatDate(person.date);
}
function safe(string) {
return string.replace(/(?:^[\.,:;<>"']+|[\0\n<>]+|[\.,:;<>"']+$)/gm, "");
}
function two(num) {
return (num < 10 ? "0" : "") + num;
}
function formatDate(date) {
var offset = date.timezoneOffset || date.getTimezoneOffset();
var neg = "+";
if (offset <= 0) offset = -offset;
else neg = "-";
offset = neg + two(Math.floor(offset / 60)) + two(offset % 60);
var seconds = Math.floor(date.getTime() / 1000);
return seconds + " " + offset;
}