summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@gmail.com>2013-05-23 21:59:20 +0100
committerRichard Maw <richard.maw@gmail.com>2013-05-27 14:38:17 +0100
commit186495107f9978e204230bb9a3f3d0c612a3542b (patch)
treec4b4ec439b30df2424aadedf97425957a78d7053
parentc8a485d91f8378a49146d2d53cfbb760fbd23be3 (diff)
downloadgitano-186495107f9978e204230bb9a3f3d0c612a3542b.tar.gz
commands: add copy <source> <target>
-rw-r--r--lib/gitano/command.lua2
-rw-r--r--lib/gitano/copycommand.lua108
2 files changed, 110 insertions, 0 deletions
diff --git a/lib/gitano/command.lua b/lib/gitano/command.lua
index f8b25a8..34e3053 100644
--- a/lib/gitano/command.lua
+++ b/lib/gitano/command.lua
@@ -994,6 +994,8 @@ local usercmds = require 'gitano.usercommand'
usercmds.register(register_cmd)
local admincmds = require 'gitano.admincommand'
admincmds.register(register_cmd)
+local copycmds = require 'gitano.copycommand'
+copycmds.register(register_cmd)
return {
register = register_cmd,
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
+}