-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumper.go
93 lines (75 loc) · 1.58 KB
/
dumper.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"os"
"time"
"github.com/schollz/progressbar/v2"
)
type entry struct {
commit string
timestamp int
data string
}
func DumpHistory(directory string, command string, output io.Writer, limit int, after string, before string) error {
var err error
if command == "" {
flag.PrintDefaults()
return fmt.Errorf("No command given\n")
}
if directory != "" {
err = os.Chdir(directory)
if err != nil {
return fmt.Errorf("Invalid directory: %s\n", directory)
}
}
// make sure we leave a clean state afterwards
branch, err := execute("git", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return err
}
// switch back to original branch
defer execute("git", "checkout", branch)
commits, err := getCommits(limit, after, before)
if err != nil {
return err
}
bar := progressbar.NewOptions(len(commits), progressbar.OptionSetWriter(os.Stderr))
result := make([]entry, 0, len(commits))
for _, commit := range commits {
bar.Add(1)
entity, err := evaluate(commit, command)
if err != nil {
log.Print(err)
continue
}
result = append(result, entity)
}
return writeCsv(result, output)
}
func writeCsv(logs []entry, file io.Writer) error {
writer := csv.NewWriter(file)
err := writer.Write([]string{
"time",
"result",
"commit",
})
if err != nil {
return err
}
for _, row := range logs {
err := writer.Write([]string{
time.Unix(int64(row.timestamp), 0).Format(time.RFC3339),
row.data,
row.commit,
})
if err != nil {
return err
}
}
writer.Flush()
return nil
}