-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
55 lines (46 loc) · 1.08 KB
/
git.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func getCommits(limit int, after string, before string) ([]entry, error) {
args := []string{
"log",
"--reverse",
"--pretty=\"%h %ct\"",
}
if after != "" {
args = append(args, fmt.Sprintf("--after='%s'", after))
}
if before != "" {
args = append(args, fmt.Sprintf("--before='%s'", before))
}
stdout, err := execute("git", args...)
commits := make([]entry, 0)
if err != nil {
return commits, err
}
lines := strings.Split(stdout, "\n")
stepSize := (len(lines) / limit) - 1
if stepSize == 0 {
stepSize = 1
}
for i, line := range lines {
if line == "" {
continue
}
if i%stepSize != 0 && i != len(lines)-1 { // include every X once + the last one for sure
continue
}
parts := strings.Split(strings.ReplaceAll(line, `"`, ""), " ")
timestamp, _ := strconv.Atoi(parts[1])
commits = append(commits, entry{
commit: parts[0],
timestamp: timestamp,
})
}
fmt.Fprintf(os.Stderr, "Commits: %d, Step size: %d (%d commits to check)\n", len(lines), stepSize, len(commits))
return commits, nil
}