diff options
Diffstat (limited to 'lib/gitano/command.lua')
-rw-r--r-- | lib/gitano/command.lua | 406 |
1 files changed, 203 insertions, 203 deletions
diff --git a/lib/gitano/command.lua b/lib/gitano/command.lua index 0fa8be8..9b83c01 100644 --- a/lib/gitano/command.lua +++ b/lib/gitano/command.lua @@ -34,14 +34,14 @@ local function default_detect_repo(config, parsed_cmdline) end local function register_cmd(cmdname, short, helptext, - validate_fn, prep_fn, run_fn, - takes_repo, hidden, is_admin, detect_repo, - suppress_error_msgs) + validate_fn, prep_fn, run_fn, + takes_repo, hidden, is_admin, detect_repo, + suppress_error_msgs) --[[ - log.ddebug("Register command", cmdname) - if takes_repo then + log.ddebug("Register command", cmdname) + if takes_repo then log.ddebug(" => Takes a repo") - end + end --]] if cmds[cmdname] then log.warn("Attempt to double-register", cmdname) @@ -100,11 +100,11 @@ local function builtin_help_validate(config, repo, cmdline) end if #cmdline == 2 then if cmdline[2] == "all" or cmdline[2] == "admin" then - return true + return true end if not cmds[cmdline[2]] then - log.error("Unknown command:", help) - return false + log.error("Unknown command:", help) + return false end end return true @@ -117,10 +117,10 @@ end local function builtin_help_run(config, repo, cmdline, env) local function do_want(cmd) if cmdline[2] == "all" then - return true + return true end if cmdline[2] == "admin" then - return cmd.admin + return cmd.admin end return not (cmd.hidden or cmd.admin) end @@ -133,46 +133,46 @@ local function builtin_help_run(config, repo, cmdline, env) -- List all commands local maxcmdn = 0 for i = 1, #cmds do - local cmd = cmds[cmds[i]] - local wanted = do_want(cmd) - if wanted then - if #cmd.name > maxcmdn then - maxcmdn = #cmd.name - end - end + local cmd = cmds[cmds[i]] + local wanted = do_want(cmd) + if wanted then + if #cmd.name > maxcmdn then + maxcmdn = #cmd.name + end + end end for i = 1, #cmds do - local cmd = cmds[cmds[i]] - local wanted = do_want(cmd) - if wanted then - local gap = (" "):rep(maxcmdn - #cmd.name) - local desc = (cmd.short or "No description") - if cmd.takes_repo then - desc = desc .. " (Takes a repo)" - end - log.state(gap .. cmd.name, do_sep(cmd), desc) - end + local cmd = cmds[cmds[i]] + local wanted = do_want(cmd) + if wanted then + local gap = (" "):rep(maxcmdn - #cmd.name) + local desc = (cmd.short or "No description") + if cmd.takes_repo then + desc = desc .. " (Takes a repo)" + end + log.state(gap .. cmd.name, do_sep(cmd), desc) + end end else local cmd = cmds[cmdline[2]] local desc = (cmd.short or "No description") if cmd.takes_repo then - desc = desc .. " (Takes a repo)" + desc = desc .. " (Takes a repo)" end log.state(cmd.name, do_sep(cmd), desc) if cmd.helptext then - log.state("") - for line in (cmd.helptext):gmatch("([^\n]*)\n") do - log.state("=>", line) - end + log.state("") + for line in (cmd.helptext):gmatch("([^\n]*)\n") do + log.state("=>", line) + end end end return "exit", 0 end assert(register_cmd("help", builtin_help_short, builtin_help_helptext, - builtin_help_validate, builtin_help_prep, - builtin_help_run, false, false)) + builtin_help_validate, builtin_help_prep, + builtin_help_run, false, false)) local function builtin_upload_pack_validate(config, repo, cmdline) -- git-upload-pack repo @@ -199,10 +199,10 @@ local function builtin_upload_pack_run(config, repo, cmdline, env) end assert(register_cmd("git-upload-pack", nil, nil, - builtin_upload_pack_validate, - builtin_upload_pack_prep, - builtin_upload_pack_run, - true, true)) + builtin_upload_pack_validate, + builtin_upload_pack_prep, + builtin_upload_pack_run, + true, true)) local function builtin_receive_pack_validate(config, repo, cmdline) -- git-receive-pack repo @@ -236,10 +236,10 @@ local function builtin_receive_pack_run(config, repo, cmdline, env) end assert(register_cmd("git-receive-pack", nil, nil, - builtin_receive_pack_validate, - builtin_receive_pack_prep, - builtin_receive_pack_run, - true, true)) + builtin_receive_pack_validate, + builtin_receive_pack_prep, + builtin_receive_pack_run, + true, true)) local builtin_create_short = "Create a new repository" local builtin_create_helptext = [[ @@ -286,7 +286,7 @@ local function builtin_create_run(config, repo, cmdline, env) local owner = cmdline[3] or env["GITANO_USER"] log.chat("Setting repository owner to", owner) ok, msg = repo:conf_set_and_save("project.owner", owner, - env.GITANO_USER, env.GITANO_ORIG_USER) + env.GITANO_USER, env.GITANO_ORIG_USER) if not ok then log.error(msg) return "exit", 1 @@ -297,14 +297,14 @@ local function builtin_create_run(config, repo, cmdline, env) log.error(msg) return "exit", 1 end - log.state("Repository", repo.name, - "created ok. Remember to configure rules etc.") + log.state("Repository", repo.name, + "created ok. Remember to configure rules etc.") return "exit", 0 end assert(register_cmd("create", builtin_create_short, builtin_create_helptext, - builtin_create_validate, builtin_create_prep, - builtin_create_run, true, false)) + builtin_create_validate, builtin_create_prep, + builtin_create_run, true, false)) local builtin_config_short = "View and change configuration for a repository" local builtin_config_helptext = [[ @@ -348,39 +348,39 @@ local function builtin_config_validate(conf, repo, cmdline) -- No validation to do yet elseif cmdline[3] == "set" then if #cmdline < 5 then - log.error("config <repo> set: takes a key and a value to set") - return false + log.error("config <repo> set: takes a key and a value to set") + return false end if #cmdline > 5 then - local cpy = {} - for i = #cmdline, 5, -1 do - table.insert(cpy, 1, cmdline[i]) - cmdline[i] = nil - end - cmdline[5] = table.concat(cpy, " ") + local cpy = {} + for i = #cmdline, 5, -1 do + table.insert(cpy, 1, cmdline[i]) + cmdline[i] = nil + end + cmdline[5] = table.concat(cpy, " ") end if cmdline[4] == "project.owner" then - -- Verify that the new owner is a valid user - if not conf.users[cmdline[5]] then - log.error("Unknown user: " .. cmdline[5]) - return false - end + -- Verify that the new owner is a valid user + if not conf.users[cmdline[5]] then + log.error("Unknown user: " .. cmdline[5]) + return false + end end elseif cmdline[3] == "del" or cmdline[3] == "delete" or - cmdline[3] == "rm" then + cmdline[3] == "rm" then cmdline[3] = "del" if #cmdline ~= 4 then - log.error("config <repo> del: takes a key and nothing more") - return false + log.error("config <repo> del: takes a key and nothing more") + return false end cmdline.orig_key = cmdline[4] if cmdline[4]:match("%.%*$") then - -- Doing a wild removal, expand it now - local prefix = cmdline[4]:match("^(.+)%.%*$") - cmdline[4] = nil - for k in repo.project_config:each(prefix) do - cmdline[#cmdline+1] = k - end + -- Doing a wild removal, expand it now + local prefix = cmdline[4]:match("^(.+)%.%*$") + cmdline[4] = nil + for k in repo.project_config:each(prefix) do + cmdline[#cmdline+1] = k + end end else log.error("Unknown subcommand <" .. cmdline[3] .. "> for config.") @@ -394,12 +394,12 @@ local function builtin_config_prep(conf, repo, cmdline, context) if cmdline[3] == "show" then context.operation = "config_show" for i = 4, #cmdline do - local cpy = util.deep_copy(context) - cpy.key = cmdline[i] - local action, reason = repo:run_lace(cpy) - if action ~= "allow" then - return action, reason - end + local cpy = util.deep_copy(context) + cpy.key = cmdline[i] + local action, reason = repo:run_lace(cpy) + if action ~= "allow" then + return action, reason + end end return "allow", "Show not denied" elseif cmdline[3] == "set" then @@ -410,12 +410,12 @@ local function builtin_config_prep(conf, repo, cmdline, context) elseif cmdline[3] == "del" then context.operation = "config_del" for i = 4, #cmdline do - local cpy = util.deep_copy(context) - cpy.key = cmdline[i] - local action, reason = repo:run_lace(cpy) - if action ~= "allow" then - return action, reason - end + local cpy = util.deep_copy(context) + cpy.key = cmdline[i] + local action, reason = repo:run_lace(cpy) + if action ~= "allow" then + return action, reason + end end return "allow", "Delete not denied" end @@ -426,70 +426,70 @@ local function builtin_config_run(conf, repo, cmdline, env) if cmdline[3] == "show" then local all_keys = {} if #cmdline == 3 then - for k in repo.project_config:each() do - all_keys[k] = true - end + for k in repo.project_config:each() do + all_keys[k] = true + end else - for i = 4, #cmdline do - for k in repo.project_config:each(cmdline[i]) do - all_keys[k] = true - end - end + for i = 4, #cmdline do + for k in repo.project_config:each(cmdline[i]) do + all_keys[k] = true + end + end end -- Transform the all_keys set into a sorted list local slist = {} for k in pairs(all_keys) do - slist[#slist+1] = k + slist[#slist+1] = k end -- TODO: Fix this sort to cope with .i_N keys neatly table.sort(slist) for i = 1, #slist do - local key = slist[i] - local value = repo.project_config.settings[key] - local prefix = key:match("^(.+)%.i_[0-9]+$") - if prefix then - local neatkey = prefix .. ".*" - for i = 4, #cmdline do - if cmdline[i] == neatkey then - neatkey = key - break - end - end - end - log.stdout(key .. ": " .. value) + local key = slist[i] + local value = repo.project_config.settings[key] + local prefix = key:match("^(.+)%.i_[0-9]+$") + if prefix then + local neatkey = prefix .. ".*" + for i = 4, #cmdline do + if cmdline[i] == neatkey then + neatkey = key + break + end + end + end + log.stdout(key .. ": " .. value) end elseif cmdline[3] == "set" then local key, value = cmdline[4], cmdline[5] local vtype, rest = value:match("^([sbi]):(.*)$") if vtype then - if vtype == "s" then - value = rest - end - if vtype == "i" then - value = tonumber(rest) - end - if vtype == "b" then - value = ((rest:lower() == "true") or (rest == "1") or - (rest:lower() == "on") or (rest:lower() == "yes")) - end + if vtype == "s" then + value = rest + end + if vtype == "i" then + value = tonumber(rest) + end + if vtype == "b" then + value = ((rest:lower() == "true") or (rest == "1") or + (rest:lower() == "on") or (rest:lower() == "yes")) + end end repo.project_config.settings[key] = value local ok, msg = repo:save_admin("Changed project setting: " .. key, - env.GITANO_USER, env.GITANO_ORIG_USER) + env.GITANO_USER, env.GITANO_ORIG_USER) if not ok then - log.error(msg) - return "exit", 2 + log.error(msg) + return "exit", 2 end elseif cmdline[3] == "del" then local key = cmdline.orig_key for i = 4, #cmdline do - repo.project_config.settings[cmdline[4]] = nil + repo.project_config.settings[cmdline[4]] = nil end local ok, msg = repo:save_admin("Deleted project setting: " .. key, - env.GITANO_USER, env.GITANO_ORIG_USER) + env.GITANO_USER, env.GITANO_ORIG_USER) if not ok then - log.error(msg) - return "exit", 2 + log.error(msg) + return "exit", 2 end else log.error("Unknown sub command slipped through") @@ -499,9 +499,9 @@ local function builtin_config_run(conf, repo, cmdline, env) end assert(register_cmd("config", builtin_config_short, - builtin_config_helptext, - builtin_config_validate, builtin_config_prep, - builtin_config_run, true, false)) + builtin_config_helptext, + builtin_config_validate, builtin_config_prep, + builtin_config_run, true, false)) local builtin_destroy_short = "Destroy (delete) a repository" local builtin_destroy_helptext = [[ @@ -544,24 +544,24 @@ local function builtin_destroy_run(config, repo, cmdline, env) log.stdout(" ", token) else if cmdline[3] ~= token and cmdline[3] ~= '--force' then - log.error("Confirmation token does not match, refusing to destroy") - return "exit", 1 + log.error("Confirmation token does not match, refusing to destroy") + return "exit", 1 end -- Tokens match, ask the repo to destroy itself local nowstamp = os.date("!%Y-%m-%d.%H:%M:%S.UTC") local ok, msg = repo:destroy_self(nowstamp .. "." .. - (repo.name:gsub("[^A-Za-z0-9_%.%-]", - "_")) .. - "." .. token .. ".destroyed") + (repo.name:gsub("[^A-Za-z0-9_%.%-]", + "_")) .. + "." .. token .. ".destroyed") if not ok then - log.error(msg) - return "exit", 1 + log.error(msg) + return "exit", 1 end log.stdout("Should you need to recover the repository you just destroyed") log.stdout("then you will need to speak with an admin as soon as possible") log.stdout("") - log.stdout("When you do, be sure to include the current time (" .. - nowstamp .. ").") + log.stdout("When you do, be sure to include the current time (" .. + nowstamp .. ").") log.stdout("It may also help if you include your token:") log.stdout(" ", token) log.stdout("") @@ -570,10 +570,10 @@ local function builtin_destroy_run(config, repo, cmdline, env) return "exit", 0 end -assert(register_cmd("destroy", builtin_destroy_short, - builtin_destroy_helptext, builtin_destroy_validate, - builtin_destroy_prep, builtin_destroy_run, - true, false)) +assert(register_cmd("destroy", builtin_destroy_short, + builtin_destroy_helptext, builtin_destroy_validate, + builtin_destroy_prep, builtin_destroy_run, + true, false)) local builtin_rename_short = "Rename a repository" local builtin_rename_helptext = [[ @@ -643,8 +643,8 @@ local function builtin_rename_run(config, repo, cmdline, env) end assert(register_cmd("rename", builtin_rename_short, builtin_rename_helptext, - builtin_rename_validate, builtin_rename_prep, - builtin_rename_run, true, false)) + builtin_rename_validate, builtin_rename_prep, + builtin_rename_run, true, false)) local builtin_ls_short = "List repositories on the server" local builtin_ls_helptext = [[ @@ -705,10 +705,10 @@ local function builtin_ls_run(config, _, cmdline, env) local firstpat, verbose, all = 2, false, false while lsargs[cmdline[firstpat]] do if lsargs[cmdline[firstpat]] == "verbose" then - firstpat, verbose = firstpat + 1, true + firstpat, verbose = firstpat + 1, true end if lsargs[cmdline[firstpat]] == "all" then - firstpat, all = firstpat + 1, true + firstpat, all = firstpat + 1, true end end for i = firstpat, #cmdline do @@ -716,53 +716,53 @@ local function builtin_ls_run(config, _, cmdline, env) local escaping, star, used_evil = false, false, false c, input = input:match("^(.)(.*)$") while c and c ~= "" do - if escaping then - pat = pat .. (builtin_ls_special[c] and "%" or "") .. c - if c == "/" then - used_evil = true - end - escaping = false - else - if c == "*" then - if star then - -- ** - pat = pat .. ".*" - used_evil = true - star = false - else - star = true - end - else - if star then - -- * - pat = pat .. "[^/]*" - star = false - end - if c == "?" then - pat = pat .. "[^/]" - elseif c == "\\" then - escaping = true - else - pat = pat .. (builtin_ls_special[c] and "%" or "") .. c - if c == "/" then - used_evil = true - end - end - end - end - c, input = input:match("^(.)(.*)$") + if escaping then + pat = pat .. (builtin_ls_special[c] and "%" or "") .. c + if c == "/" then + used_evil = true + end + escaping = false + else + if c == "*" then + if star then + -- ** + pat = pat .. ".*" + used_evil = true + star = false + else + star = true + end + else + if star then + -- * + pat = pat .. "[^/]*" + star = false + end + if c == "?" then + pat = pat .. "[^/]" + elseif c == "\\" then + escaping = true + else + pat = pat .. (builtin_ls_special[c] and "%" or "") .. c + if c == "/" then + used_evil = true + end + end + end + end + c, input = input:match("^(.)(.*)$") end if star then - -- spare star - pat = pat .. "[^/]*" + -- spare star + pat = pat .. "[^/]*" end if cmdline[i]:match("/$") then pat = pat .. ".*" end if used_evil then - pat = "^/" .. pat .. "%.git$" + pat = "^/" .. pat .. "%.git$" else - pat = "/" .. pat .. "%.git$" + pat = "/" .. pat .. "%.git$" end log.debug("PAT:", pat) pats[#pats+1] = pat @@ -775,31 +775,31 @@ local function builtin_ls_run(config, _, cmdline, env) local _ctx = cmdline._ctx local function filter_callback(name) for i = 1, #pats do - if ("/" .. name):match(pats[i]) then - return true - end + if ("/" .. name):match(pats[i]) then + return true + end end end local function callback(reponame, repo, msg) if repo then - local archived = repo:conf_get("project.archived") - if archived and not all then return end - local ctx = util.deep_copy(_ctx) - ctx.operation = "read" - local action, reason = repo:run_lace(ctx) - if action == "allow" then - ctx = util.deep_copy(_ctx) - ctx.operation = "write" - action, reason = repo:run_lace(ctx) - local tail = "" - if verbose then - local desc = repo:conf_get("project.description") - desc = desc:gsub("\n.*", "") - tail = " " .. repo:conf_get("project.head") .. " " .. desc - end - log.stdout((action == "allow" and "RW" or "R ") .. - (archived and "A" or " "), repo.name .. tail) - end + local archived = repo:conf_get("project.archived") + if archived and not all then return end + local ctx = util.deep_copy(_ctx) + ctx.operation = "read" + local action, reason = repo:run_lace(ctx) + if action == "allow" then + ctx = util.deep_copy(_ctx) + ctx.operation = "write" + action, reason = repo:run_lace(ctx) + local tail = "" + if verbose then + local desc = repo:conf_get("project.description") + desc = desc:gsub("\n.*", "") + tail = " " .. repo:conf_get("project.head") .. " " .. desc + end + log.stdout((action == "allow" and "RW" or "R ") .. + (archived and "A" or " "), repo.name .. tail) + end end end repository.foreach(config, callback, filter_callback) @@ -807,8 +807,8 @@ local function builtin_ls_run(config, _, cmdline, env) end assert(register_cmd("ls", builtin_ls_short, builtin_ls_helptext, - builtin_ls_validate, builtin_ls_prep, builtin_ls_run, - false, false)) + builtin_ls_validate, builtin_ls_prep, builtin_ls_run, + false, false)) local usercmds = require 'gitano.usercommand' |