-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
81 lines (61 loc) · 1.5 KB
/
stat.go
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
package main
import (
"fmt"
"sort"
"time"
"path/filepath"
"github.com/go-git/go-git"
"gopkg.in/src-d/go-git.v5/plumbing/object"
)
const outOfRange = 99999
const daysInLastSixMonths = 183
const weeksInLastSixMonths = 26
func stats(email string){
commits := processRepos(email)
printStats(commits)
}
func processRepos(email string) map[int]int{
filepath := getDotFilePath()
repos := filetoSlice(filepath)
commits := make(map[int]int, daysInLastSixMonths)
for i := daysInLastSixMonths; i > 0; i--{
commits[i] = 0
}
for _, i := range repos{
commits := fillCommits(email, i, commits)
}
return commits
}
func fillCommits(email string, path string, commits map[int]int) map[int]int{
repo, err := git.PlainOpen(path)
if err != nil{
panic(err)
}
ref, err := repo.Head()
if err != nil {
panic(err)
}
// get the commits history starting from HEAD
iterator, err := repo.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
panic(err)
}
// iterate the commits
offset := calcOffset()
err = iterator.ForEach(func(c *object.Commit) error {
daysAgo := countDaysSinceDate(c.Author.When) + offset
if c.Author.Email != email {
return nil
}
if daysAgo != outOfRange {
commits[daysAgo]++
}
return nil
})
if err != nil {
panic(err)
}
return commits
}
func printStats(commits map[int]int){
}