-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.lua
More file actions
93 lines (84 loc) · 2.03 KB
/
git.lua
File metadata and controls
93 lines (84 loc) · 2.03 KB
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
local cmd, remote, dir = ...
if cmd == "help" then
print("Use: git pull [github-repo] [local-path]")
return
end
if cmd ~= "pull" then
print("Command "..cmd.." unknown")
return
end
local defaultRemote = "riesenacht/cc-codebase"
local defaultDir = "cb"
if remote == nil or remote == "" then
remote = defaultRemote
end
if dir == nil or dir == "" then
dir = defaultDir
end
local function ensureLibDir()
local libDir = "/lib"
if not fs.exists(libDir) then
fs.makeDir(libDir)
end
end
local function ensureJsonLib()
ensureLibDir()
local jsonLib = "/lib/json.lua"
if not fs.exists(jsonLib) then
local request = http.get("https://raw.githubusercontent.com/rxi/json.lua/master/json.lua")
local content = request.readAll()
request.close()
file = fs.open(jsonLib, "w")
file.write(content)
file.close()
end
end
local function includePath(path)
if string.sub(path, 1, 1) == "." then
return false
end
if string.match(path, "/") then
return false
end
return true
end
local function processFiles(repoTree)
local files = {}
for i, file in ipairs(repoTree) do
local path = file.path
if includePath(path) then
table.insert(files, path)
end
end
return files
end
local function getRepoTree()
ensureJsonLib()
local json = require("/lib/json")
local repoUrl = "https://api.github.com/repos/"..remote.."/git/trees/main?recursive=1"
local request = http.get(repoUrl)
local raw = request.readAll()
request.close()
local repo = json.decode(raw)
local tree = repo.tree
return repo.tree
end
local function recreateLocalDir()
if fs.exists(dir) then
fs.delete(dir)
end
fs.makeDir(dir)
end
local repoTree = getRepoTree()
local files = processFiles(repoTree)
recreateLocalDir()
local repoUrl = "https://raw.githubusercontent.com/"..remote.."/main"
for k,v in pairs(files) do
print(k, v)
local request = http.get(repoUrl.."/"..v)
local content = request.readAll()
request.close()
local file = fs.open(dir.."/"..v, "w")
file.write(content)
file.close()
end