mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-27 02:10:40 +00:00
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
|
@ -11,6 +11,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/optional"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckAttributeOpts represents the possible options to CheckAttribute
|
// CheckAttributeOpts represents the possible options to CheckAttribute
|
||||||
|
@ -322,3 +323,23 @@ func (repo *Repository) CheckAttributeReader(commitID string) (*CheckAttributeRe
|
||||||
|
|
||||||
return checker, deferable
|
return checker, deferable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// true if "set"/"true", false if "unset"/"false", none otherwise
|
||||||
|
func attributeToBool(attr map[string]string, name string) optional.Option[bool] {
|
||||||
|
if value, has := attr[name]; has && value != "unspecified" {
|
||||||
|
switch value {
|
||||||
|
case "set", "true":
|
||||||
|
return optional.Some(true)
|
||||||
|
case "unset", "false":
|
||||||
|
return optional.Some(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return optional.None[bool]()
|
||||||
|
}
|
||||||
|
|
||||||
|
func attributeToString(attr map[string]string, name string) optional.Option[string] {
|
||||||
|
if value, has := attr[name]; has && value != "unspecified" {
|
||||||
|
return optional.Some(value)
|
||||||
|
}
|
||||||
|
return optional.None[string]()
|
||||||
|
}
|
||||||
|
|
|
@ -13,18 +13,6 @@ const (
|
||||||
bigFileSize int64 = 1024 * 1024 // 1 MiB
|
bigFileSize int64 = 1024 * 1024 // 1 MiB
|
||||||
)
|
)
|
||||||
|
|
||||||
type LinguistBoolAttrib struct {
|
|
||||||
Value string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (attrib *LinguistBoolAttrib) IsTrue() bool {
|
|
||||||
return attrib.Value == "set" || attrib.Value == "true"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (attrib *LinguistBoolAttrib) IsFalse() bool {
|
|
||||||
return attrib.Value == "unset" || attrib.Value == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
// mergeLanguageStats mergers language names with different cases. The name with most upper case letters is used.
|
// mergeLanguageStats mergers language names with different cases. The name with most upper case letters is used.
|
||||||
func mergeLanguageStats(stats map[string]int64) map[string]int64 {
|
func mergeLanguageStats(stats map[string]int64) map[string]int64 {
|
||||||
names := map[string]struct {
|
names := map[string]struct {
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/analyze"
|
"code.gitea.io/gitea/modules/analyze"
|
||||||
|
"code.gitea.io/gitea/modules/optional"
|
||||||
|
|
||||||
"github.com/go-enry/go-enry/v2"
|
"github.com/go-enry/go-enry/v2"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
|
@ -53,31 +54,30 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
firstExcludedLanguage := ""
|
firstExcludedLanguage := ""
|
||||||
firstExcludedLanguageSize := int64(0)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
err = tree.Files().ForEach(func(f *object.File) error {
|
err = tree.Files().ForEach(func(f *object.File) error {
|
||||||
if f.Size == 0 {
|
if f.Size == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
isVendored := LinguistBoolAttrib{}
|
isVendored := optional.None[bool]()
|
||||||
isGenerated := LinguistBoolAttrib{}
|
isGenerated := optional.None[bool]()
|
||||||
isDocumentation := LinguistBoolAttrib{}
|
isDocumentation := optional.None[bool]()
|
||||||
isDetectable := LinguistBoolAttrib{}
|
isDetectable := optional.None[bool]()
|
||||||
|
|
||||||
if checker != nil {
|
if checker != nil {
|
||||||
attrs, err := checker.CheckPath(f.Name)
|
attrs, err := checker.CheckPath(f.Name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if vendored, has := attrs["linguist-vendored"]; has {
|
isVendored = attributeToBool(attrs, "linguist-vendored")
|
||||||
isVendored = LinguistBoolAttrib{Value: vendored}
|
isGenerated = attributeToBool(attrs, "linguist-generated")
|
||||||
}
|
isDocumentation = attributeToBool(attrs, "linguist-documentation")
|
||||||
if generated, has := attrs["linguist-generated"]; has {
|
isDetectable = attributeToBool(attrs, "linguist-detectable")
|
||||||
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}
|
|
||||||
}
|
|
||||||
if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
|
if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
|
||||||
// group languages, such as Pug -> HTML; SCSS -> CSS
|
// group languages, such as Pug -> HTML; SCSS -> CSS
|
||||||
group := enry.GetLanguageGroup(language)
|
group := enry.GetLanguageGroup(language)
|
||||||
|
@ -108,11 +108,11 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if isDetectable.IsFalse() || isVendored.IsTrue() || isDocumentation.IsTrue() ||
|
if isFalse(isDetectable) || isTrue(isVendored) || isTrue(isDocumentation) ||
|
||||||
(!isVendored.IsFalse() && analyze.IsVendor(f.Name)) ||
|
(!isFalse(isVendored) && analyze.IsVendor(f.Name)) ||
|
||||||
enry.IsDotFile(f.Name) ||
|
enry.IsDotFile(f.Name) ||
|
||||||
enry.IsConfiguration(f.Name) ||
|
enry.IsConfiguration(f.Name) ||
|
||||||
(!isDocumentation.IsFalse() && enry.IsDocumentation(f.Name)) {
|
(!isFalse(isDocumentation) && enry.IsDocumentation(f.Name)) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
if f.Size <= bigFileSize {
|
if f.Size <= bigFileSize {
|
||||||
content, _ = readFile(f, fileSizeLimit)
|
content, _ = readFile(f, fileSizeLimit)
|
||||||
}
|
}
|
||||||
if !isGenerated.IsTrue() && enry.IsGenerated(f.Name, content) {
|
if !isTrue(isGenerated) && enry.IsGenerated(f.Name, content) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
langtype := enry.GetLanguageType(language)
|
langtype := enry.GetLanguageType(language)
|
||||||
included = langtype == enry.Programming || langtype == enry.Markup
|
included = langtype == enry.Programming || langtype == enry.Markup
|
||||||
if !included {
|
if !included {
|
||||||
if isDetectable.IsTrue() {
|
if isTrue(isDetectable) {
|
||||||
included = true
|
included = true
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/analyze"
|
"code.gitea.io/gitea/modules/analyze"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/optional"
|
||||||
|
|
||||||
"github.com/go-enry/go-enry/v2"
|
"github.com/go-enry/go-enry/v2"
|
||||||
)
|
)
|
||||||
|
@ -77,6 +78,13 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
firstExcludedLanguage := ""
|
firstExcludedLanguage := ""
|
||||||
firstExcludedLanguageSize := int64(0)
|
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 {
|
for _, f := range entries {
|
||||||
select {
|
select {
|
||||||
case <-repo.Ctx.Done():
|
case <-repo.Ctx.Done():
|
||||||
|
@ -91,26 +99,18 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
isVendored := LinguistBoolAttrib{}
|
isVendored := optional.None[bool]()
|
||||||
isGenerated := LinguistBoolAttrib{}
|
isGenerated := optional.None[bool]()
|
||||||
isDocumentation := LinguistBoolAttrib{}
|
isDocumentation := optional.None[bool]()
|
||||||
isDetectable := LinguistBoolAttrib{}
|
isDetectable := optional.None[bool]()
|
||||||
|
|
||||||
if checker != nil {
|
if checker != nil {
|
||||||
attrs, err := checker.CheckPath(f.Name())
|
attrs, err := checker.CheckPath(f.Name())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if vendored, has := attrs["linguist-vendored"]; has {
|
isVendored = attributeToBool(attrs, "linguist-vendored")
|
||||||
isVendored = LinguistBoolAttrib{Value: vendored}
|
isGenerated = attributeToBool(attrs, "linguist-generated")
|
||||||
}
|
isDocumentation = attributeToBool(attrs, "linguist-documentation")
|
||||||
if generated, has := attrs["linguist-generated"]; has {
|
isDetectable = attributeToBool(attrs, "linguist-detectable")
|
||||||
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}
|
|
||||||
}
|
|
||||||
if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
|
if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
|
||||||
// group languages, such as Pug -> HTML; SCSS -> CSS
|
// group languages, such as Pug -> HTML; SCSS -> CSS
|
||||||
group := enry.GetLanguageGroup(language)
|
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() ||
|
if isFalse(isDetectable) || isTrue(isVendored) || isTrue(isDocumentation) ||
|
||||||
(!isVendored.IsFalse() && analyze.IsVendor(f.Name())) ||
|
(!isFalse(isVendored) && analyze.IsVendor(f.Name())) ||
|
||||||
enry.IsDotFile(f.Name()) ||
|
enry.IsDotFile(f.Name()) ||
|
||||||
enry.IsConfiguration(f.Name()) ||
|
enry.IsConfiguration(f.Name()) ||
|
||||||
(!isDocumentation.IsFalse() && enry.IsDocumentation(f.Name())) {
|
(!isFalse(isDocumentation) && enry.IsDocumentation(f.Name())) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !isGenerated.IsTrue() && enry.IsGenerated(f.Name(), content) {
|
if !isTrue(isGenerated) && enry.IsGenerated(f.Name(), content) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
||||||
langType := enry.GetLanguageType(language)
|
langType := enry.GetLanguageType(language)
|
||||||
included = langType == enry.Programming || langType == enry.Markup
|
included = langType == enry.Programming || langType == enry.Markup
|
||||||
if !included {
|
if !included {
|
||||||
if isDetectable.IsTrue() {
|
if isTrue(isDetectable) {
|
||||||
included = true
|
included = true
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in a new issue