mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-18 22:23:07 -03:00
implement IsGenerated helper to filter out generated files
Closes #17 Implements the IsGenerated helper function to filter out generated files using the rules and matchers in: - https://github.com/github/linguist/blob/master/lib/linguist/generated.rb Since the vast majority of matchers have very different logic, it cannot be autogenerated directly from linguist like other logics in enry, so it's translated by hand. There are three different types of matchers in this implementation: - By extension, which mark as generated based only in the extension. These are the fastest matchers, so they're done first. - By file name, which matches patterns against the filename. These are performed in second place. Unlike linguist, we try to use string functions instead of regexps as much as possible. - Finally, the rest of the matchers, which go into the content and try to identify if they're generated or not based on the content. Unlike linguist, we try to only read the content we need and not split it all unless it's necessary and use byte functions instead of regexps as much as possible. Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
This commit is contained in:
33
utils.go
33
utils.go
@ -11,8 +11,13 @@ import (
|
||||
|
||||
const binSniffLen = 8000
|
||||
|
||||
var configurationLanguages = map[string]bool{
|
||||
"XML": true, "JSON": true, "TOML": true, "YAML": true, "INI": true, "SQL": true,
|
||||
var configurationLanguages = map[string]struct{}{
|
||||
"XML": {},
|
||||
"JSON": {},
|
||||
"TOML": {},
|
||||
"YAML": {},
|
||||
"INI": {},
|
||||
"SQL": {},
|
||||
}
|
||||
|
||||
// IsConfiguration tells if filename is in one of the configuration languages.
|
||||
@ -102,3 +107,27 @@ func matchRegexSlice(exprs []regex.EnryRegexp, str string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsGenerated returns whether the file with the given path and content is a
|
||||
// generated file.
|
||||
func IsGenerated(path string, content []byte) bool {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
if _, ok := data.GeneratedCodeExtensions[ext]; ok {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, m := range data.GeneratedCodeNameMatchers {
|
||||
if m(path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
path = strings.ToLower(path)
|
||||
for _, m := range data.GeneratedCodeMatchers {
|
||||
if m(path, ext, content) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user