summaryrefslogtreecommitdiff
path: root/lib/gall/commit.lua
blob: 080c7ed87b78dd3d9cac5bddba2f940907518829 (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
-- gall.commit
--
-- Git Abstraction Layer for Lua -- Commit object interface
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
--

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 "<GitCommit(" .. tostring(objs[commit].sha) .. ") in " .. tostring(repos[commit]) .. ">"
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"
      cmd[#cmd+1] = v.sha
   end

   if not data.message:match("\n$") then
      data.message = data.message .. "\n"
   end

   local why, sha = repo:_run_with_input_and_env(env, data.message,
						 ll.chomp, unpack(cmd))

   if why ~= 0 then
      return nil, "commit-tree returned " .. tostring(why)
   end

   return repo:get(sha)
end

return {
   create = _create,
   new = _new
}