This repository was archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_stats.go
127 lines (105 loc) · 2.69 KB
/
calculate_stats.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
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
package main
import (
"time"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// func calcOffset returns the amount of days missing
// to fill the last row of stats graph
func calcOffset() int {
var offset int
weekday := time.Now().Weekday()
switch weekday {
case time.Sunday:
offset = 7
case time.Monday:
offset = 6
case time.Tuesday:
offset = 5
case time.Wednesday:
offset = 4
case time.Thursday:
offset = 3
case time.Friday:
offset = 2
case time.Saturday:
offset = 1
}
return offset
}
// getBeginningOfDay given a time.Time calculates the start time of that day
func getBeginningOfDay(t time.Time) time.Time {
year, month, day := t.Date()
startOfDay := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
return startOfDay
}
// func countDaysSinceDate return how many days have passed since the given date
func countDaysSinceDate(date time.Time) int {
days := 0
now := getBeginningOfDay(time.Now())
for date.Before(now) {
date = date.Add(time.Hour*24)
days++
if days > daysInLastSixMonths {
return outOfRange
}
}
return days
}
// fillCommits given a repository found in `path`, gets the commits and
// puts them in the `commits` map, returning it when completed
func fillCommits(email string, path string, commits map[int]int) map[int]int {
// instantiate repo object from path
repo, err := git.PlainOpen(path)
if err != nil {
panic(err)
}
// get HEAD reference
ref, err := repo.Head()
if err != nil {
panic(err)
}
// get commit 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 {
// outOfRange is a constant which countDaysSinceDate returns
// when commit is more than six months old
commits[daysAgo]++
}
return nil
})
if err != nil {
panic(err)
}
return commits
}
// func processRepositories given a email address, returns
// commits made in the last six months
func processRepositories(email string) map[int]int {
file_path := getDotFilePath()
repos := parseFileLinesToSlice(file_path)
daysInMap := daysInLastSixMonths // daysInLastSixMonths is a constant
commits := make(map[int]int, daysInMap)
for i := daysInMap; i > 0; i-- {
commits[i] = 0
}
for _, path := range repos {
commits = fillCommits(email, path, commits)
}
return commits
}
// func stats calculates and prints the stats.
func stats(email string){
commits := processRepositories(email)
printCommitStats(commits)
}