-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitwit.pluto
407 lines (380 loc) · 14.1 KB
/
gitwit.pluto
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
local crypto = require "pluto:crypto"
local http = require "pluto:http"
local socket = require "pluto:socket"
local url = require "pluto:url"
local gitwit = {}
-- returns a table like: { ["HEAD"] = "<sha1 hash>", ["refs/heads/senpai"] = "<sha1 hash>", ["refs/tags/0.1.0"] = "<sha1 hash>" }
function gitwit.fetchrefs(url_base)
return gitwit.parserefs(gitwit.downloadrefs(url_base))
end
function gitwit.downloadrefs(url_base)
return http.request(url_base.."/info/refs?service=git-upload-pack")
end
function gitwit.parserefs(data)
local i = 1
local unpacked = ""
while i < #data do
local chunk_size = tonumber(data:sub(i, i + 3), 16)
if chunk_size < 4 then
chunk_size = 4
end
unpacked ..= data:sub(i + 4, i + chunk_size - 1)
i += chunk_size
end
local refs = {}
for unpacked:split("\n") as line do
if #line ~= 0 and line[1] ~= "#" then
local [hash, ref] = line:split("\0")[1]:split(" ")
refs[ref] = hash
end
end
return refs
end
function gitwit.fetchpackfile(url_base, commit_hash, base_objects: ?table)
return gitwit.parsepackfile(gitwit.downloadpackfile(url_base, commit_hash, base_objects), base_objects)
end
local function downloadpackfileaux(url_base, body)
local { host, path } = url.parse(url_base.."/git-upload-pack")
local s = socket.connect(host, 443)
assert(s, "failed to connect to "..host)
assert(s:starttls(host), "failed to establish a secure tunnel to "..host)
s:send("POST "..path.." HTTP/1.0\r\n"
.. "Host: "..host.."\r\n"
.. "User-Agent: PlutoLang/gitwit\r\n"
.. "Git-Protocol: version=2\r\n"
.. "Content-Type: application/x-git-upload-pack-request\r\n"
.. "Content-Length: "..#body.."\r\n"
.. "\r\n"
.. body
)
-- Now we gotta parse this shit: https://git-scm.com/docs/gitprotocol-v2
-- I don't wanna handle a pkt-line possibly being sent over multiple TLS records, so we're buffering it first.
local data = ""
while chunk := s:recv() do
data ..= chunk
end
data = data:sub(data:find("\r\n\r\n") + 4)
local i = 1
local packfile = ""
while true do
local chunk_size = tonumber(data:sub(i, i + 3), 16)
--print(chunk_size)
--if data:sub(i + 4, i + 4) == "\2" then
--print(data:sub(i + 5, i + chunk_size - 1))
--end
if data:sub(i + 4, i + 4) == "\1" then
packfile ..= data:sub(i + 5, i + chunk_size - 1)
end
if chunk_size == 0 then
break
end
i += chunk_size
end
return packfile
end
function gitwit.downloadpackfile(url_base, commit_hash, base_objects: ?table)
if commit_hash == nil then
commit_hash = gitwit.fetchrefs(url_base).HEAD
end
assert(#commit_hash == 40)
-- This is based on what my client sent with 'git clone --no-checkout --depth=1'. I think the duplicated request is a quirk of protocol version 2.
local request = "0032want "..commit_hash.."\n"
if base_objects then
local have = {}
for base_objects as object do
if object.type == "commit" then
have:insert(object.hash)
end
end
for have as hash do
request ..= "0032have "..hash.."\n"
end
for have as hash do
request ..= "0035shallow "..hash.."\n"
end
end
local body = "0011command=fetch001eagent=git/2.40.0.windows.10016object-format=sha10001000dthin-pack000finclude-tag000dofs-delta000cdeepen 1"..request..request
.. "0009done\n"
.. "0000"
return downloadpackfileaux(url_base, body)
end
function gitwit.downloadfullpackfile(url_base, commit_hash)
if commit_hash == nil then
commit_hash = gitwit.fetchrefs(url_base).HEAD
end
assert(#commit_hash == 40)
local body = "0011command=fetch001eagent=git/2.40.0.windows.10016object-format=sha10001000dthin-pack000finclude-tag000dofs-delta0032want "..commit_hash.."\n"
.. "0032want "..commit_hash.."\n"
.. "0009done\n"
.. "0000"
return downloadpackfileaux(url_base, body)
end
-- https://git-scm.com/docs/gitformat-pack
-- https://github.com/robisonsantos/packfile_reader
local typenames <const> = { "commit", "tree", "blob", "tag" }
function gitwit.parsepackfile(packfile, base_objects)
local objects = {}
assert(packfile:sub(1, 4) == "PACK")
assert(string.unpack(">I4", packfile, 5) == 2)
local num_objects = string.unpack(">I4", packfile, 9)
local i = 13
for obj = 1, num_objects do
local obj_offset = i
local byte = packfile:byte(i)
i += 1
local type = (byte >> 4) & 0b111
assert(type ~= 0)
local length = byte & 0b1111
local has_more = (byte >> 7) ~= 0
local shift = 4
while has_more do
byte = packfile:byte(i)
i += 1
length |= (byte & 0b1111111) << shift
shift += 7
has_more = (byte >> 7) ~= 0
end
local base
if type == 6 then
-- OBJ_OFS_DELTA, has an offset before the compressed data
-- https://github.com/git/git/blob/26e47e261e969491ad4e3b6c298450c061749c9e/builtin/pack-objects.c#L1443-L1473
byte = packfile:byte(i)
i += 1
local offset = (byte & 0b1111111)
has_more = (byte >> 7) ~= 0
while has_more do
byte = packfile:byte(i)
i += 1
offset += 1
offset = (offset << 7) + (byte & 0b1111111)
has_more = (byte >> 7) ~= 0
end
offset = (obj_offset - offset)
base = objects:find(|x| -> x.offset == offset)
elseif type == 7 then
-- OBJ_REF_DELTA, has an object hash before the compressed data
if base_objects then
local base_hash = packfile:sub(i, i + 19):split(""):map(|x| -> string.format("%02x", x:byte())):concat("")
base = base_objects:find(|x| -> x.hash == base_hash)
end
i += 20
end
local decompressed, info = crypto.decompress(packfile:sub(i), length)
if type == 6 or type == 7 then
type = base.typeid
local data = {}
local j = 1
-- skip size of base object
while (decompressed:byte(j) >> 7) ~= 0 do
j += 1
end
j += 1
-- skip size of undeltified object
while (decompressed:byte(j) >> 7) ~= 0 do
j += 1
end
j += 1
while j < #decompressed do
local insn = decompressed:byte(j)
j += 1
if (insn >> 7) == 0 then
-- Add new data
data:insert(decompressed:sub(j, j + insn - 1))
j += insn
else
-- Copy from base object
local cp_offset = 0
local cp_size = 0
if (insn & 1) ~= 0 then
cp_offset |= decompressed:byte(j)
j += 1
end
if ((insn >> 1) & 1) ~= 0 then
cp_offset |= decompressed:byte(j) << 8
j += 1
end
if ((insn >> 2) & 1) ~= 0 then
cp_offset |= decompressed:byte(j) << 16
j += 1
end
if ((insn >> 3) & 1) ~= 0 then
cp_offset |= decompressed:byte(j) << 24
j += 1
end
if ((insn >> 4) & 1) ~= 0 then
cp_size |= decompressed:byte(j)
j += 1
end
if ((insn >> 5) & 1) ~= 0 then
cp_size |= decompressed:byte(j) << 8
j += 1
end
if ((insn >> 6) & 1) ~= 0 then
cp_size |= decompressed:byte(j) << 16
j += 1
end
if cp_size == 0 then
cp_size = 0x10000
end
cp_offset += 1
data:insert(base.raw_data:sub(cp_offset, cp_offset + cp_size - 1))
end
end
decompressed = data:concat("")
end
if type <= #typenames then
local data = decompressed
if type == 1 then
data = gitwit.parsecommit(data)
elseif type == 2 then
data = gitwit.parsetree(data)
end
objects:insert({
typeid = type,
type = typenames[type],
data = data,
raw_data = decompressed,
hash = crypto.sha1(typenames[type].." "..#decompressed.."\0"..decompressed),
compressed_data = packfile:sub(i, i + info.compressed_size - 1),
compressed_base = base?.hash,
compressed_length = length,
offset = obj_offset,
})
end
i += info.compressed_size
end
--print(crypto.sha1(packfile:sub(1, i - 1)))
--print(packfile:sub(i):split(""):map(|x| -> string.format("%02x", x:byte())):concat(""))
return objects
end
function gitwit.blobhash(data)
return crypto.sha1("blob "..#data.."\0"..data)
end
function gitwit.parsecommit(data)
local sep = data:find("\n\n")
local commit = {
message = data:sub(sep + 2):rstrip("\n")
}
for data:sub(1, sep - 1):split("\n") as line do
sep = line:find(" ")
commit[line:sub(1, sep - 1)] = line:sub(sep + 1)
end
return commit
end
function gitwit.parsetree(data)
local files = {}
local i = 1
while i < #data do
local mode = {}
local name = {}
while i < #data and data[i] ~= " " do
mode:insert(data[i])
i += 1
end
i += 1
while i < #data and data[i] ~= "\0" do
name:insert(data[i])
i += 1
end
i += 1
local hash = data:sub(i, i + 19):split(""):map(|x| -> string.format("%02x", x:byte())):concat("")
i += 20
files:insert({
mode = mode:concat(""),
name = name:concat(""),
hash = hash,
})
end
return files
end
function gitwit.listallfiles(objects, root_tree)
local files = {}
gitwit.listallfilesaux(objects, files, "", root_tree)
return files
end
function gitwit.listallfilesaux(objects, files, prefix, tree)
for tree.data as file do
if file.mode == "40000" then
local data = objects:find(|x| -> x.hash == file.hash)
assert(data)
gitwit.listallfilesaux(objects, files, prefix .. file.name .. "/", data)
else
files:insert({
mode = file.mode,
name = file.name,
path = prefix .. file.name,
hash = file.hash,
})
end
end
end
function gitwit.createpackfile(objects)
local data = { "PACK", string.pack(">I4I4", 2, #objects) }
local offset = 12
local offsets = {}
for objects as object do
local typeid = object.typeid
local base_in_packfile = false
if object.compressed_base then
base_in_packfile = offsets[object.compressed_base]
if base_in_packfile then
typeid = 6
else
typeid = 7
end
end
offsets[object.hash] = offset
local length = object.compressed_length or #object.raw_data
local byte = length & 0b1111
length >>= 4
byte |= (typeid << 4)
if length ~= 0 then
byte |= 0x80
end
data:insert(string.char(byte)) offset += 1
while length ~= 0 do
byte = length & 0x7f
length >>= 7
if length ~= 0 then
byte |= 0x80
end
data:insert(string.char(byte)) offset += 1
end
if object.compressed_data then
if object.compressed_base then
if base_in_packfile then
-- https://github.com/git/git/blob/26e47e261e969491ad4e3b6c298450c061749c9e/builtin/pack-objects.c#L409-L419
local delta = offsets[object.hash] - offsets[object.compressed_base]
local bytes = { delta & 0x7f }
delta >>= 7
while delta ~= 0 do
delta -= 1
bytes:insert(0x80 | (delta & 0x7f))
delta >>= 7
end
for i = #bytes, 1, -1 do
data:insert(string.char(bytes[i])) offset += 1
end
else
data:insert((object.compressed_base:gsub("..", |x| -> string.char(tonumber(x, 16))))) offset += 20
end
end
data:insert(object.compressed_data) offset += #object.compressed_data
else
length = #object.raw_data
local i = 1
while length >= 0xffff do
data:insert(string.pack("<I1I2I2", 0, 0xffff, 0)) offset += 5
data:insert(object.raw_data:sub(i, i + 0xfffe)) offset += 0xffff
i += 0xffff
length -= 0xffff
end
data:insert(string.pack("<I1I2I2", 1, length, length ~ 0xffff)) offset += 5
data:insert(object.raw_data:sub(i)) offset += length
end
end
data = data:concat("")
data ..= crypto.sha1(data, true)
return data
end
return gitwit