-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_features.go
46 lines (41 loc) · 1.07 KB
/
count_features.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
package publisher
import (
"github.com/libgit2/git2go/v33"
"github.com/sabhiram/go-gitignore"
"strings"
)
// CountFeatures counts how many lines of code, and how many files there are.
func CountFeatures(repo *git.Repository, tree *git.Tree, exclude *ignore.GitIgnore, include *ignore.GitIgnore, countLines bool) (int, int, error) {
var loc int
if countLines {
loc = 0
} else {
loc = -1
}
files := 0
err := tree.Walk(func(name string, entry *git.TreeEntry) error {
isFile := entry.Filemode&git.FilemodeBlob != 0
path := strings.Join([]string{name, entry.Name}, "")
if isFile && fileIncluded(exclude, include, path) {
files += 1
if countLines {
blob, err := repo.LookupBlob(entry.Id)
if err != nil {
return err
}
contents := string(blob.Contents())
loc += lineCount(contents)
}
}
return nil
})
return loc, files, err
}
// https://stackoverflow.com/questions/47240127/fastest-way-to-find-number-of-lines-in-go
func lineCount(s string) int {
n := strings.Count(s, "\n")
if len(s) > 0 && !strings.HasSuffix(s, "\n") {
n++
}
return n
}