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
|
local dates = require "activity.dates"
local git = {}
function git.log (dirname, entries, filter)
entries = entries or {}
if not dirname then
error "did not provide repo"
end
local handle = io.popen(([[git -C %s log --all --format="%%aE;%%cE;%%as"]]):format(dirname))
for line in handle:lines() do
local author, committer, year, month, day = line:match"([^;]*);([^;]*);(%d+)%-(%d+)%-(%d+)"
local date = {year=tonumber(year), month=tonumber(month), day=tonumber(day)}
local entry = {author=author, committer=committer, date=date}
if not filter or filter(entry) then
table.insert(entries, entry)
end
end
local ok, _ = handle:close()
if not ok then
io.stderr:write("could not read repo: ", dirname, "\n")
end
return not ok, entries
end
function git.lookup (repositories, filter)
local lookup = {}
local errors = nil
local entries = {}
for _, dirname in pairs(repositories) do
local err, _ = git.log(dirname, entries, filter)
errors = errors or err
end
for _, entry in pairs(entries) do
local day = dates.day_of_year(entry.date)
local year = entry.date.year
lookup[year] = lookup[year] or {}
lookup[year][day] = (lookup[year][day] or 0) + 1
end
return errors, function (year, day)
return (lookup[year] or {})[day] or 0
end
end
function git.maybe_filter (field, set)
if next(set) then
return function (entry)
return set[entry[field]]
end
end
return nil
end
return git
|