@@ -3,11 +3,13 @@ package statloc
33import (
44 _ "embed"
55 "errors"
6+ "os"
67 "path/filepath"
8+ "sync"
79
810 "github.com/statloc/core/internal/mapping"
911 "github.com/statloc/core/internal/matching"
10- "github.com/statloc/core/internal/tree"
12+ t "github.com/statloc/core/internal/tree"
1113)
1214
1315var (
2123func GetStatistics (path string ) (statistics Statistics , err error ) {
2224 mapping .Load (rawComponentsMapping , rawLanguagesMapping )
2325
26+ workdir , _ := os .Getwd ()
27+ tree := t.Tree {WorkDir : workdir }
28+
2429 list , err := tree .List (path )
2530
26- var treePathError * tree .PathError
31+ var treePathError * t .PathError
2732 if errors .As (err , & treePathError ) {
2833 err = & PathError {Path : path }
2934 return
@@ -37,14 +42,20 @@ func GetStatistics(path string) (statistics Statistics, err error) {
3742
3843 err = nil
3944
40- componentsSet := & componentSet {
45+ componentsSet := componentSet {
4146 Elements : make (map [string ]struct {}),
4247 Tail : nil ,
4348 }
4449
50+ var waitGroup sync.WaitGroup
51+ var mutex sync.Mutex
52+
53+
54+ waitGroup .Add (1 )
4555 tree .Chdir (path ) //nolint:errcheck
46- goAroundCalculating (list , & statistics , nil , componentsSet )
56+ goAroundCalculating (& waitGroup , & mutex , tree , list , & statistics , componentsSet )
4757 tree .Chdir (".." ) //nolint:errcheck
58+ waitGroup .Wait ()
4859
4960 cleanStatistics (statistics .Languages )
5061 cleanStatistics (statistics .Components )
@@ -53,24 +64,28 @@ func GetStatistics(path string) (statistics Statistics, err error) {
5364}
5465
5566func goAroundCalculating (
56- list tree.Nodes ,
67+ waitGroup * sync.WaitGroup ,
68+ mutex * sync.Mutex ,
69+ tree t.Tree ,
70+ list t.Nodes ,
5771 statistics * Statistics ,
58- tailComponent * component ,
59- componentsSet * componentSet ,
72+ componentsSet componentSet ,
6073) {
74+ defer waitGroup .Done ()
6175 for _ , node := range list {
6276 if node .IsDir {
6377 newComponentTitle , exists := matching .FindMatch (filepath .Base (node .Name ), mapping .Components )
6478
6579 exists = exists && ! componentsSet .In (newComponentTitle )
6680 if exists {
67- tailComponent = componentsSet .Add (newComponentTitle )
81+ componentsSet . Tail = componentsSet .Add (newComponentTitle )
6882 }
6983
70- list , _ = tree .List (node .Name )
84+ newList , _ : = tree .List (node .Name )
7185
86+ waitGroup .Add (1 )
7287 tree .Chdir (node .Name ) //nolint:errcheck
73- goAroundCalculating (list , statistics , tailComponent , componentsSet )
88+ go goAroundCalculating (waitGroup , mutex , tree . Copy (), newList , statistics , componentsSet . Copy () )
7489 tree .Chdir (".." ) //nolint:errcheck
7590
7691 if exists {
@@ -83,6 +98,7 @@ func goAroundCalculating(
8398 LOC := uint64 (1 )
8499 tree .ReadNodeLineByLine (node .Name , proceedLine , & LOC )
85100
101+ mutex .Lock ()
86102 statistics .Total .Append (LOC , 1 )
87103 statistics .Languages [language ].Append (LOC , 1 )
88104
@@ -95,6 +111,7 @@ func goAroundCalculating(
95111 for componentTitle := range componentsSet .Elements {
96112 statistics .Components [componentTitle ].Append (LOC , 1 )
97113 }
114+ mutex .Unlock ()
98115 }
99116 }
100117 }
0 commit comments