-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepoharvester.go
More file actions
1264 lines (1143 loc) · 39.3 KB
/
repoharvester.go
File metadata and controls
1264 lines (1143 loc) · 39.3 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"golang.org/x/sync/semaphore"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"text/tabwriter"
"time"
)
type Repo struct {
Name string
Clone_url string
Size uint64
Fork bool
local_path string // This will not be used by json to decode
}
type EmailContext struct {
Repo *Repo
EmailAddress string
Role int8
}
const (
ROLE_AUTHOR int8 = 1 << iota
ROLE_COMMITTER int8 = 1 << iota
ROLE_NAME_COMMITTER string = "Committer"
ROLE_NAME_AUTHOR string = "Author"
ROLE_NAME_BOTH string = "Author+Committer"
ROLE_MASK_BOTH int8 = ROLE_AUTHOR | ROLE_COMMITTER
)
type EmailGroupByRepoKey struct {
Email string
Repo *Repo
}
type FmtEmailPerRepo struct {
RepoUrl string
Emails map[string]string
}
type FmtRepoPerEmail struct {
RepoName string
Role string
RepoUrl string
}
var active_data []uint32
var error_data []uint32
var completion_data []uint32
var total_data []uint32
const (
GITHUB_FETCH int8 = 0
GITHUB_TOTAL_PAGES int8 = 0
GITHUB_PARSE int8 = 1
REMOTE_REPOS int8 = 1
GIT_OPS_CLONE int8 = 2
LOCAL_REPOS int8 = 2
GIT_OPS_LOG int8 = 3
GIT_IDENTITIES int8 = 3
EMAILS_DEDUP int8 = 4
EMAILS_GROUPED int8 = 5
)
const DEFAULT_SIZE_FILTER int = 1000000
var (
LINE_SEP string
g_buff_pool sync.Pool
g_semaphore *semaphore.Weighted
BUFFER_SIZE int
)
// Logging code
const (
LOG_FATAL uint8 = 0
LOG_ERROR uint8 = 1
LOG_INFO uint8 = 2
LOG_DEBUG uint8 = 3
)
type Logger struct {
Panic func(...interface{})
Fatal func(...interface{})
Error func(...interface{})
Errorf func(string, ...interface{})
ErrorFunc func(func() (string, bool))
Info func(...interface{})
Infof func(string, ...interface{})
InfoFunc func(func() (string, bool))
Debug func(...interface{})
Debugf func(string, ...interface{})
DebugFunc func(func() (string, bool))
wg sync.WaitGroup
log_level uint8
}
func (logger *Logger) set_level(level uint8) bool {
if level > 3 {
return false
}
logger.log_level = level
logger.Fatal = noop
logger.Error = noop
logger.Errorf = noopf
logger.ErrorFunc = noopfunc
logger.Info = noop
logger.Infof = noopf
logger.InfoFunc = noopfunc
logger.Debug = noop
logger.Debugf = noopf
logger.DebugFunc = noopfunc
switch level {
case LOG_DEBUG:
logger.Debug = logger.log_debug
logger.Debugf = logger.logf_debug
logger.DebugFunc = logger.logfunc_debug
fallthrough
case LOG_INFO:
logger.Info = logger.log_info
logger.Infof = logger.logf_info
logger.InfoFunc = logger.logfunc_info
fallthrough
case LOG_ERROR:
logger.Error = logger.log_error
logger.Errorf = logger.logf_error
logger.ErrorFunc = logger.logfunc_error
fallthrough
case LOG_FATAL:
logger.Fatal = logger.log_fatal
logger.Panic = logger.log_panic
}
return true
}
func (logger *Logger) LogLevel() uint8 {
return logger.log_level
}
// Do the main logging functions in a goroutine to ensure they don't slow down the main operation
func (logger *Logger) log_debug(a ...interface{}) {
logger.wg.Add(1)
go func() {
msg := fmt.Sprint(a...)
log("DEBUG", &msg)
logger.wg.Done()
}()
}
func (logger *Logger) logf_debug(format string, a ...interface{}) {
logger.wg.Add(1)
go func() {
msg := fmt.Sprintf(format, a...)
log("DEBUG", &msg)
logger.wg.Done()
}()
}
func (logger *Logger) logfunc_debug(logfunc func() (string, bool)) {
logger.wg.Add(1)
go func() {
msg, ok := logfunc()
if ok {
log("DEBUG", &msg)
}
logger.wg.Done()
}()
}
func (logger *Logger) log_info(a ...interface{}) {
logger.wg.Add(1)
go func() {
msg := fmt.Sprint(a...)
log("INFO", &msg)
logger.wg.Done()
}()
}
func (logger *Logger) logf_info(format string, a ...interface{}) {
logger.wg.Add(1)
go func() {
msg := fmt.Sprintf(format, a...)
log("INFO", &msg)
logger.wg.Done()
}()
}
func (logger *Logger) logfunc_info(logfunc func() (string, bool)) {
logger.wg.Add(1)
go func() {
msg, ok := logfunc()
if ok {
log("INFO", &msg)
}
logger.wg.Done()
}()
}
func (logger *Logger) log_error(a ...interface{}) {
logger.wg.Add(1)
go func() {
msg := fmt.Sprint(a...)
log("ERROR", &msg)
logger.wg.Done()
}()
}
func (logger *Logger) logf_error(format string, a ...interface{}) {
logger.wg.Add(1)
go func() {
msg := fmt.Sprintf(format, a...)
log("ERROR", &msg)
logger.wg.Done()
}()
}
func (logger *Logger) logfunc_error(logfunc func() (string, bool)) {
logger.wg.Add(1)
go func() {
msg, ok := logfunc()
if ok {
log("ERROR", &msg)
}
logger.wg.Done()
}()
}
// These two functions terminate execution so they don't run async
func (logger *Logger) log_fatal(a ...interface{}) {
msg := fmt.Sprint(a...)
log("FATAL", &msg)
os.Exit(1)
}
func (logger *Logger) log_panic(a ...interface{}) {
msg := fmt.Sprint(a...)
log("PANIC", &msg)
panic(msg)
}
func log(level string, msg *string) {
out := g_buff_pool.Get().(*bytes.Buffer)
out.Reset()
out.WriteString(level)
out.WriteString(": ")
out.WriteString(*msg)
out.WriteString(LINE_SEP)
out.WriteTo(os.Stderr)
//fmt.Fprintf(os.Stderr, "%s: %s%s", level, *msg, LINE_SEP)
g_buff_pool.Put(out)
}
func noop(a ...interface{}) {
return
}
func noopf(format string, a ...interface{}) {
return
}
func noopfunc(logfunc func() (string, bool)) {
return
}
// Wait for any writers to return
// Kinda like a Sync()
func (logger *Logger) Wait() {
logger.wg.Wait()
}
var logger Logger
// End logging functions
type ResourceOptions struct {
Type string `short:"t" long:"type" description:"type of object to target" choice:"user" choice:"org" choice:"url"`
Org bool `short:"o" long:"org" description:"alias to --type org" group:"parse-type"`
User bool `short:"u" long:"user" description:"alias to --type user" group:"parse-type"`
Url bool `long:"url" description:"alias to --type url" group:"parse-type"`
SizeFilter uint64 `long:"size-filter" value-name:"<size in kB>" description:"repo size to filter (set 0 to disable)" default:"1000000" long-description:"There are often repos that are asset heavy and increase the faceprint time without a lot of gain. This filters those out."`
ForkFilter bool `long:"no-fork" description:"filter out forked repos"`
}
type OutputOptions struct {
OutputJson flags.Filename `short:"j" long:"json" description:"Output JSON file" value-name:"output.json" required:"true"`
OutputFile flags.Filename `short:"f" long:"file" description:"Output flat file" value-name:"output.list" required:"true"`
}
type Positional struct {
TargetName string `positional-arg-name:"target-name" description:"The name of the user or org to faceprint"`
}
type ApplicationOptions struct {
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Quiet bool `short:"q" long:"quiet" description:"Show fewer messages"`
PreserveDir bool `long:"preserve-dir" description:"preserve working directory"`
WorkingDir flags.Filename `short:"w" long:"working-dir" value-name:"<path_to_working_dir>" default:"!None-Provided!" default-mask:"Uses working directory" description:"working dir path (should have space to store all repos)"`
GitPath flags.Filename `long:"git-path" short:"g" description:"path to git" value-name:"<path_to_git>" default:"!None-Provided!" default-mask:"Uses system git"`
}
type AdvancedOptions struct {
Workers int8 `long:"workers" description:"numbers of workers to use" default:"20" value-name:"<int>"`
QueueSize int `long:"queue-size" description:"base size of the operating queue" default:"20" value-name:"<int>"`
}
var opts struct {
//Name string `name:"name" description:"The name of the user or org to faceprint" positional-args:"yes" required:"yes"`
Args Positional `positional-args:"yes" required:"yes"`
Resource ResourceOptions `group:"Resource Options (Required)"`
Output OutputOptions `group:"Output Options (Required)"`
Application ApplicationOptions `group:"Application Options"`
Advanced AdvancedOptions `group:"Advanced Options"`
}
var parser = flags.NewParser(&opts, flags.Default)
func check_working_dir(working_dir string) (bool, error) {
err := os.MkdirAll(working_dir, 0700)
if err != nil {
return false, err
}
f, err := os.Open(working_dir)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdir(1)
if err != nil {
if err == io.EOF {
return true, nil
}
return false, err
}
return false, nil
}
func check_ouput_location(file string) (bool, error) {
_, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return false, err
}
return true, nil
}
func get_next_link(http_header map[string][]string, next_url *string) bool {
val, ok := http_header["Link"]
if ok {
link_header := strings.Split(val[0], ", ")
for _, value := range link_header {
if strings.HasSuffix(value, `"next"`) {
*next_url = strings.Split(value, "; ")[0]
*next_url = (*next_url)[1 : len(*next_url)-1]
return true
}
}
}
return false
}
func get_total_pages(http_header map[string][]string, total_pages *uint32) {
// if we already set the total_pages -- we don't need to do it again
if *total_pages > 0 {
return
}
val, ok := http_header["Link"]
if ok {
link_header := strings.Split(val[0], ", ")
for _, value := range link_header {
if strings.HasSuffix(value, `"last"`) {
url := strings.Split(value, "; ")[0]
pages, err := strconv.ParseUint(url[len(url)-2:len(url)-1], 10, 32)
if err != nil {
panic(err)
}
*total_pages = uint32(pages)
return
}
}
}
// If there is no Link header, then we only have one page
*total_pages = 1
return
}
func get_repos_from_github(ctx context.Context, url string) chan io.ReadCloser {
func_logging_name := "Stage 1 - Get Github Repos"
bodies := make(chan io.ReadCloser, BUFFER_SIZE)
c := &http.Client{}
urls := make(chan string, BUFFER_SIZE)
urls <- url
go func() {
defer close(urls)
defer close(bodies)
err := g_semaphore.Acquire(ctx, 1)
// If we get an error back, it means the context is done
if err != nil {
return
}
defer g_semaphore.Release(1)
infoLogger := func() (string, bool) {
active := atomic.LoadUint32(&active_data[GITHUB_FETCH])
completed := atomic.LoadUint32(&completion_data[GITHUB_FETCH])
total := atomic.LoadUint32(&total_data[GITHUB_TOTAL_PAGES])
if active+completed+total > 0 {
var out strings.Builder
out.WriteString(func_logging_name)
out.WriteString(": Processed ")
out.WriteString(strconv.FormatUint(uint64(completed), 10))
out.WriteString(" of a total ")
out.WriteString(strconv.FormatUint(uint64(total), 10))
out.WriteString(" pages. Active requests: ")
out.WriteString(strconv.FormatUint(uint64(active), 10))
out.WriteString(".")
return out.String(), true
}
return "", false
}
var next_url string
var total_pages uint32 = 0
for {
logger.DebugFunc(infoLogger)
select {
// select read from urls
case <-ctx.Done():
return
case url := <-urls:
atomic.AddUint32(&active_data[GITHUB_FETCH], 1)
var fetch_counter int8 = 1
//run the req with the context to cancel if needed
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
logger.Error(func_logging_name, ": Error setting up request. Error: ", err)
atomic.AddUint32(&error_data[GITHUB_FETCH], 1)
atomic.AddUint32(&active_data[GITHUB_FETCH], ^uint32(0))
return
}
for {
resp, err := c.Do(req)
if err != nil {
if fetch_counter >= 4 {
logger.Errorf("%s: Error fetching %s. Error:%v", func_logging_name, url, err)
atomic.AddUint32(&error_data[GITHUB_FETCH], 1)
atomic.AddUint32(&active_data[GITHUB_FETCH], ^uint32(0))
return
} else {
logger.Debugf("%s: Attempt #%d. URL: %s. Error: %v", func_logging_name, fetch_counter, url, err)
fetch_counter++
continue
}
}
get_total_pages(resp.Header, &total_pages)
atomic.StoreUint32(&total_data[GITHUB_TOTAL_PAGES], total_pages)
atomic.AddUint32(&completion_data[GITHUB_FETCH], 1)
atomic.AddUint32(&active_data[GITHUB_FETCH], ^uint32(0))
// select write to bodies
select {
case <-ctx.Done():
return
case bodies <- resp.Body:
}
ok := get_next_link(resp.Header, &next_url)
if ok {
// This should never block
urls <- next_url
} else {
logger.Info(func_logging_name, ": Completed. Pages pulled: ", total_pages, ". Error count: ", atomic.LoadUint32(&error_data[GITHUB_FETCH]))
return
}
break
}
}
}
}()
return bodies
}
func parse_github_response(ctx context.Context, repo_data chan io.ReadCloser, fork_filter bool) chan Repo {
func_logging_name := "Stage 2 - Parse URLs"
repos := make(chan Repo, BUFFER_SIZE)
go func() {
var wg sync.WaitGroup
infoLogger := func() (string, bool) {
active := atomic.LoadUint32(&active_data[GITHUB_PARSE])
completed := atomic.LoadUint32(&completion_data[GITHUB_PARSE])
total := atomic.LoadUint32(&total_data[GITHUB_TOTAL_PAGES])
if active+completed+total > 0 {
var out strings.Builder
out.WriteString(func_logging_name)
out.WriteString(": Processed ")
out.WriteString(strconv.FormatUint(uint64(completed), 10))
out.WriteString(" of a total ")
out.WriteString(strconv.FormatUint(uint64(total), 10))
out.WriteString(" pages. Active pages: ")
out.WriteString(strconv.FormatUint(uint64(active), 10))
out.WriteString(".")
return out.String(), true
}
return "", false
}
for {
logger.DebugFunc(infoLogger)
select {
case <-ctx.Done():
wg.Wait()
close(repos)
return
case body, ok := <-repo_data:
if !ok {
logger.Debug(func_logging_name, ": cleared queue of size: ", atomic.LoadUint32(&total_data[GITHUB_TOTAL_PAGES]), " - in-flight actions: ", atomic.LoadUint32(&active_data[GITHUB_PARSE]))
wg.Wait()
close(repos)
logger.Info(func_logging_name, ": Completed. Total Pages Parsed: ", atomic.LoadUint32(&completion_data[GITHUB_PARSE]), ". Work Items Created: ", atomic.LoadUint32(&total_data[REMOTE_REPOS]), ". Error count: ", atomic.LoadUint32(&error_data[GITHUB_PARSE]))
return
}
err := g_semaphore.Acquire(ctx, 1)
// If we get an error back, it means the context is done
if err != nil {
wg.Wait()
close(repos)
return
}
wg.Add(1)
atomic.AddUint32(&active_data[GITHUB_PARSE], 1)
go func(body io.ReadCloser) {
defer wg.Done()
defer g_semaphore.Release(1)
dec := json.NewDecoder(body)
for {
var r []Repo
if err := dec.Decode(&r); err == io.EOF {
body.Close()
break
} else if err != nil {
logger.Error(func_logging_name, ": Error parsing body. Error: ", err)
atomic.AddUint32(&error_data[GITHUB_PARSE], 1)
atomic.AddUint32(&active_data[GITHUB_PARSE], ^uint32(0))
// Not sure if I should continue or just exit.
// Exiting for now, can change it to continue
return
}
for _, repo := range r {
if repo.Fork && fork_filter {
logger.Debug(func_logging_name, ": Skipping ", repo.Name, " based on the fork filter.")
continue
}
select {
case <-ctx.Done():
return
case repos <- repo:
atomic.AddUint32(&total_data[REMOTE_REPOS], 1)
}
}
atomic.AddUint32(&completion_data[GITHUB_PARSE], 1)
atomic.AddUint32(&active_data[GITHUB_PARSE], ^uint32(0))
}
}(body)
}
}
wg.Wait()
close(repos)
}()
return repos
}
func git_ops_clone(ctx context.Context, repos chan Repo, git_path *string, working_dir *string, size_filter uint64) chan Repo {
local_repos := make(chan Repo, BUFFER_SIZE)
func_logging_name := "Stage 3 - Clone Repos"
go func() {
var wg sync.WaitGroup
infoLogger := func() (string, bool) {
active := atomic.LoadUint32(&active_data[GIT_OPS_CLONE])
completed := atomic.LoadUint32(&completion_data[GIT_OPS_CLONE])
total := atomic.LoadUint32(&total_data[REMOTE_REPOS])
if active+completed+total > 0 && completed%3 == 0 {
var out strings.Builder
out.WriteString(func_logging_name)
out.WriteString(": Cloned ")
out.WriteString(strconv.FormatUint(uint64(completed), 10))
out.WriteString(" of a total ")
out.WriteString(strconv.FormatUint(uint64(total), 10))
out.WriteString(" repos. Active clones: ")
out.WriteString(strconv.FormatUint(uint64(active), 10))
out.WriteString(".")
return out.String(), true
}
return "", false
}
for {
logger.DebugFunc(infoLogger)
select {
case <-ctx.Done():
wg.Wait()
close(local_repos)
return
case repo, ok := <-repos:
if !ok {
logger.Debug(func_logging_name, ": cleared queue of size: ", atomic.LoadUint32(&total_data[REMOTE_REPOS]), " - in-flight actions: ", atomic.LoadUint32(&active_data[GIT_OPS_CLONE]))
wg.Wait()
close(local_repos)
logger.Info(func_logging_name, ": Completed. Total repos cloned: ", atomic.LoadUint32(&completion_data[GIT_OPS_CLONE]), ". Work Items Created: ", atomic.LoadUint32(&total_data[LOCAL_REPOS]), ". Error count: ", atomic.LoadUint32(&error_data[GIT_OPS_CLONE]))
return
}
if size_filter > 0 && repo.Size > size_filter {
atomic.AddUint32(&completion_data[GIT_OPS_CLONE], 1)
logger.Infof("%s: Skipping %s of size %d based on filter %d.", func_logging_name, repo.Name, repo.Size, size_filter)
continue
}
err := g_semaphore.Acquire(ctx, 1)
if err != nil {
wg.Wait()
close(local_repos)
return
}
wg.Add(1)
atomic.AddUint32(&active_data[GIT_OPS_CLONE], 1)
go func() {
defer wg.Done()
defer g_semaphore.Release(1)
cmd := exec.CommandContext(ctx, *git_path, "clone", "-n", "-q", "--filter=tree:0", repo.Clone_url)
cmd.Dir = *working_dir
std_err := g_buff_pool.Get().(*bytes.Buffer)
std_err.Reset()
defer g_buff_pool.Put(std_err)
cmd.Stderr = std_err
err := cmd.Run()
if err != nil {
switch err_defined := err.(type) {
case *exec.ExitError:
// Probably a context kill
if !err_defined.ProcessState.Exited() && err_defined.ProcessState.ExitCode() == -1 {
// Really probably an ctx kill so we'll make this log level info
logger.Debug(func_logging_name, ": ", repo.Name, " killed by application interrupt. Error: ", err, ". Error from application: ", std_err.String())
repo.local_path = filepath.Join(*working_dir, repo.Name)
atomic.AddUint32(&error_data[GIT_OPS_CLONE], 1)
atomic.AddUint32(&active_data[GIT_OPS_CLONE], ^uint32(0))
return
}
// otherwise, things are probably bad. This will be log level error
logger.Error(func_logging_name, ": Got an error. Repo Name: ", repo.Name, " - golang err: ", err, ". Error from command: ", std_err.String())
repo.local_path = filepath.Join(*working_dir, repo.Name)
atomic.AddUint32(&error_data[GIT_OPS_CLONE], 1)
atomic.AddUint32(&active_data[GIT_OPS_CLONE], ^uint32(0))
return
default:
// All other cases are log level error
logger.Error(func_logging_name, ": Got an error. Repo Name: ", repo.Name, " - golang err: ", err, ". Error from command: ", std_err.String())
repo.local_path = filepath.Join(*working_dir, repo.Name)
atomic.AddUint32(&error_data[GIT_OPS_CLONE], 1)
atomic.AddUint32(&active_data[GIT_OPS_CLONE], ^uint32(0))
return
}
}
repo.local_path = filepath.Join(*working_dir, repo.Name)
select {
case <-ctx.Done():
return
case local_repos <- repo:
atomic.AddUint32(&total_data[LOCAL_REPOS], 2)
atomic.AddUint32(&completion_data[GIT_OPS_CLONE], 1)
atomic.AddUint32(&active_data[GIT_OPS_CLONE], ^uint32(0))
return
}
}()
}
}
}()
return local_repos
}
func git_ops_shortlog(ctx context.Context, local_repos chan Repo, git_path *string) (chan string, chan EmailContext) {
emails := make(chan string, BUFFER_SIZE)
context_emails := make(chan EmailContext, BUFFER_SIZE)
func_logging_name := "Stage 4 - Find Emails"
go func() {
var wg sync.WaitGroup
l_semaphore := semaphore.NewWeighted(2)
var sem *semaphore.Weighted
params_containers := map[int8][]string{ROLE_AUTHOR: []string{"--no-pager", "shortlog", "--all", "-n", "-e", "-s"}, ROLE_COMMITTER: []string{"--no-pager", "shortlog", "--all", "-n", "-e", "-s", "-c"}}
infoLogger := func() (string, bool) {
active := atomic.LoadUint32(&active_data[GIT_OPS_LOG])
completed := atomic.LoadUint32(&completion_data[GIT_OPS_LOG])
total := atomic.LoadUint32(&total_data[LOCAL_REPOS])
if active+completed+total > 0 && completed%3 == 0 {
var out strings.Builder
out.WriteString(func_logging_name)
out.WriteString(": Processsed ")
out.WriteString(strconv.FormatUint(uint64(completed), 10))
out.WriteString(" of a total ")
out.WriteString(strconv.FormatUint(uint64(total), 10))
out.WriteString(" repos. Active shortlogs: ")
out.WriteString(strconv.FormatUint(uint64(active), 10))
out.WriteString(".")
return out.String(), true
}
return "", false
}
for {
logger.DebugFunc(infoLogger)
select {
case <-ctx.Done():
wg.Wait()
close(emails)
close(context_emails)
return
case repo, ok := <-local_repos:
if !ok {
logger.Debug(func_logging_name, ": completed queue of size: ", atomic.LoadUint32(&total_data[LOCAL_REPOS]), " - in-flight actions: ", atomic.LoadUint32(&active_data[GIT_OPS_LOG]))
wg.Wait()
close(emails)
close(context_emails)
logger.Info(func_logging_name, ": Completed. Total repos processed: ", atomic.LoadUint32(&completion_data[GIT_OPS_LOG]), ". Work Items Created: ", atomic.LoadUint32(&total_data[GIT_IDENTITIES]), ". Error count: ", atomic.LoadUint32(&error_data[GIT_OPS_LOG]))
return
}
for role, params := range params_containers {
if !l_semaphore.TryAcquire(1) {
err := g_semaphore.Acquire(ctx, 1)
if err != nil {
wg.Wait()
close(emails)
close(context_emails)
return
}
sem = g_semaphore
} else {
sem = l_semaphore
}
wg.Add(1)
atomic.AddUint32(&active_data[GIT_OPS_LOG], 1)
go func(params []string, role int8, sem *semaphore.Weighted) {
defer wg.Done()
defer sem.Release(1)
//author_cmd := exec.CommandContext(ctx, *git_path, "--no-pager", "shortlog", "--all", "-n", "-e", "-s")
//commiter_cmd := exec.CommandContext(ctx, *git_path, "shortlog", "--all", "-n", "-e", "-s", "-c")
cmd := exec.CommandContext(ctx, *git_path, params...)
cmd.Dir = repo.local_path
std_out := g_buff_pool.Get().(*bytes.Buffer)
std_out.Reset()
defer g_buff_pool.Put(std_out)
cmd.Stdout = std_out
std_err := g_buff_pool.Get().(*bytes.Buffer)
std_err.Reset()
defer g_buff_pool.Put(std_err)
cmd.Stderr = std_err
err := cmd.Run()
if err != nil {
switch err_defined := err.(type) {
case *exec.ExitError:
// Probably a context kill
if !err_defined.ProcessState.Exited() && err_defined.ProcessState.ExitCode() == -1 {
// Really probably an ctx kill so we'll make this log level info
logger.Debug(func_logging_name, ": ", repo.Name, " killed by interrupt. Error: ", err, ". Error from application: ", std_err.String())
atomic.AddUint32(&error_data[GIT_OPS_LOG], 1)
atomic.AddUint32(&active_data[GIT_OPS_LOG], ^uint32(0))
return
}
// otherwise, things are probably bad. This will be log level error
logger.Error(func_logging_name, ": Got an error. Repo Name: ", repo.Name, " - golang err: ", err, ". Error from command: ", std_err.String())
atomic.AddUint32(&error_data[GIT_OPS_LOG], 1)
atomic.AddUint32(&active_data[GIT_OPS_LOG], ^uint32(0))
return
default:
// All other cases are log level error
logger.Error(func_logging_name, ": Got an error. Repo Name: ", repo.Name, " - golang err: ", err, ". Error from command: ", std_err.String())
atomic.AddUint32(&error_data[GIT_OPS_LOG], 1)
atomic.AddUint32(&active_data[GIT_OPS_LOG], ^uint32(0))
return
}
}
scanner := bufio.NewScanner(std_out)
for scanner.Scan() {
full_author := scanner.Text()
email := full_author[strings.LastIndex(full_author, "<")+1 : len(full_author)-1]
select {
case <-ctx.Done():
return
case emails <- email:
//noop
}
select {
case <-ctx.Done():
return
case context_emails <- EmailContext{Repo: &repo, EmailAddress: email, Role: role}:
//noop
}
atomic.AddUint32(&total_data[GIT_IDENTITIES], 1)
}
if err = scanner.Err(); err != nil {
atomic.AddUint32(&error_data[GIT_OPS_LOG], 1)
atomic.AddUint32(&active_data[GIT_OPS_LOG], ^uint32(0))
logger.Error(func_logging_name, ": Error scanning text, error: ", err)
return
}
atomic.AddUint32(&completion_data[GIT_OPS_LOG], 1)
atomic.AddUint32(&active_data[GIT_OPS_LOG], ^uint32(0))
}(params, role, sem)
}
}
}
}()
return emails, context_emails
}
func emails_dedup(emails chan string) (map[string]uint, chan struct{}) {
emails_deduped := make(map[string]uint, 50)
done := make(chan struct{})
go func(emails_deduped map[string]uint) {
var emails_processed_count uint = 0
for email := range emails {
//fmt.Println("Processing email: ", email)
if _, ok := emails_deduped[email]; !ok {
emails_deduped[email] = 0
}
atomic.AddUint32(&completion_data[EMAILS_DEDUP], 1)
emails_processed_count++
}
close(done)
logger.Info("Stage 5a - Dedup Emails: Completed. Emails processed: ", emails_processed_count, ". Final email count: ", len(emails_deduped))
}(emails_deduped)
return emails_deduped, done
}
func emails_by_repo(contexts chan EmailContext) (map[EmailGroupByRepoKey]int8, chan struct{}) {
emails_grouped := make(map[EmailGroupByRepoKey]int8, 50)
done := make(chan struct{})
go func(emails_grouped map[EmailGroupByRepoKey]int8) {
var emails_processed_count uint = 0
for context := range contexts {
//fmt.Printf("Processing email: %s for %s\n", context.EmailAddress, context.Repo.Name)
emails_grouped[EmailGroupByRepoKey{Email: context.EmailAddress, Repo: context.Repo}] |= context.Role
atomic.AddUint32(&completion_data[EMAILS_GROUPED], 1)
emails_processed_count++
}
close(done)
logger.Info("Stage 5b - Emails per Repo: Completed. Emails processed: ", emails_processed_count, ". Final contextual info count: ", len(emails_grouped))
}(emails_grouped)
return emails_grouped, done
}
func create_output_file(output_file string, emails map[string]uint) error {
output_data := g_buff_pool.Get().(*bytes.Buffer)
output_data.Reset()
defer g_buff_pool.Put(output_data)
for key := range emails {
output_data.WriteString(key)
output_data.WriteString(LINE_SEP)
}
// Try to write three times before giving up
var write_counter int8 = 1
for {
// Write it as one block
err := ioutil.WriteFile(output_file, output_data.Bytes(), 0600)
if err != nil {
logger.Debug("Create Deduped File: Error writing file, attempt: ", write_counter, ". Error: ", err)
if write_counter > 3 {
return err
}
write_counter++
// Short sleep before retrying
time.Sleep(time.Millisecond * 100)
continue
}
break
}
return nil
}
func create_output_json(output_json string, emails_grouped map[EmailGroupByRepoKey]int8) error {
repos := make(map[string]FmtEmailPerRepo)
emails := make(map[string]map[string][]FmtRepoPerEmail)
role_reference := map[int8]string{ROLE_AUTHOR: ROLE_NAME_AUTHOR, ROLE_COMMITTER: ROLE_NAME_COMMITTER, ROLE_MASK_BOTH: ROLE_NAME_BOTH}
var domain string
for group_by_key, role_id := range emails_grouped {
if group_by_key.Email == "" {
group_by_key.Email = "!blank!"
domain = "!none!"
} else {
at_index := strings.LastIndex(group_by_key.Email, "@")
if at_index > 0 {
domain = string(group_by_key.Email[at_index+1:])
} else {
domain = "!none!"
}
}
if _, ok := repos[group_by_key.Repo.Name]; !ok {
repos[group_by_key.Repo.Name] = FmtEmailPerRepo{RepoUrl: group_by_key.Repo.Clone_url, Emails: map[string]string{}}
}
if _, ok := emails[domain]; !ok {
emails[domain] = make(map[string][]FmtRepoPerEmail)
}
if _, ok := emails[domain][group_by_key.Email]; !ok {
emails[domain][group_by_key.Email] = []FmtRepoPerEmail{}
}
emails[domain][group_by_key.Email] = append(emails[domain][group_by_key.Email], FmtRepoPerEmail{RepoName: group_by_key.Repo.Name, RepoUrl: group_by_key.Repo.Clone_url, Role: role_reference[role_id]})
repos[group_by_key.Repo.Name].Emails[group_by_key.Email] = role_reference[role_id]
}
output := make(map[string]interface{})
output["repos"] = repos
output["emails"] = emails
b, err := json.MarshalIndent(output, "", "\t")
if err != nil {
return err
}
var write_counter int8 = 1
for {
err = ioutil.WriteFile(output_json, b, 0600)
if err != nil {
logger.Debug("Create JSON: Error writing file, attempt: ", write_counter, ". Error: ", err)
if write_counter > 3 {
return err
}
write_counter++
time.Sleep(time.Millisecond * 100)
continue
}
break
}
return nil
}
func init() {
if runtime.GOOS == "windows" {
LINE_SEP = "\r\n"
} else {
LINE_SEP = "\n"
}
}
func init() {
args, err := parser.Parse()
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
}
if len(args) > 0 {
fmt.Fprintf(os.Stderr, "Unexpected arguments found: %s\n", strings.Join(args, " "))
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
if !opts.Resource.User && !opts.Resource.Org && !opts.Resource.Url && len(opts.Resource.Type) == 0 {
fmt.Fprintln(os.Stderr, "Please provide either org, user or url as the target type")
parser.WriteHelp(os.Stderr)
os.Exit(1)