summaryrefslogtreecommitdiff
path: root/lib/gitano/copycommand.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitano/copycommand.lua')
-rw-r--r--lib/gitano/copycommand.lua108
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/gitano/copycommand.lua b/lib/gitano/copycommand.lua
new file mode 100644
index 0000000..6de825a
--- /dev/null
+++ b/lib/gitano/copycommand.lua
@@ -0,0 +1,108 @@
+-- gitano.copycommand
+--
+-- Gitano repository copy commands
+--
+-- Copyright 2013 Richard Maw <richard.maw@gmail.com>
+
+local repository = require "gitano.repository"
+local log = require "gitano.log"
+local util = require "gitano.util"
+
+local builtin_copy_short = "Copy a repository to a new path"
+
+local builtin_copy_helptext = [[
+usage: copy <oldrepo> <newrepo>
+
+Copy a git repository locally. This is quicker than fetching the
+old repository, creating the new one, then pushing to the new one.
+]]
+
+local function builtin_copy_validate(config, srcrepo, cmdline)
+ if #cmdline ~= 3 then
+ log.error("usage: copy <oldrepo> <newrepo>")
+ return false
+ end
+ -- Check if source repository is nascent
+ if srcrepo.is_nascent then
+ return "deny", "Cannot copy a repository which does not exist"
+ end
+ -- Create the target repository object
+ local tgtrepo, msg = repository.find(config, cmdline[3])
+ if not tgtrepo then
+ log.critical("Unable to locate repository.")
+ log.critical(" * " .. (tostring(msg)))
+ log.fatal("Cannot continue")
+ return false
+ end
+ if not tgtrepo.is_nascent then
+ log.error("Repository", tgtrepo.name, "already exists")
+ return false
+ end
+ cmdline.tgtrepo = tgtrepo
+ return true
+end
+
+local function builtin_copy_prep(config, srcrepo, cmdline, context)
+ local ctx, action, reason
+ -- Check 1, source repository can be read
+ ctx = util.deep_copy(context)
+ ctx.operation = "read"
+ action, reason = srcrepo:run_lace(ctx)
+ if action ~= "allow" then
+ return action, reason
+ end
+ -- Check 2, target repository can be created
+ ctx = util.deep_copy(context)
+ ctx.operation = "createrepo"
+ action, reason = cmdline.tgtrepo:run_lace(ctx)
+ if action ~= "allow" then
+ return action, reason
+ end
+ -- Able to read and create, thus can copy
+ return "allow", "Passed all checks, can copy"
+end
+
+local function builtin_copy_run(config, repo, cmdline, env)
+ local ok, msg = repo:copy_to(cmdline.tgtrepo)
+ if not ok then
+ log.error(msg)
+ return "exit", 1
+ end
+ log.state("Copied", cmdline[2], "to", cmdline[3])
+
+ local tgtrepo, msg = repository.find(config, cmdline[3])
+ if not tgtrepo then
+ log.critical("Unable to locate repository.")
+ log.critical(" * " .. (tostring(msg)))
+ log.fatal("Cannot continue")
+ return false
+ end
+
+ local owner = env["GITANO_USER"]
+ log.chat("Setting repository owner to", owner)
+ ok, msg = tgtrepo:set_owner(owner)
+ if not ok then
+ log.error(msg)
+ return "exit", 1
+ end
+ log.chat("Running checks to ensure hooks etc are configured")
+ ok, msg = tgtrepo:run_checks()
+ if not ok then
+ log.error(msg)
+ return "exit", 1
+ end
+ log.state("Repository", tgtrepo.name,
+ "copied ok. Remember to configure rules etc.")
+
+ return "exit", 0
+end
+
+local function register_commands(reg)
+ assert(reg("copy", builtin_copy_short, builtin_copy_helptext,
+ builtin_copy_validate, builtin_copy_prep, builtin_copy_run,
+ true, false, false))
+end
+
+return {
+ register = register_commands
+}