-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (128 loc) · 3.43 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"flag"
"fmt"
"math"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
type Cycle struct {
issue string
end time.Time
duration time.Duration
}
func printCycleTimes(path string, authorExclude regexp.Regexp, days int, github bool) {
repo, err := git.PlainOpen(path)
if err != nil {
fmt.Printf("Not a git repo: %s\n", path)
os.Exit(1)
}
commits, err := repo.Log(&git.LogOptions{Order: git.LogOrderCommitterTime})
if err != nil {
fmt.Printf("Failed to load git log:\n%s\n", err)
os.Exit(1)
}
cycles := make(map[string]Cycle)
issueRegex, _ := regexp.Compile("#([0-9]+)")
commits.ForEach(func(c *object.Commit) error {
issue := issueRegex.Find([]byte(c.Message))
if issue == nil {
return nil
}
if authorExclude.Match([]byte(c.Author.Name)) {
return nil
}
if days > 0 {
timeOfCommit := c.Author.When
deltaDuration := time.Duration(days) * 24 * time.Hour
earliestOfInterest := time.Now().Add(-deltaDuration)
if timeOfCommit.Before(earliestOfInterest) {
return nil
}
}
existingCycle, cycleKnown := cycles[string(issue)]
var end time.Time
if cycleKnown {
end = existingCycle.end
} else {
end = c.Committer.When
}
cycles[string(issue)] = Cycle{
issue: string(issue),
end: end,
duration: end.Sub(c.Committer.When),
}
return nil
})
result := make([]Cycle, 0, len(cycles))
for _, value := range cycles {
result = append(result, value)
}
sort.Slice(result, func(i, j int) bool {
return result[i].end.Before(result[j].end)
})
for _, cycle := range result {
visualRepresentation := strings.Repeat("·", int(math.Ceil(cycle.duration.Hours()/8)))
if len(visualRepresentation) > 50 {
visualRepresentation = strings.Repeat("·", 48) + ">>"
}
issue := cycle.issue[1:]
if github {
ghCmd := exec.Command("gh")
ghCmd.Dir = path
var outb, errb bytes.Buffer
ghCmd.Stdout = &outb
ghCmd.Stderr = &errb
ghCmd.Args = []string{"gh", "issue", "view", "--json", "title", "-t", "{{.title}}", issue}
err := ghCmd.Run()
if err != nil {
fmt.Println(err)
fmt.Println(errb.String())
} else {
title := outb.String()
if len(title) > 60 {
title = fmt.Sprintf("%s...", title[:53])
}
issue = fmt.Sprintf("%-60s #%s", title, issue)
}
} else {
issue = fmt.Sprintf("#%s", issue)
}
fmt.Printf("%s %s\t%8.1f %s\n", cycle.end.Format(time.DateOnly), issue, cycle.duration.Hours(), visualRepresentation)
}
}
func main() {
excludeFlag := flag.String("exclude", "^$", "Exclude commits with authors that match this regex")
daysFlag := flag.Int("days", -1, "How many days to look back, -1 being infinity")
githubFlag := flag.Bool("gh", false, "Use gh cli to obtain issue titles")
flag.Usage = func() {
fmt.Print("Usage: cycletime [flags] [PATH]\n\n")
fmt.Print("Hours between first and last commit tagged with an issue number\n\n")
fmt.Print("PATH defaults to the current working directory\n\n")
flag.PrintDefaults()
}
flag.Parse()
authorExcludeRegex, err := regexp.Compile(*excludeFlag)
if err != nil {
fmt.Println("Invalid regex")
fmt.Println(err)
flag.Usage()
os.Exit(1)
}
path := flag.Arg(0)
if path == "" {
path, err = os.Getwd()
if err != nil {
fmt.Println("Can't get current working directory")
os.Exit(1)
}
}
printCycleTimes(path, *authorExcludeRegex, *daysFlag, *githubFlag)
}