Skip to content

Commit 8fe2417

Browse files
authored
Merge pull request #318 from k1LoW/fix-file-type
Update gocloc
2 parents 72e299e + 723efc7 commit 8fe2417

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/google/go-github/v50 v50.0.0
2222
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c
2323
github.com/hashicorp/go-multierror v1.1.1
24-
github.com/hhatto/gocloc v0.4.3
24+
github.com/hhatto/gocloc v0.5.2
2525
github.com/josharian/txtarfs v0.0.0-20210615234325-77aca6df5bca
2626
github.com/jszwec/s3fs v0.4.0
2727
github.com/k1LoW/duration v1.2.0

go.sum

+2-5
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5
178178
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
179179
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
180180
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
181-
github.com/go-enry/go-enry/v2 v2.8.0/go.mod h1:GVzIiAytiS5uT/QiuakK7TF1u4xDab87Y8V5EJRpsIQ=
182181
github.com/go-enry/go-enry/v2 v2.8.3 h1:BwvNrN58JqBJhyyVdZSl5QD3xoxEEGYUrRyPh31FGhw=
183182
github.com/go-enry/go-enry/v2 v2.8.3/go.mod h1:GVzIiAytiS5uT/QiuakK7TF1u4xDab87Y8V5EJRpsIQ=
184183
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
@@ -354,8 +353,8 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
354353
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
355354
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
356355
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
357-
github.com/hhatto/gocloc v0.4.3 h1:bWbEi+cOKDAvWwPsP2lT30638Bg37X+Ru00TG7adpGg=
358-
github.com/hhatto/gocloc v0.4.3/go.mod h1:EPoonh5stxIeraUU70Ogyj9yIpvV6Xirnjhyx+3/cHM=
356+
github.com/hhatto/gocloc v0.5.2 h1:wCPQZziiXBed1cbPH6sj23bWsgxiSgiujpSA2IGxoZA=
357+
github.com/hhatto/gocloc v0.5.2/go.mod h1:pTtvBwdm0Mhqjkqu1g9uXplkncP/CHnhhDOigLj9/ek=
359358
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
360359
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
361360
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -364,7 +363,6 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P
364363
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
365364
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
366365
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
367-
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
368366
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
369367
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
370368
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
@@ -534,7 +532,6 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
534532
github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2AQ=
535533
github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
536534
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
537-
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
538535
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
539536
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
540537
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=

ratio/copy_from_gocloc.go

+37-31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package ratio
22

3-
// original: https://github.com/hhatto/gocloc/blob/b2dad3847df87ab84c56bb8d27c91ca041e69c16/language.go
3+
// original: https://github.com/hhatto/gocloc/blob/7b24285f3e4368e0b3df5cd16b0969f3c9be03cb/language.go
44
import (
55
"bufio"
66
"bytes"
7-
"log"
87
"os"
98
"path/filepath"
109
"regexp"
@@ -27,9 +26,9 @@ var shebang2ext = map[string]string{
2726
"escript": "erl",
2827
}
2928

30-
func shebang(line string) (shebangLang string, ok bool) {
29+
func getShebang(line string) (shebangLang string, ok bool) {
3130
ret := reShebangEnv.FindAllStringSubmatch(line, -1)
32-
if len(ret) != 0 && len(ret[0]) == 3 {
31+
if ret != nil && len(ret[0]) == 3 {
3332
shebangLang = ret[0][2]
3433
if sl, ok := shebang2ext[shebangLang]; ok {
3534
return sl, ok
@@ -38,7 +37,7 @@ func shebang(line string) (shebangLang string, ok bool) {
3837
}
3938

4039
ret = reShebangLang.FindAllStringSubmatch(line, -1)
41-
if len(ret) != 0 && len(ret[0]) >= 2 {
40+
if ret != nil && len(ret[0]) >= 2 {
4241
shebangLang = ret[0][1]
4342
if sl, ok := shebang2ext[shebangLang]; ok {
4443
return sl, ok
@@ -49,18 +48,47 @@ func shebang(line string) (shebangLang string, ok bool) {
4948
return "", false
5049
}
5150

52-
func fileType(path string) (ext string, ok bool) {
51+
func getFileTypeByShebang(path string) (shebangLang string, ok bool) {
52+
f, err := os.Open(path)
53+
if err != nil {
54+
return // ignore error
55+
}
56+
defer f.Close()
57+
58+
reader := bufio.NewReader(f)
59+
line, err := reader.ReadBytes('\n')
60+
if err != nil {
61+
return
62+
}
63+
line = bytes.TrimLeftFunc(line, unicode.IsSpace)
64+
65+
if len(line) > 2 && line[0] == '#' && line[1] == '!' {
66+
return getShebang(string(line))
67+
}
68+
return
69+
}
70+
71+
func getFileType(path string) (ext string, ok bool) {
5372
ext = filepath.Ext(path)
5473
base := filepath.Base(path)
5574

5675
switch ext {
5776
case ".m", ".v", ".fs", ".r", ".ts":
58-
content, err := os.ReadFile(filepath.Clean(path))
77+
content, err := os.ReadFile(path)
78+
if err != nil {
79+
return "", false
80+
}
81+
lang := enry.GetLanguage(path, content)
82+
return lang, true
83+
case ".mo":
84+
content, err := os.ReadFile(path)
5985
if err != nil {
6086
return "", false
6187
}
6288
lang := enry.GetLanguage(path, content)
63-
log.Printf("path=%v, lang=%v\n", path, lang)
89+
if lang != "" {
90+
return "Motoko", true
91+
}
6492
return lang, true
6593
}
6694

@@ -88,7 +116,7 @@ func fileType(path string) (ext string, ok bool) {
88116
return "", false
89117
}
90118

91-
shebangLang, ok := fileTypeByShebang(path)
119+
shebangLang, ok := getFileTypeByShebang(path)
92120
if ok {
93121
return shebangLang, true
94122
}
@@ -98,25 +126,3 @@ func fileType(path string) (ext string, ok bool) {
98126
}
99127
return ext, ok
100128
}
101-
102-
func fileTypeByShebang(path string) (shebangLang string, ok bool) {
103-
f, err := os.Open(filepath.Clean(path))
104-
if err != nil {
105-
return // ignore error
106-
}
107-
reader := bufio.NewReader(f)
108-
line, err := reader.ReadBytes('\n')
109-
if err != nil {
110-
_ = f.Close() //nostyle:handlerrors
111-
return
112-
}
113-
line = bytes.TrimLeftFunc(line, unicode.IsSpace)
114-
115-
if len(line) > 2 && line[0] == '#' && line[1] == '!' {
116-
_ = f.Close() //nostyle:handlerrors
117-
return shebang(string(line))
118-
}
119-
_ = f.Close() //nostyle:handlerrors
120-
121-
return
122-
}

ratio/ratio.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func Measure(root string, code, test []string) (*Ratio, error) {
141141
if !isCode && !isTest {
142142
return nil
143143
}
144-
ext, ok := fileType(path)
144+
ext, ok := getFileType(path)
145145
if !ok {
146146
if _, err := fmt.Fprintf(os.Stderr, "could not detect language: %s\n", path); err != nil {
147147
return err
@@ -150,7 +150,7 @@ func Measure(root string, code, test []string) (*Ratio, error) {
150150
}
151151
l, ok := gocloc.Exts[ext]
152152
if !ok {
153-
if _, err := fmt.Fprintf(os.Stderr, "unsupported language: %s\n", ext); err != nil {
153+
if _, err := fmt.Fprintf(os.Stderr, "unsupported language (%s): %s\n", ext, path); err != nil {
154154
return err
155155
}
156156
return nil

0 commit comments

Comments
 (0)