-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
227 lines (183 loc) · 4.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"sync"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type ConcData struct {
mu sync.Mutex
v map[string]*IntIntPair
l map[string][]StringIntIntTriplet
f int
}
func print_help() {
fmt.Printf(`
ppeb's git language metrics generator!!!
Usage: ./ppebtrics [OPTIONS]
-h|--help Display this message and exit
-c|--config Specify the path to your config.yml
-o|--output Specify the output path of your svg
-d|--dry-run Dry run! List the repos to be cloned and analyzed
-s|--silent Don't output to stdout
`)
os.Exit(1)
}
func main() {
var config_path string
var dryRun = false
var silent = false
argsLen := len(os.Args)
if argsLen <= 1 {
fmt.Println("No arguments provided! --config is required to continue.")
print_help()
}
for i := 1; i < argsLen; i++ {
arg := os.Args[i]
switch arg {
case "-h", "--help":
print_help()
case "-c", "--config":
if argsLen > i+1 {
config_path = os.Args[i+1]
i++
}
case "-o", "--output":
if argsLen > i+1 {
outputPath = os.Args[i+1]
i++
}
case "-d", "--dry-run":
dryRun = true
case "-s", "--silent":
silent = true
default:
fmt.Printf("Unknown argument %s!\n", arg)
print_help()
}
}
if len(config_path) == 0 {
panic("Missing config argument, provide a config.yml with -c or --config")
}
if len(outputPath) == 0 {
fmt.Println("No output path specified! Defaulting to ./langs.svg")
outputPath = "./langs.svg"
}
log_init(silent)
defer log_close()
config_init(config_path)
if dryRun {
fmt.Println("The following repositories will be cloned and analyzed:")
for _, v := range reposToCheck {
fmt.Printf(" %s\n", v)
}
return
}
cursorY = log_get_cursor_pos()
cumulative := ConcData{
mu: sync.Mutex{},
v: map[string]*IntIntPair{},
l: map[string][]StringIntIntTriplet{},
f: 0,
}
var cancelChannel chan bool
closed := false
closeOnce := sync.OnceFunc(func() { close(cancelChannel); closed = true })
countRepo := func(workerID int, repos <-chan string, cancel <-chan bool, wg *sync.WaitGroup) {
defer wg.Done()
var lastRepo *Repo
defer func() {
if r := recover(); r != nil {
log(Critical, lastRepo, fmt.Sprintf("Panic caught in WorkerID %d: %s, exiting...\n%s", workerID, r, debug.Stack()))
if lastRepo != nil {
log(Info, lastRepo, fmt.Sprintf("Reverting to branch %s", lastRepo.LatestBranch))
lastRepo.checkout_branch(lastRepo.LatestBranch)
}
pstr := strings.Replace(fmt.Sprint(r), "\n", "", -1)
log_progress(lastRepo, fmt.Sprintf("Panic caught, %s, exiting...", pstr), -1)
closeOnce()
}
}()
REPOSLOOP:
for id := range repos {
select {
case _, ok := <-cancel:
if !ok {
log_progress(lastRepo, "Exited", -1)
break REPOSLOOP
}
default:
log(Info, nil, fmt.Sprintf("WorkerID %d: preparing to initialize repo %s", workerID, id))
repo := Repo{
Identifier: id,
}
lastRepo = &repo
repo_initialize(&repo)
if len(repo.LatestCommit.Hash) == 0 {
continue
}
var counts map[string]*IntIntPair
if config.Indepth {
counts = repo.repo_count_by_commit()
} else {
counts = repo.repo_count()
}
cumulative.mu.Lock()
for k, v := range counts {
if cumulative.v[k] == nil {
cumulative.v[k] = &IntIntPair{}
}
cumulative.v[k].lines += v.lines
cumulative.v[k].bytes += v.bytes
if cumulative.l[k] == nil {
cumulative.l[k] = []StringIntIntTriplet{}
}
cumulative.l[k] = append(cumulative.l[k], StringIntIntTriplet{
lang: repo.Identifier,
lines: v.lines,
bytes: v.bytes,
})
}
cumulative.f += len(repo.UniqueFiles)
cumulative.mu.Unlock()
}
}
}
repoChannel := make(chan string, len(reposToCheck))
cancelChannel = make(chan bool)
var wg sync.WaitGroup
for i := 0; i < int(config.Parallel); i++ {
wg.Add(1)
go countRepo(i, repoChannel, cancelChannel, &wg)
}
for _, id := range reposToCheck {
repoChannel <- id
}
close(repoChannel)
wg.Wait()
log_reset_term_if_needed()
if closed {
os.Exit(1)
}
create_svg(cumulative.v, cumulative.f)
for k, v := range cumulative.l {
totals := cumulative.v[k]
lines := 0
bytes := 0
if totals != nil {
lines = totals.lines
bytes = totals.bytes
}
msg := fmt.Sprintf("Language %s: %d lines, %d bytes\n", k, lines, bytes)
for _, triplet := range v {
msg += fmt.Sprintf("ID: %s, Lines: %d, Bytes: %d\n", triplet.lang, triplet.lines, triplet.bytes)
}
log(Info, nil, msg)
}
}