summaryrefslogtreecommitdiff
path: root/lib/gitano/copycommand.lua
blob: 6de825a676c9f6bf71dbc820cbb8b9b5e4bf20ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
}