forked from Azure/draft-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linguist.go
307 lines (278 loc) · 7.52 KB
/
linguist.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
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
package linguist
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/Azure/draft/pkg/osutil"
log "github.com/sirupsen/logrus"
)
var (
isIgnored func(string) bool
isDetectedInGitAttributes func(filename string) string
)
// used for displaying results
type (
// Language is the programming langage and the percentage on how sure linguist feels about its
// decision.
Language struct {
Language string `json:"language"`
Percent float64 `json:"percent"`
// Color represents the color associated with the language in HTML hex notation.
Color string `json:"color"`
}
)
// sortableResult is a list or programming languages, sorted based on the likelihood of the
// primary programming language the application was written in.
type sortableResult []*Language
func (s sortableResult) Len() int {
return len(s)
}
func (s sortableResult) Less(i, j int) bool {
return s[i].Percent < s[j].Percent
}
func (s sortableResult) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func initLinguistAttributes(dir string) error {
ignore := []string{}
except := []string{}
detected := make(map[string]string)
gitignoreExists, err := osutil.Exists(filepath.Join(dir, ".gitignore"))
if err != nil {
return err
}
if gitignoreExists {
log.Debugln("found .gitignore")
f, err := os.Open(filepath.Join(dir, ".gitignore"))
if err != nil {
return err
}
defer f.Close()
ignoreScanner := bufio.NewScanner(f)
for ignoreScanner.Scan() {
var isExcept bool
path := strings.TrimSpace(ignoreScanner.Text())
// if it's whitespace or a comment
if len(path) == 0 || string(path[0]) == "#" {
continue
}
if string(path[0]) == "!" {
isExcept = true
path = path[1:]
}
p := strings.Trim(path, string(filepath.Separator))
if isExcept {
except = append(except, p)
} else {
ignore = append(ignore, p)
}
}
if err := ignoreScanner.Err(); err != nil {
return fmt.Errorf("error reading .gitignore: %v", err)
}
}
gitAttributesExists, err := osutil.Exists(filepath.Join(dir, ".gitattributes"))
if err != nil {
return err
}
if gitAttributesExists {
log.Debugln("found .gitattributes")
f, err := os.Open(filepath.Join(dir, ".gitattributes"))
if err != nil {
return err
}
defer f.Close()
attributeScanner := bufio.NewScanner(f)
var lineNumber int
for attributeScanner.Scan() {
lineNumber++
line := strings.TrimSpace(attributeScanner.Text())
words := strings.Fields(line)
if len(words) != 2 {
log.Printf("invalid line in .gitattributes at L%d: '%s'\n", lineNumber, line)
continue
}
path := strings.Trim(words[0], string(filepath.Separator))
if runtime.GOOS == "windows" {
// on Windows, we also accept / as a path separator, so let's strip those as well
path = strings.Trim(words[0], "/")
}
attribute := words[1]
if strings.HasPrefix(attribute, "linguist-documentation") || strings.HasPrefix(attribute, "linguist-vendored") || strings.HasPrefix(attribute, "linguist-generated") {
if !strings.HasSuffix(strings.ToLower(attribute), "false") {
ignore = append(ignore, path)
}
} else if strings.HasPrefix(attribute, "linguist-language") {
attr := strings.Split(attribute, "=")
if len(attr) != 2 {
log.Printf("invalid line in .gitattributes at L%d: '%s'\n", lineNumber, line)
continue
}
language := attr[1]
detected[path] = language
}
}
if err := attributeScanner.Err(); err != nil {
return fmt.Errorf("error reading .gitattributes: %v", err)
}
}
isIgnored = func(filename string) bool {
for _, p := range ignore {
cleanPath, err := filepath.Rel(dir, filename)
if err != nil {
log.Debugf("could not get relative path: %v", err)
return false
}
if m, _ := filepath.Match(p, cleanPath); m {
for _, e := range except {
if m, _ := filepath.Match(e, cleanPath); m {
return false
}
}
return true
}
}
return false
}
isDetectedInGitAttributes = func(filename string) string {
for p, lang := range detected {
cleanPath, err := filepath.Rel(dir, filename)
if err != nil {
log.Debugf("could not get relative path: %v", err)
return ""
}
if m, _ := filepath.Match(p, cleanPath); m {
return lang
}
}
return ""
}
return nil
}
// shoutouts to php
func fileGetContents(filename string) ([]byte, error) {
log.Debugln("reading contents of", filename)
// read only first 512 bytes of files
contents := make([]byte, 512)
f, err := os.Open(filename)
if err != nil {
return nil, err
}
_, err = f.Read(contents)
f.Close()
if err != io.EOF {
if err != nil {
return nil, err
}
}
return contents, nil
}
// ProcessDir walks through a directory and returns a list of sorted languages within that directory.
func ProcessDir(dirname string) ([]*Language, error) {
var (
langs = make(map[string]int)
totalSize int
)
if err := initLinguistAttributes(dirname); err != nil {
return nil, err
}
exists, err := osutil.Exists(dirname)
if err != nil {
return nil, err
}
if !exists {
return nil, os.ErrNotExist
}
filepath.Walk(dirname, func(path string, file os.FileInfo, err error) error {
size := int(file.Size())
log.Debugf("with file: %s", path)
log.Debugln(path, "is", size, "bytes")
if isIgnored(path) {
log.Debugln(path, "is ignored, skipping")
if file.IsDir() {
return filepath.SkipDir
}
return nil
}
if size == 0 {
log.Debugln(path, "is empty file, skipping")
return nil
}
if file.IsDir() {
if file.Name() == ".git" {
log.Debugln(".git directory, skipping")
return filepath.SkipDir
}
} else if (file.Mode() & os.ModeSymlink) == 0 {
if ShouldIgnoreFilename(path) {
log.Debugf("%s: filename should be ignored, skipping", path)
return nil
}
byGitAttr := isDetectedInGitAttributes(path)
if byGitAttr != "" {
log.Debugln(path, "got result by .gitattributes: ", byGitAttr)
langs[byGitAttr] += size
totalSize += size
return nil
}
if byName := LanguageByFilename(path); byName != "" {
log.Debugln(path, "got result by name: ", byName)
langs[byName] += size
totalSize += size
return nil
}
contents, err := fileGetContents(path)
if err != nil {
return err
}
if ShouldIgnoreContents(contents) {
log.Debugln(path, ": contents should be ignored, skipping")
return nil
}
hints := LanguageHints(path)
log.Debugf("%s got language hints: %#v\n", path, hints)
byData := LanguageByContents(contents, hints)
if byData != "" {
log.Debugln(path, "got result by data: ", byData)
langs[byData] += size
totalSize += size
return nil
}
log.Debugln(path, "got no result!!")
langs["(unknown)"] += size
totalSize += size
}
return nil
})
results := []*Language{}
for lang, size := range langs {
l := &Language{
Language: lang,
Percent: (float64(size) / float64(totalSize)) * 100.0,
Color: LanguageColor(lang),
}
results = append(results, l)
log.Debugf("language: %s percent: %f color: %s", l.Language, l.Percent, l.Color)
}
sort.Sort(sort.Reverse(sortableResult(results)))
return results, nil
}
// Alias returns the language name for a given known alias.
//
// Occasionally linguist comes up with odd language names, or determines a Java app as a "Maven POM"
// app, which in essence is the same thing for Draft's intent.
func Alias(lang *Language) *Language {
packAliases := map[string]string{
"maven pom": "Java",
"c#": "csharp",
}
if alias, ok := packAliases[strings.ToLower(lang.Language)]; ok {
lang.Language = alias
}
return lang
}