summaryrefslogtreecommitdiff
path: root/lib/gall/repository.lua
blob: af1ded4d0f02d7634ddfc7526e85c3325bb96047 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
-- gall.repository
--
-- Git Abstraction Layer for Lua -- Repository interface
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
--

local ll = require "gall.ll"
local object = require "gall.object"

local chomp = ll.chomp

local repomethod = {}

local pattern = {
   fullsha = string.rep("[0-9a-f]", 40),
   shortsha = string.rep("[0-9a-f]", 7),
   ref = "refs/.+"
}

for k, v in pairs(pattern) do
   pattern[k] = ("^%s$"):format(v)
end

local function _repotostring(repo)
   return "<GitRepository(" .. repo.path .. ")>"
end

function repomethod:_run_with_input_and_env(env, input, want_output, ...)
   local t = {...}
   t.repo = self.path
   if want_output then
      t.stdout = want_output
   end
   if input then
      t.stdin = input
   end
   -- We never want to see the stderr dumped to the client, so we eat it.
   t.stderr = true
   if env then
      t.env = env
   end
   return ll.rungit(t)
end

function repomethod:_run_with_input(input, want_output, ...)
   return self:_run_with_input_and_env(nil, input, want_output, ...)
end

function repomethod:_run(want_output, ...)
   return self:_run_with_input(nil, want_output, ...)
end

function repomethod:gather(...)
   return self:_run(chomp, ...)
end

function repomethod:rawgather(...)
   return self:_run(true, ...)
end

function repomethod:force_empty_tree()
   self:_run(true, "hash-object", "-t", "tree", "-w", "/dev/null")
end

function repomethod:hash_object(type, content, inject)
   local args = {
      "hash-object", "-t", type, "--stdin",
   }
   if inject then
      args[#args+1] = "-w"
   end
   local ok, sha = self:_run_with_input(content, chomp, unpack(args))
   return (ok == 0) and sha or nil
end

function repomethod:get_ref(ref)
   local ok, sha = self:_run(chomp, "show-ref", "--verify", "-s", ref)
   return (ok == 0) and sha or nil
end

function repomethod:update_ref(ref, new_ref, reason, old_ref)
   if new_ref and not old_ref then
      old_ref = string.rep("0", 40)
   end
   if not reason then
      reason = "Gall internal operations"
   end

   local cmd = { "update-ref", "-m", reason }

   if not new_ref then
      cmd[#cmd+1] = "-d"
   end

   cmd[#cmd+1] = ref

   if new_ref then
      cmd[#cmd+1] = new_ref
   end
   if old_ref then
      cmd[#cmd+1] = old_ref
   end
   
   local why = self:_run(false, unpack(cmd))
   if why ~= 0 then
      return nil, "update-ref returned " .. tostring(why)
   end
   return true
end

function repomethod:update_server_info()
   local why = self:_run(false, "update-server-info")
   if why ~= 0 then
      return nil, "update-server-info returned " .. tostring(why)
   end
   return true
end

function repomethod:all_refs()
   local ok, refs = self:_run(chomp, "show-ref")
   if ok ~= 0 then return nil, refs end
   local reft = {}
   for sha, ref in refs:gmatch("([0-9a-f]+) (refs/[^\n]+)") do
      reft[ref] = sha
   end
   return reft
end

function repomethod:normalise(sha)
   -- Purpose is to take a 'shaish' object and normalise it
   if sha:match(pattern.fullsha) then
      return sha
   elseif sha:match(pattern.ref) then
      local ref, err = self:get_ref(sha)
      return ref, err
   else
      local ok, out = self:_run_with_input(sha, chomp, "cat-file", "--batch-check")
      if not ok then
	 error(out)
      end
      sha = out:match("^("..string.rep("[0-9a-f]", 40)..")")
      return sha
   end
   return nil, "Unable to normalise " .. tostring(sha)
end

function repomethod:get(_sha)
   local sha, err = self:normalise(_sha)
   if not sha then
      return nil, err
   end
   local ret = self.objmemo[sha]
   if not ret then
      ret = object.new(self, sha)
      self.objmemo[sha] = ret
   end
   return ret
end

function repomethod:merge_base(sha_1, sha_2, get_all)
   local args = { sha_1, sha_2 }
   if get_all then
      args = { "-a", sha_1, sha_2 }
   end
   args.repo = self.path
   local ok, out = ll.merge_base(args)
   if not ok then
      return nil, out
   end
   local ret = {}
   for sha in out:gmatch("([a-f0-9]+)") do
      ret[#ret+1] = sha
   end
   if #ret == 0 then
      return true
   end
   return unpack(ret)
end

function repomethod:rev_list(oldhead, newhead, firstonly)
   local args = { newhead, "^" .. oldhead }
   if firstonly then
      table.insert(args, 1, "--first-parent")
   end
   args.repo = self.path
   local ok, out = ll.rev_list(args)
   if not ok then
      return nil, out
   end
   local ret = {}
   for sha in out:gmatch("([a-f0-9]+)") do
      ret[#ret+1] = sha
   end
   if #ret == 0 then
      return true
   end
   return ret
end

function repomethod:symbolic_ref(name, toref)
   if not toref then
      return ll.symbolic_ref { "-q", name, 
	 stderr=true, repo=self.path }
   else
      return ll.symbolic_ref { "-q", name, toref,
	 stderr=true, repo=self.path }
   end
   return false, "argh"
end

function repomethod:config(confname, value)
   -- Trivial interface to key/value in config
   if not value then
      return self:gather("config", confname)
   else
      self:gather("config", "--replace-all", confname, value)
   end
end

local repomt = {
   __index = repomethod,
   __tostring = _repotostring
}

local function _new(path)
   -- return a new git repository object
   -- with the git_dir set for the provided path
   -- and, if we had to add /.git then the GIT_WORK_DIR set
   -- appropriately too

   local retrepo = {objmemo=setmetatable({}, {__mode="v"})}

   local repopath = path
   local workpath = nil

   local ok, symref = ll.symbolic_ref { "-q", "HEAD", stderr=true, repo=repopath }
   if ok ~= 0 then
      repopath = path .. "/.git"
      workpath = path
      ok, symref = ll.symbolic_ref { "-q", "HEAD", stderr=true, repo=repopath }
   end

   if ok ~= 0 then
      return nil, "Unable to find Git repository at " .. path
   end

   retrepo.path = repopath
   retrepo.work = workpath
   retrepo.HEAD = symref

   return setmetatable(retrepo, repomt)
end

local function _create(path, full)
   -- Cause a bare repository to be created (or a non-bare if full is true)
   local args = {
      stderr = true,
      repo = path,
      "-q"
   }
   if not full then
      args[#args+1] = "--bare"
   end
   ok, msg = ll.init(args)

   if ok ~= 0 then
      return nil, "Unable to create Git repository at " .. path
   end

   -- Otherwise, return the shiny new repo
   return _new(path)
end

return {
   create = _create,
   new = _new
}