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
|
#!/usr/bin/lua
local repo, localname = ...
local luxio = require "luxio"
local sio = require "luxio.simple"
local sp = require "luxio.subprocess"
local archive = require "archive"
local function make_temp(syscall, template, tempdir)
template = template or "tmp.XXXXXX"
tempdir = tempdir or os.getenv("TMPDIR") or "/tmp"
local retries = 100
if not template:find('/') then
template = tempdir..'/'..template
end
if template:sub(-6) ~= "XXXXXX" then
error("Template must end in XXXXXX")
end
repeat
local tempname = template:sub(1, -7)..tostring(math.random(999999))
local ret = {syscall(tempname)}
local ok = table.remove(ret, 1)
if ok then
return tempname, unpack(ret)
end
retries = retries - 1
until retries == 0
error("Unable to create temporary")
end
local function mkdtemp(template, tempdir)
return make_temp(function(tempname)
local ret, errmsg, errno = sio.mkdir(tempname, "0700")
if ret == 0 then
return tempname
end
if errno == luxio.EEXIST then
return false
end
error(("Failed to make directory %s: %s"):format(tempname, errmsg))
end, template, tempdir)
end
local CONFIG_FORMAT = [[
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = %s
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "%s"]
remote = origin
merge = refs/heads/%s
]]
local function get_config(fetch_url, HEAD)
return CONFIG_FORMAT:format(fetch_url, HEAD, HEAD)
end
local include_whitelist = {
"./objects",
"./description",
}
local function matches_prefix(path, prefix)
local matched = string.sub(path, 1, string.len(prefix)) == prefix
return matched
end
local function whitelisted(path)
for _, prefix in ipairs(include_whitelist) do
if matches_prefix(path, prefix) then
return true
end
end
return false
end
local function copy_file(from, write_all, buffer_size)
-- Default buffer size is 4M, but can be changed
buffer_size = buffer_size or 4 * 1024 * 1024
local fromfile, emsg = sio.open(from, "r")
if not fromfile then
return false, emsg
end
local write_count
repeat
local ok
local bytes, emsg = fromfile:read(buffer_size)
if not bytes then
fromfile:close()
tofile:close()
return false, emsg
end
ok, write_count, emsg = write_all(bytes)
if not ok then
fromfile:close()
return false, emsg
end
until write_count == 0
return true
end
local function add_dir(writer, basedir, path)
local fspath = basedir..'/'..path
for name, dinfo in sio.opendir(fspath):iterate() do
local fssubpath = fspath..'/'..name
local subpath = path..'/'..name
--io.stderr:write("name:\t", subpath, '\n')
if name ~= '.' and name ~= '..' and whitelisted(subpath) then
dt = dinfo.d_type
dts = (dt == luxio.DT_REG and "Regular")
or (dt == luxio.DT_DIR and "Directory")
or (dt == luxio.DT_LNK and "Symlink")
--io.stderr:write("d_type:\t", dts, '\n')
local entry = archive.entry{
sourcepath = fssubpath,
pathname = localname..'/.git/'..subpath,
ino = 0,
dev = 0,
uid = 0,
uname = 'root',
gid = 0,
gname = 'root',
atime = {683074800, 683074800},
ctime = {683074800, 683074800},
mtime = {683074800, 683074800},
birthtime = {683074800, 683074800},
}
if dt == luxio.DT_REG then
writer:header(entry)
copy_file(fssubpath, function(bytes)
writer:data(bytes)
return 0, #bytes
end)
elseif dt == luxio.DT_DIR then
writer:header(entry)
add_dir(writer, basedir, subpath)
elseif dt == luxio.DT_LNK then
writer:header(entry)
end
end
end
end
local function write_file(writer, path, data)
writer:header(archive.entry{
pathname = path,
mode = tonumber('100664', 8),
ino = 0,
dev = 0,
uid = 0,
uname = 'root',
gid = 0,
gname = 'root',
atime = {683074800, 683074800},
ctime = {683074800, 683074800},
mtime = {683074800, 683074800},
birthtime = {683074800, 683074800},
size = #data
})
writer:data(data)
end
local function run_git(dir, ...)
local proc = sp.spawn_simple{cwd=dir, stdout=sp.PIPE, ...}
local output = proc.stdout:read("*a")
local how, why = proc:wait()
if how == -1 then
error("run_git failed: "..tostring(why))
end
return output
end
local function write_refs(writer, repo, HEAD, localname)
for branch, sha1 in run_git(repo, 'git', 'for-each-ref',
'--format', '%(refname)%00%(objectname)%00',
'refs/heads')
:gmatch"refs/heads/([^%z]+)%z([^%z]+)%z\n" do
write_file(writer, localname..'/.git/refs/remotes/origin/'..branch,
sha1..'\n')
if branch == HEAD then
write_file(writer, localname..'/.git/refs/heads/'..branch,
sha1..'\n')
end
end
end
local function write_workspace(writer, repo, HEAD, localname, bufsize)
bufsize = bufsize or 4 * 1024 * 1024 --4MB
local proc = sp.spawn_simple{"git", "archive", "--format=tar", HEAD,
cwd = repo, stdout = sp.PIPE}
local reader = archive.read{
compression = "none",
format = "tar",
reader = function(archive_read)
local r = proc.stdout:read(bufsize)
if r == -1 then
error("Reading git archive output")
end
if #r == 0 then
return nil
end
return r
end,
}
while true do
local header = reader:next_header()
if header == nil then
break
end
header:pathname(localname..'/'..header:pathname())
writer:header(header)
while true do
local data = reader:data()
if data == nil then
break
end
writer:data(data)
end
end
end
local function list_tree(repo, HEAD)
local proc = sp.spawn_simple{"git", "ls-tree", "-rz", HEAD,
cwd = repo, stdout = sp.PIPE}
local output = proc.stdout:read("*a")
return output:gmatch"([^ ]+) ([^ ]+) ([^ ]+)\t([^%z]+)%z"
end
local function update_index(repo, HEAD, index)
local function exec(args)
args.cwd = repo
args.env = {GIT_INDEX_FILE = index}
local proc = sp.spawn_simple(args)
local how, why = proc:wait()
if how == -1 then
error("git update-index failed: "..tostring(why))
end
end
local cmdline = {"git", "update-index", "--add"}
for mode, kind, sha1, path in list_tree(repo, HEAD) do
table.insert(cmdline, "--cacheinfo")
table.insert(cmdline, mode)
table.insert(cmdline, sha1)
table.insert(cmdline, path)
if #cmdline >= 50 then -- arbitrary limit
exec(cmdline)
cmdline = {"git", "update-index", "--add"}
end
end
if #cmdline > 3 then
exec(cmdline)
end
end
local function write_index(writer, repo, HEAD, localname)
local dir = mkdtemp()
local index = dir..'/index'
update_index(repo, HEAD, index)
local entry = archive.entry{
sourcepath = index,
pathname = localname..'/.git/index',
ino = 0,
dev = 0,
uid = 0,
uname = 'root',
gid = 0,
gname = 'root',
atime = {683074800, 683074800},
ctime = {683074800, 683074800},
mtime = {683074800, 683074800},
birthtime = {683074800, 683074800},
}
writer:header(entry)
copy_file(index, function(data)
writer:data(data)
return 0, #data
end)
end
local writer = archive.write{
format = "pax",
writer = function(archive, string)
if string == nil then
io.stdout:flush()
else
io.stdout:write(string)
return #string
end
end,
}
add_dir(writer, repo, '.')
HEAD = run_git(repo, "git", "rev-parse",
"--abbrev-ref", "HEAD"):sub(1, -2)
write_file(writer, localname..'/.git/HEAD',
'ref: refs/heads/'..HEAD..'\n')
config = get_config('git://git@gitserver/repo', HEAD)
write_file(writer, localname..'/.git/config', config)
write_refs(writer, repo, HEAD, localname)
write_workspace(writer, repo, HEAD, localname)
write_index(writer, repo, HEAD, localname)
writer:close()
|