summaryrefslogtreecommitdiff
path: root/lib/gitano/repocommand.lua
blob: 6cdcf0a6daded2219cf772c2228b363d23a28877 (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
-- gitano.repocommand
--
-- Gitano repository related commands such as gc, count-objects and fsck
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>

local log = require 'gitano.log'
local util = require 'gitano.util'
local repository = require 'gitano.repository'

local sp = require "luxio.subprocess"

local builtin_gc_short = "Invoke git gc on your repository"
local builtin_gc_helptext = [[
usage: gc repo [options]

Invoke, git gc, passing the given options, on the given repository.
You must have basic write access to the repository in order to invoke a gc.
]]

local function builtin_gc_prep(config, repo, cmdline, context)
   context.operation = "write"
   return repo:run_lace(context)
end

local builtin_count_objects_short = "Count objects in your projects"
local builtin_count_objects_helptext = [[
usage: count-objects repo [options]

Counts objects in your repository.

You must have read access to the repository in order 
to run count-objects.
]]

local function builtin_count_objects_prep(config, repo, cmdline, context)
   context.operation = "read"
   return repo:run_lace(context)
end

local function builtin_simple_validate(config, repo, cmdline)
   if not repo or repo.is_nascent then
      log.error("Unable to proceed, repository does not exist")
      return false
   end
   return true
end

local function builtin_simple_run(config, repo, cmdline, env)
   local cmdcopy = {env=util.deep_copy(env), "git", cmdline[1]}
   cmdcopy.env.GIT_DIR=repo:fs_path()
   for i = 3, #cmdline do cmdcopy[#cmdcopy+1] = cmdline[i] end
   local proc = sp.spawn(cmdcopy)
   return proc:wait()
end

local function register_repocommand(register_cmd)
   assert(register_cmd("gc", builtin_gc_short, builtin_gc_helptext,
		       builtin_simple_validate, builtin_gc_prep,
		       builtin_simple_run, true, false))
   
   assert(register_cmd("count-objects", builtin_count_objects_short,
		       builtin_count_objects_helptext, builtin_simple_validate,
		       builtin_count_objects_prep, builtin_simple_run,
		       true, false))
end

return {
   register = register_repocommand
}