summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-08 22:28:26 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-08 22:28:26 +0100
commit89e28dfa06f176f0887fd5962d1c07b86df8c148 (patch)
tree652a2f4311555845c06705536b2093ef84511d40
parent351eea0d80739567da34c85cf9570ded393561a9 (diff)
downloadgall-89e28dfa06f176f0887fd5962d1c07b86df8c148.tar.gz
GALL.REPOSITORY: Use libgit2 some more
-rw-r--r--lib/gall/repository.lua77
1 files changed, 67 insertions, 10 deletions
diff --git a/lib/gall/repository.lua b/lib/gall/repository.lua
index 32be33c..7c4dc81 100644
--- a/lib/gall/repository.lua
+++ b/lib/gall/repository.lua
@@ -82,6 +82,19 @@ function repomethod:get_ref(ref)
return (ok == 0) and sha or nil
end
+if ll.git2 then
+ function repomethod:get_ref(ref)
+ local rref = ll.git2.Reference.lookup(self.git2.repo, ref)
+ if not rref then
+ return nil
+ end
+ if rref:type() ~= ll.git2.REF_OID then
+ return nil
+ end
+ return tostring(rref:oid())
+ end
+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)
@@ -138,11 +151,26 @@ function repomethod:normalise(sha)
local ref, err = self:get_ref(sha)
return ref, err
else
- local ok, out, err = self:_run_with_input(sha, chomp, "cat-file", "--batch-check")
- if ok ~= 0 then
- error((out or "") .. "\n" .. (err or ""))
+ local fullsha
+ if ll.git2 then
+ local refobj = ll.git2.Reference.lookup(self.git2.repo, sha)
+ if refobj then
+ if refobj:type() == ll.git2.REF_SYMBOLIC then
+ refobj = ll.git2.Reference.lookup(self.git2.repo,
+ refobj:target())
+ end
+ if refobj:type() == ll.git2.REF_OID then
+ fullsha = tostring(refobj:oid())
+ end
+ end
+ end
+ if not fullsha then
+ local ok, out, err = self:_run_with_input(sha, chomp, "cat-file", "--batch-check")
+ if ok ~= 0 then
+ error((out or "") .. "\n" .. (err or ""))
+ end
+ fullsha = out:match("^("..string.rep("[0-9a-f]", 40)..")")
end
- local fullsha = out:match("^("..string.rep("[0-9a-f]", 40)..")")
if fullsha then
return fullsha
end
@@ -223,6 +251,22 @@ function repomethod:symbolic_ref(name, toref)
return nil, (ref or "") .. "\n" .. (err or "")
end
+if ll.git2 then
+ function repomethod:symbolic_ref(name, toref)
+ local symref = ll.git2.Reference.lookup(self.git2.repo, name)
+ if not symref then
+ return nil, "No such ref: " .. tostring(toref)
+ end
+ if symref:type() ~= ll.git2.REF_SYMBOLIC then
+ return false
+ end
+ if toref then
+ symref:set_target(toref)
+ end
+ return true, symref:target()
+ end
+end
+
function repomethod:config(confname, value)
-- Trivial interface to key/value in config
if not value then
@@ -248,17 +292,30 @@ local function _new(path)
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"
+ local ok = luxio.stat(repopath .. "/.git")
+ if ok == 0 then
+ repopath = repopath .. "/.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
+ local symref
+ if ll.git2 then
+ local git2, msg = ll.git2.Repository(repopath)
+ if not git2 then
+ return nil, "Unable to find Git repository at " .. path
+ end
+ local odb = git2:odb()
+ retrepo.git2 = { repo = git2, odb = odb }
+ symref = ll.git2.Reference.lookup(git2, "HEAD")
+ symref = symref:target()
+ else
+ ok, symref = ll.symbolic_ref { "-q", "HEAD", stderr=true, repo=repopath }
+ if ok ~= 0 then
+ return nil, "Unable to find Git repository at " .. path
+ end
end
+
retrepo.path = repopath
retrepo.work = workpath
retrepo.HEAD = symref