-- gall.commit -- -- Git Abstraction Layer for Lua -- Commit object interface -- -- Copyright 2012 Daniel Silverstone -- -- local ll = require "gall.ll" local objs = setmetatable({}, {__mode="k"}) local repos = setmetatable({}, {__mode="k"}) local parsed = setmetatable({}, {__mode="k"}) local _new local function parse_person(pers) local real, email, when, tz = pers:match("^(.-) <([^>]+)> ([0-9]+) ([+-][0-9]+)$") return { realname = real, email = email, unixtime = when, timezone = tz } end local function parse_parents(repo, ps) local ret = {} for _, psha in ipairs(ps) do ret[_] = repo:get(psha) end return ret end local function commitindex(commit, field) if not parsed[commit] then local raw = objs[commit].raw local headers, body, signature = {}, "" local state = "headers" for l in raw:gmatch("([^\n]*)\n") do if state == "headers" then local first, second = l:match("^([^ ]+) (.+)$") if first then if first == "gpgsig" then signature = second .. "\n" state = "signature" else if headers[first] then headers[first][#headers[first]+1] = second else headers[first] = { second } end end else state = "message" end elseif state == "signature" then signature = signature .. l:sub(2) .. "\n" if l:find("END PGP SIG") then state = "headers" end else body = body .. l .. "\n" end end -- there's always one tree rawset(commit, "tree", repos[commit]:get(headers.tree[1])) -- Always one author rawset(commit, "author", parse_person(headers.author[1])) -- Always one committer rawset(commit, "committer", parse_person(headers.committer[1])) -- Zero or more parents headers.parent = headers.parent or {} rawset(commit, "parents", parse_parents(repos[commit], headers.parent)) -- A message rawset(commit, "message", body) -- And an optional signature rawset(commit, "signature", signature) -- Promote the SHA rawset(commit, "sha", objs[commit].sha) -- Signal we are parsed parsed[commit] = true end return rawget(commit, field) end local function committostring(commit) return "" end local commitmeta = { __index = commitindex, __tostring = committostring } local function _new(repo, obj) local ret = setmetatable({}, commitmeta) objs[ret] = obj repos[ret] = repo return ret end local function _create(repo, data) if not data.tree then return nil, "No tree?" end if not data.author then return nil, "No author?" end if not data.committer then return nil, "No committer?" end if not data.author.realname then return nil, "No author name?" end if not data.author.email then return nil, "No author email?" end if not data.committer.realname then return nil, "No committer name?" end if not data.committer.email then return nil, "No committer email?" end if not data.message then return nil, "No message?" end if not data.parents then data.parents = {} end -- Construct the commandline and environment local env = { GIT_AUTHOR_NAME = data.author.realname, GIT_AUTHOR_EMAIL = data.author.email, GIT_COMMITTER_NAME = data.committer.realname, GIT_COMMITTER_EMAIL = data.committer.email, } local cmd = { "commit-tree", data.tree.sha } for i, v in ipairs(data.parents) do cmd[#cmd+1] = "-p" if not v.sha then return nil, "Parent " .. tostring(i) .. " had no sha?" end cmd[#cmd+1] = v.sha end if not data.message:match("\n$") then data.message = data.message .. "\n" end local why, sha, err = repo:_run_with_input_and_env(env, data.message, ll.chomp, unpack(cmd)) if why ~= 0 then return nil, "commit-tree returned " .. tostring(why) .. (sha or "") .. "\n" .. (err or "") end return repo:get(sha) end return { create = _create, new = _new }