diff --git a/git.lua b/git.lua index d1ecd33..412436c 100644 --- a/git.lua +++ b/git.lua @@ -5,35 +5,106 @@ local GIT_URL = "https://git.ejebring.com/eejebring/CC-tweaked-Gitea-Client/raw/ local GIT_FILE = "git.lua" -- Parse a Gitea URL like: https://gitea.example.com/owner/repo or /owner/repo/branch/filename +-- local function parseGiteaURL(url) +-- local protocol, domain, path = url:match("^(https?://([^/]+))(/[^?]*)") +-- if not protocol then +-- protocol, domain, path = "https://gitea.local", "gitea.local", url +-- end + +-- -- Remove trailing slashes and clean path +-- path = path:gsub("/+$", "") + +-- -- Extract components from path (format: /owner/repo or /owner/repo/branch) +-- local parts = {} +-- for segment in path:gmatch("[^/]+") do +-- 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 + +---@param str string +---@param comp string +function string.beginsWith(str, comp) + local compLen = string.len(comp) + local begining = string.sub(str, 0, compLen) + print(begining == comp, begining, comp) + return begining == comp +end + +---@param str string +---@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) - local protocol, domain, path = url:match("^(https?://([^/]+))(/[^?]*)") - if not protocol then - protocol, domain, path = "https://gitea.local", "gitea.local", url - end - - -- Remove trailing slashes and clean path - path = path:gsub("/+$", "") - - -- Extract components from path (format: /owner/repo or /owner/repo/branch) - local parts = {} - for segment in path:gmatch("[^/]+") do - 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 + local info = { + protocol = "", + origin = "", + owner = "", + repo = "", + branch = "", + 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 protocols do + if string.beginsWith(lowerUrl, proto) then + info.protocol = proto + break + end + end + local urlLen = string.len(url) + local protoLen = string.len(info.protocol) + local endLen = string.len(ending) + local cleaned = string.sub(url, protoLen, urlLen - endLen) + print("cleaned", cleaned) + + local urlParts = string.split(cleaned, "/") + if not (#urlParts) == 3 then error("Unexpected link length.") end + info.origin, info.owner, info.repo = urlParts[0], urlParts[1], urlParts[2] + + info.treeUrl = + ("%s%s/api/v1/repos/%s/%s/git/trees/%s%s?recursive=1") + :format(info.protocol, info.base, info.owner, info.repo, info.branch, ending) + + for k, v in pairs(info) do + if v == "" then + error("Could not parse " .. k .. " from link.") + end + end + print("treeUrl: ", info.treeUrl) + return info end local function downloadFile(base, owner, repo, branch, path, target) @@ -60,15 +131,14 @@ local function downloadFile(base, owner, repo, branch, path, target) end local function cloneRepo(url) - local info, err = parseGiteaURL(url) - if not info then error(err) end + local info = parseGiteaURL(url) + -- if not info then error(err) end print("Cloning: " .. info.owner .. "/" .. info.repo .. "@" .. info.branch) - local treeUrl = ("%s/api/v1/repos/%s/%s/git/trees/%s?recursive=1"):format(info.base, info.owner, info.repo, - info.branch) + -- 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 } + local req = { url = info.treeUrl } if GIT_TOKEN ~= "" then req.headers = { Authorization = "token " .. GIT_TOKEN } end @@ -102,9 +172,9 @@ local args = { ... } if args[1] == "clone" and args[2] then cloneRepo(args[2]) elseif args[1] == "update" then - local req = http.get(GIT_URL) - if not req then error("Failed to download update.") end - local code = req.readAll() + local resp = http.get(GIT_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 @@ -116,7 +186,7 @@ else print("") print("Examples:") print(' git clone "https://gitea.example.com/user/myrepo"') - print(' git clone "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("Set GIT_TOKEN variable above for private repos.") end