forked from forgejo/forgejo
Convert linguist attribute handling to optional.Option
Based on @KN4CK3R's work in gitea#29267. This drops the custom `LinguistBoolAttrib` type, and uses `optional.Option` instead. I added the `isTrue()` and `isFalse()` (function-local) helpers to make the code easier to follow, because these names convey their goal better than `v.ValueorDefault(false)` or `!v.ValueOrDefault(true)`. Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
This commit is contained in:
parent
c80406332f
commit
ee39c58120
4 changed files with 63 additions and 54 deletions
|
@ -15,6 +15,7 @@ import (
|
|||
|
||||
"code.gitea.io/gitea/modules/analyze"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
|
||||
"github.com/go-enry/go-enry/v2"
|
||||
)
|
||||
|
@ -77,6 +78,13 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
firstExcludedLanguage := ""
|
||||
firstExcludedLanguageSize := int64(0)
|
||||
|
||||
isTrue := func(v optional.Option[bool]) bool {
|
||||
return v.ValueOrDefault(false)
|
||||
}
|
||||
isFalse := func(v optional.Option[bool]) bool {
|
||||
return !v.ValueOrDefault(true)
|
||||
}
|
||||
|
||||
for _, f := range entries {
|
||||
select {
|
||||
case <-repo.Ctx.Done():
|
||||
|
@ -91,26 +99,18 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
continue
|
||||
}
|
||||
|
||||
isVendored := LinguistBoolAttrib{}
|
||||
isGenerated := LinguistBoolAttrib{}
|
||||
isDocumentation := LinguistBoolAttrib{}
|
||||
isDetectable := LinguistBoolAttrib{}
|
||||
isVendored := optional.None[bool]()
|
||||
isGenerated := optional.None[bool]()
|
||||
isDocumentation := optional.None[bool]()
|
||||
isDetectable := optional.None[bool]()
|
||||
|
||||
if checker != nil {
|
||||
attrs, err := checker.CheckPath(f.Name())
|
||||
if err == nil {
|
||||
if vendored, has := attrs["linguist-vendored"]; has {
|
||||
isVendored = LinguistBoolAttrib{Value: vendored}
|
||||
}
|
||||
if generated, has := attrs["linguist-generated"]; has {
|
||||
isGenerated = LinguistBoolAttrib{Value: generated}
|
||||
}
|
||||
if documentation, has := attrs["linguist-documentation"]; has {
|
||||
isDocumentation = LinguistBoolAttrib{Value: documentation}
|
||||
}
|
||||
if detectable, has := attrs["linguist-detectable"]; has {
|
||||
isDetectable = LinguistBoolAttrib{Value: detectable}
|
||||
}
|
||||
isVendored = attributeToBool(attrs, "linguist-vendored")
|
||||
isGenerated = attributeToBool(attrs, "linguist-generated")
|
||||
isDocumentation = attributeToBool(attrs, "linguist-documentation")
|
||||
isDetectable = attributeToBool(attrs, "linguist-detectable")
|
||||
if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
|
||||
// group languages, such as Pug -> HTML; SCSS -> CSS
|
||||
group := enry.GetLanguageGroup(language)
|
||||
|
@ -142,11 +142,11 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
}
|
||||
}
|
||||
|
||||
if isDetectable.IsFalse() || isVendored.IsTrue() || isDocumentation.IsTrue() ||
|
||||
(!isVendored.IsFalse() && analyze.IsVendor(f.Name())) ||
|
||||
if isFalse(isDetectable) || isTrue(isVendored) || isTrue(isDocumentation) ||
|
||||
(!isFalse(isVendored) && analyze.IsVendor(f.Name())) ||
|
||||
enry.IsDotFile(f.Name()) ||
|
||||
enry.IsConfiguration(f.Name()) ||
|
||||
(!isDocumentation.IsFalse() && enry.IsDocumentation(f.Name())) {
|
||||
(!isFalse(isDocumentation) && enry.IsDocumentation(f.Name())) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -179,7 +179,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
if !isGenerated.IsTrue() && enry.IsGenerated(f.Name(), content) {
|
||||
if !isTrue(isGenerated) && enry.IsGenerated(f.Name(), content) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -201,7 +201,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
langType := enry.GetLanguageType(language)
|
||||
included = langType == enry.Programming || langType == enry.Markup
|
||||
if !included {
|
||||
if isDetectable.IsTrue() {
|
||||
if isTrue(isDetectable) {
|
||||
included = true
|
||||
} else {
|
||||
continue
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue