Compare commits
16
Commits
4d8b615ccb
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd5cec8a4b | ||
|
|
ae5e7ce8f4 | ||
|
|
9286509d31 | ||
|
|
7403d1a1f4 | ||
|
|
3ca3dad456 | ||
|
|
cc48938255 | ||
|
|
ff0fefd0b9 | ||
|
|
ecffbdd55c | ||
|
|
02704ae595 | ||
|
|
43b24446f3 | ||
|
|
cda3c4f433 | ||
|
|
971e9a614c | ||
|
|
029bf80c51 | ||
|
|
c4555da4cf | ||
|
|
035fb45cf6 | ||
|
|
7beb7ef4fe |
@@ -0,0 +1,7 @@
|
|||||||
|
# Usage:
|
||||||
|
|
||||||
|
`git clone [https link from gitea]`
|
||||||
|
|
||||||
|
# Download:
|
||||||
|
|
||||||
|
`wget https://git.ejebring.com/eejebring/CC-tweaked-Gitea-Client/raw/branch/main/git.lua git.lua`
|
||||||
@@ -1,47 +1,93 @@
|
|||||||
local GIT_TOKEN = "" -- Leave empty for public repos, or add "your-token-here"
|
-- Leave empty for public repos, or add "your-token-here"
|
||||||
|
local GIT_TOKEN = ""
|
||||||
|
|
||||||
-- Parse a Gitea URL like: https://gitea.example.com/owner/repo or /owner/repo/branch/filename
|
-- Update config
|
||||||
local function parseGiteaURL(url)
|
local UPDATE_URL = "https://git.ejebring.com/eejebring/CC-tweaked-Gitea-Client/raw/branch/main/git.lua"
|
||||||
local protocol, domain, path = url:match("^(https?://([^/]+))(/[^?]*)")
|
local GIT_FILE = "git.lua"
|
||||||
if not protocol then
|
|
||||||
protocol, domain, path = "https://gitea.local", "gitea.local", url
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Remove trailing slashes and clean path
|
---@param str string
|
||||||
path = path:gsub("/+$", "")
|
---@param comp string
|
||||||
|
function string.beginsWith(str, comp)
|
||||||
-- Extract components from path (format: /owner/repo or /owner/repo/branch)
|
local compLen = string.len(comp)
|
||||||
local parts = {}
|
local begining = string.sub(str, 0, compLen)
|
||||||
for segment in path:gmatch("[^/]+") do
|
return begining == comp
|
||||||
if segment ~= "" then table.insert(parts, segment) end
|
|
||||||
end
|
|
||||||
|
|
||||||
if #parts < 2 then
|
|
||||||
return nil, "Invalid URL format. Expected: https://domain.com/owner/repo"
|
|
||||||
end
|
|
||||||
|
|
||||||
local owner = parts[1]
|
|
||||||
local repo = parts[2]
|
|
||||||
local branch = parts[3] or "master"
|
|
||||||
|
|
||||||
return {
|
|
||||||
base = protocol .. "/" .. domain,
|
|
||||||
owner = owner,
|
|
||||||
repo = repo,
|
|
||||||
branch = branch
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function downloadFile(base, owner, repo, branch, path, target)
|
---@param str string
|
||||||
local url = ("%s/api/v1/repos/%s/%s/raw/%s/%s"):format(base, owner, repo, branch, path)
|
---@param comp string
|
||||||
|
function string.endsWith(str, comp)
|
||||||
|
local reverseStr = string.reverse(str)
|
||||||
|
local reverseComp = string.reverse(comp)
|
||||||
|
return string.beginsWith(reverseStr, reverseComp)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param str string
|
||||||
|
---@param delimiter string
|
||||||
|
function string.split(str, delimiter)
|
||||||
|
local returnTable = {}
|
||||||
|
for k in string.gmatch(str, "([^" .. delimiter .. "]+)")
|
||||||
|
do
|
||||||
|
returnTable[#returnTable + 1] = k
|
||||||
|
end
|
||||||
|
return returnTable
|
||||||
|
end
|
||||||
|
|
||||||
|
local protocols = { "http://", "https://" }
|
||||||
|
local ending = ".git"
|
||||||
|
---@param url string
|
||||||
|
local function parseGiteaURL(url)
|
||||||
|
---@class urlinfo
|
||||||
|
local urlInfo = {
|
||||||
|
protocol = "",
|
||||||
|
origin = "",
|
||||||
|
owner = "",
|
||||||
|
repo = "",
|
||||||
|
branch = "main",
|
||||||
|
treeUrl = "",
|
||||||
|
}
|
||||||
|
local lowerUrl = string.lower(url)
|
||||||
|
if not string.endsWith(lowerUrl, ending) then error("A gitea link needs to end with " .. ending) end
|
||||||
|
for _, proto in pairs(protocols) do
|
||||||
|
if string.beginsWith(lowerUrl, proto) then
|
||||||
|
urlInfo.protocol = proto
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local urlLen = string.len(url)
|
||||||
|
local protoLen = string.len(urlInfo.protocol)
|
||||||
|
local endLen = string.len(ending)
|
||||||
|
local cleaned = string.sub(url, protoLen + 1, urlLen - endLen)
|
||||||
|
|
||||||
|
local urlParts = string.split(cleaned, "/")
|
||||||
|
if not (#urlParts) == 3 then error("Unexpected link length.") end
|
||||||
|
urlInfo.origin, urlInfo.owner, urlInfo.repo = urlParts[1], urlParts[2], urlParts[3]
|
||||||
|
|
||||||
|
urlInfo.treeUrl =
|
||||||
|
("%s%s/api/v1/repos/%s/%s/git/trees/%s?recursive=1")
|
||||||
|
:format(urlInfo.protocol, urlInfo.origin, urlInfo.owner, urlInfo.repo, urlInfo.branch)
|
||||||
|
for k, v in pairs(urlInfo) do
|
||||||
|
if v == "" or v == nil then
|
||||||
|
error("Could not parse " .. k .. " from link.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print("treeUrl: ", urlInfo.treeUrl)
|
||||||
|
return urlInfo
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param urlInfo urlinfo
|
||||||
|
---@param path string
|
||||||
|
---@param target string
|
||||||
|
local function downloadFile(urlInfo, path, target)
|
||||||
|
local url = ("%s%s/api/v1/repos/%s/%s/raw/%s/%s"):format(urlInfo.protocol, urlInfo.origin, urlInfo.owner,
|
||||||
|
urlInfo.repo, urlInfo.branch, path)
|
||||||
|
|
||||||
local req = { url = url }
|
local req = { url = url }
|
||||||
if GIT_TOKEN ~= "" then
|
if GIT_TOKEN ~= "" then
|
||||||
req.headers = { Authorization = "token " .. GIT_TOKEN }
|
req.headers = { Authorization = "token " .. GIT_TOKEN }
|
||||||
end
|
end
|
||||||
|
|
||||||
local response = http.get(req)
|
local response, err = http.get(req)
|
||||||
if not response then error("Failed to download: " .. path) end
|
if not response then error("Failed to download: " .. path .. " due to: " .. err) end
|
||||||
|
|
||||||
local data = response.readAll()
|
local data = response.readAll()
|
||||||
response.close()
|
response.close()
|
||||||
@@ -56,15 +102,10 @@ local function downloadFile(base, owner, repo, branch, path, target)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function cloneRepo(url)
|
local function cloneRepo(url)
|
||||||
local info, err = parseGiteaURL(url)
|
local urlInfo = parseGiteaURL(url)
|
||||||
if not info then error(err) end
|
print("Cloning: " .. urlInfo.owner .. "/" .. urlInfo.repo .. "@" .. urlInfo.branch)
|
||||||
|
|
||||||
print("Cloning: " .. info.owner .. "/" .. info.repo .. "@" .. info.branch)
|
local req = { url = urlInfo.treeUrl }
|
||||||
|
|
||||||
local treeUrl = ("%s/api/v1/repos/%s/%s/git/trees/%s?recursive=1"):format(info.base, info.owner, info.repo,
|
|
||||||
info.branch)
|
|
||||||
|
|
||||||
local req = { url = treeUrl }
|
|
||||||
if GIT_TOKEN ~= "" then
|
if GIT_TOKEN ~= "" then
|
||||||
req.headers = { Authorization = "token " .. GIT_TOKEN }
|
req.headers = { Authorization = "token " .. GIT_TOKEN }
|
||||||
end
|
end
|
||||||
@@ -81,29 +122,38 @@ local function cloneRepo(url)
|
|||||||
local count = 0
|
local count = 0
|
||||||
for _, node in ipairs(tree.tree) do
|
for _, node in ipairs(tree.tree) do
|
||||||
if node.type == "blob" then
|
if node.type == "blob" then
|
||||||
local localPath = ("git/%s/%s/%s"):format(info.owner, info.repo, node.path)
|
local localPath = ("%s/%s"):format(urlInfo.repo, node.path)
|
||||||
local dir = fs.getDir(localPath)
|
local dir = fs.getDir(localPath)
|
||||||
|
|
||||||
if not fs.isDir(dir) then fs.makeDir(dir) end
|
if not fs.isDir(dir) then fs.makeDir(dir) end
|
||||||
|
|
||||||
downloadFile(info.base, info.owner, info.repo, info.branch, node.path, localPath)
|
downloadFile(urlInfo, node.path, localPath)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print(("Cloned %d files to /home/git/%s/%s"):format(count, info.owner, info.repo))
|
print(("Cloned %d files to /home/%s"):format(count, urlInfo.repo))
|
||||||
end
|
end
|
||||||
|
|
||||||
local args = { ... }
|
local args = { ... }
|
||||||
if args[1] == "clone" and args[2] then
|
if args[1] == "clone" and args[2] then
|
||||||
cloneRepo(args[2])
|
cloneRepo(args[2])
|
||||||
|
elseif args[1] == "update" then
|
||||||
|
local resp = http.get(UPDATE_URL)
|
||||||
|
if not resp then error("Failed to download update.") end
|
||||||
|
local code = resp.readAll()
|
||||||
|
if not code then error("Recived empty response from server.") end
|
||||||
|
local file = fs.open(GIT_FILE, "w+")
|
||||||
|
if not file then error("Could not overwite " .. GIT_FILE) end
|
||||||
|
file.write(code)
|
||||||
else
|
else
|
||||||
print("Usage:")
|
print("Usage:")
|
||||||
print(" git pull <full-gitea-url>")
|
print(" git update")
|
||||||
|
print(" git clone <full-gitea-url>")
|
||||||
print("")
|
print("")
|
||||||
print("Examples:")
|
print("Examples:")
|
||||||
print(' git pull "https://gitea.example.com/user/myrepo"')
|
print(' git clone "https://gitea.example.com/user/myrepo"')
|
||||||
print(' git pull "https://gitea.example.com/user/myrepo/tree/main/path/to/file.lua"')
|
--print(' git clone "https://gitea.example.com/user/myrepo/tree/main/path/to/file.lua"')
|
||||||
print("")
|
print("")
|
||||||
print("Set GIT_TOKEN variable above for private repos.")
|
print("Set GIT_TOKEN variable above for private repos.")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user