97 lines
2.6 KiB
Lua
97 lines
2.6 KiB
Lua
local git = require("git")
|
|
local json = require("json")
|
|
local fs = require("fs")
|
|
local cmd = require("cmd")
|
|
|
|
local function get_command(args)
|
|
-- Validate URL
|
|
local repo_url = args.repo
|
|
local url_type = git.valid_url(repo_url)
|
|
if url_type == -1 then
|
|
io.stderr:write(repo_url .. " is not a valid repo\n")
|
|
os.exit(1)
|
|
end
|
|
|
|
-- Parse URL
|
|
local domain, path = git.parse_url(repo_url)
|
|
if not domain or not path then
|
|
io.stderr:write("Failed to parse repository URL: " .. repo_url .. "\n")
|
|
os.exit(1)
|
|
end
|
|
|
|
-- Get configuration
|
|
local base_path = os.getenv("REPOTOOL_PATH") or os.getenv("HOME") .. "/repo"
|
|
local method = args.method or "ssh"
|
|
local ssh_user = args.ssh_user or "git"
|
|
local http_user = args.http_user or ""
|
|
local http_pass = args.http_pass or ""
|
|
|
|
-- Debug output
|
|
local function lcat(text)
|
|
if os.getenv("DEBUG_LOG") == "1" then
|
|
io.stderr:write(text)
|
|
end
|
|
end
|
|
|
|
lcat(string.format([[
|
|
found valid repo target
|
|
|
|
domain: %s
|
|
path: %s
|
|
|
|
ssh_user: %s
|
|
method: %s
|
|
|
|
http_user: %s
|
|
http_pass: %s
|
|
]], domain, path, ssh_user, method, http_user, http_pass))
|
|
|
|
-- Construct target directory
|
|
local target_dir = base_path .. "/" .. domain .. "/" .. path
|
|
|
|
-- Create directory if it doesn't exist
|
|
if not fs.dir_exists(target_dir) then
|
|
fs.mkdir_p(target_dir)
|
|
end
|
|
|
|
-- Note: cd command doesn't work in os.execute as it runs in a subshell
|
|
|
|
-- Construct repo URL based on method
|
|
local clone_url
|
|
if method == "ssh" then
|
|
clone_url = ssh_user .. "@" .. domain .. ":" .. path
|
|
elseif method == "https" or method == "http" then
|
|
-- TODO: support http_user and http_pass
|
|
clone_url = method .. "://" .. domain .. "/" .. path .. ".git"
|
|
else
|
|
io.stderr:write("unrecognized clone method " .. method .. "\n")
|
|
os.exit(1)
|
|
end
|
|
|
|
-- Check if we need to clone
|
|
local cloned = "false"
|
|
local git_dir = fs.join(target_dir, ".git")
|
|
if not fs.dir_exists(git_dir) then
|
|
-- Check if remote exists
|
|
local check_cmd = "cd '" .. target_dir .. "' && git ls-remote '" .. clone_url .. "'"
|
|
if cmd.run(check_cmd) == 0 then
|
|
cmd.execute("git clone '" .. clone_url .. "' '" .. target_dir .. "'")
|
|
cloned = "true"
|
|
else
|
|
io.stderr:write("Could not find repo: " .. clone_url .. "\n")
|
|
os.exit(1)
|
|
end
|
|
end
|
|
|
|
-- Output JSON
|
|
print(json.encode({
|
|
cd = target_dir,
|
|
domain = domain,
|
|
path = path,
|
|
repo_url = clone_url,
|
|
cloned = cloned,
|
|
hook = "get"
|
|
}))
|
|
end
|
|
|
|
return get_command |