-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.lua
286 lines (228 loc) · 5.35 KB
/
repo.lua
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
consts = require 'consts'
diffsplit = require 'diffsplit'
local repo = {}
-- find repo root with a metadir
function repo:findroot(path)
path = path or "."
local fullpath, err = fs.abs(path)
if err then
return nil, err
end
if fs.exists(fs.join(fullpath, consts.metadir)) then
return fullpath
end
if fullpath == "/" then
return nil
end
return self:findroot(fs.join(fullpath, ".."))
end
-- check if provided path resides in a repo
function repo:isrepo(path)
return self:findroot(path) ~= nil
end
function repo:isvalidrepo(path)
local path, err = self:findroot(path)
if err then
return false, err
end
if not path then
return false, "repo metadir not found"
end
return fs.exists(fs.join(path, consts.metadir)) and
fs.exists(fs.join(path, consts.metadir, consts.snapshot))
end
function repo:isroot(root)
local root, err = fs.abs(root)
if err then
-- todo: swallowed error
return false
end
return root == self.root or root == self.snapshot
end
function repo:relpath(path)
local path, err = fs.abs(path)
if err then
return nil, err
end
local root = self.root
if root == path then
return "."
end
local start, fin = string.find(path, root, 0, true)
if start then
local relpath = string.sub(path, fin + 1)
while string.sub(relpath, 1, 1) == "/" do relpath = string.sub(relpath, 2) end
if #relpath > 0 then
return relpath
end
end
return nil, "not relpath"
end
function repo:create(path)
local path, err = fs.abs(path or ".")
if err then
return nil, err
end
local metadir = fs.join(path, consts.metadir)
-- todo: add error handling for mkdir calls
fs.create_dir(metadir, true)
fs.create_dir(fs.join(metadir, consts.snapshot), true)
end
function repo:readignores()
local ignores = {}
-- todo: swallowed error
local f = io.open(self.ignorefile)
if f ~= nil then
for line in f:lines() do
-- todo: maybe use trim, but that will break expressions with significant spaces
if line and #line > 0 then
table.insert(ignores, line)
end
end
f:close()
end
self.ignores = ignores
end
function repo:init(path)
path, err = self:findroot(path)
if err then
return err
end
local valid, err = self:isvalidrepo(path)
if err then
return err
end
if not valid then
return "not a valid repo"
end
self.root = path
self.metadir = fs.join(self.root, consts.metadir)
self.snapshot = fs.join(self.metadir, consts.snapshot)
self.ignorefile = fs.join(self.root, consts.ignorefile)
return self:readignores()
end
function repo:files(root, relpath)
local path = root
local isroot = relpath == "."
if not isroot then
path = fs.join(root, relpath)
end
if not fs.exists(path) then
return {}
end
local entries = fs.entries(path)
if entries == nil then
return nil, "failed to get directory entries"
end
local files = {}
for filename in entries do
local skip = false
if isroot then
for i, v in ipairs(consts.ignores) do
if string.match(filename, v) then
skip = true
break
end
end
end
if not skip then
for i, v in ipairs(self.ignores) do
if string.match(filename, v) then
skip = true
break
end
end
end
if not skip then
table.insert(files, filename)
end
end
return files
end
function repo:save()
local files, err = repo:files(self.root, ".")
if err then
return err
end
for i, file in ipairs(files) do
local src = fs.join(self.root, file)
local dest = fs.join(self.snapshot, file)
if fs.is_dir(src) then
fs.copy_dir(src, dest)
else
fs.copy_file(src, dest)
end
end
end
function repo:diff(root, oldroot, relpath, diffs)
local diffs = diffs or {}
local a = fs.join(oldroot, relpath)
local b = fs.join(root, relpath)
local aexists = fs.exists(a)
local bexists = fs.exists(b)
local aisdir = aexists and fs.is_dir(a)
local bisdir = bexists and fs.is_dir(b)
local afiles = {}
local bfiles = {}
if aexists and aisdir then
afiles = self:files(oldroot, relpath)
else
afiles = {}
end
if bexists and bisdir then
bfiles = self:files(root, relpath)
else
bfiles = {}
end
-- compare directories
if #afiles > 0 or #bfiles > 0 then
local exists = {}
local files = {}
for i, filename in ipairs(afiles) do
if not exists[filename] then
table.insert(files, filename)
exists[filename] = true
end
end
for i, filename in ipairs(bfiles) do
if not exists[filename] then
table.insert(files, filename)
exists[filename] = true
end
end
table.sort(files)
for i, filename in ipairs(files) do
self:diff(root, oldroot, fs.join(relpath, filename), diffs)
end
end
-- compare files
if aexists and not aisdir or bexists and not bisdir then
local acontent = ""
local bcontent = ""
if aexists and not aisdir then
acontent = io.open(a):read("a")
end
if bexists and not bisdir then
bcontent = io.open(b):read("a")
end
if acontent ~= bcontent then
if #acontent > 0 or #bcontent > 0 then
diffstr = diff.compare_strings(acontent, bcontent)
-- todo: remove following lines later
local aname = '/dev/null'
if #acontent > 0 then
aname = fs.join('a', relpath)
end
local bname = '/dev/null'
if #bcontent > 0 then
bname = fs.join('b', relpath)
end
diffstr = string.gsub(diffstr, "--- a", "--- " .. aname)
diffstr = string.gsub(diffstr, "+++ b", "+++ " .. bname)
table.insert(diffs, diffstr)
end
end
end
return diffs
end
return repo