summaryrefslogtreecommitdiff
path: root/lib/gall/object.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gall/object.lua')
-rw-r--r--lib/gall/object.lua78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/gall/object.lua b/lib/gall/object.lua
new file mode 100644
index 0000000..3b904c5
--- /dev/null
+++ b/lib/gall/object.lua
@@ -0,0 +1,78 @@
+-- gall.object
+--
+-- Git Abstraction Layer for Lua -- Generic object interface
+--
+-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
+--
+--
+
+local ll = require "gall.ll"
+local commit = require "gall.commit"
+local tree = require "gall.tree"
+local tag = require "gall.tag"
+
+local repos = setmetatable({}, {__mode="k"})
+
+local function _objectindex(obj, field)
+ local ok, ret
+ if field == "type" then
+ ok, ret = repos[obj]:gather("cat-file", "-t", obj.sha)
+ elseif field == "size" then
+ ok, ret = repos[obj]:gather("cat-file", "-s", obj.sha)
+ elseif field == "raw" then
+ ok, ret = repos[obj]:rawgather("cat-file", (obj.type == "tag" and "tag" or "-p"), obj.sha)
+ elseif field == "content" then
+ if obj.type == "blob" then
+ ok, ret = 0, obj.raw
+ elseif obj.type == "commit" then
+ ok, ret = 0, commit.new(repos[obj], obj)
+ elseif obj.type == "tree" then
+ ok, ret = 0, tree.new(repos[obj], obj)
+ elseif obj.type == "tag" then
+ ok, ret = 0, tag.new(repos[obj], obj)
+ else
+ error("Unknown type <" .. obj.type .. "> for content parse")
+ end
+ else
+ error("Unknown field <" .. tostring(field) .. ">")
+ end
+
+ assert(ok == 0, "Unable to retrieve " .. field)
+
+ if field == "size" then ret = tonumber(ret) end
+
+ rawset(obj, field, ret)
+
+ return ret
+end
+
+function _objecttostring(obj)
+ return "<GitObject(" .. tostring(obj.sha)..") in " .. tostring(repos[obj]) .. ">"
+end
+
+local objectmeta = {
+ __index = _objectindex,
+ __tostring = _objecttostring
+}
+
+local function _new(repo, sha)
+ local ret = setmetatable({sha=sha}, objectmeta)
+ repos[ret] = repo
+ return ret
+end
+
+local function _create(repo, type, content)
+ local why, sha =
+ repo:_run_with_input(content, ll.chomp,
+ "hash-object", "-t", type, "-w", "--stdin")
+ if why ~= 0 then
+ return nil, "hash-object returned " .. tostring(why)
+ end
+
+ return _new(repo, sha)
+end
+
+return {
+ new = _new,
+ create = _create,
+}