Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for linguist-detectable and linguist-documentation #29267

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Change method name.
  • Loading branch information
KN4CK3R committed Feb 22, 2024
commit bb4e8b773e14ec6cefa872e5d9a2543aa0bd2107
21 changes: 21 additions & 0 deletions modules/git/repo_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
)

// CheckAttributeOpts represents the possible options to CheckAttribute
Expand Down Expand Up @@ -316,3 +317,23 @@ func (repo *Repository) CheckAttributeReader(commitID string) (*CheckAttributeRe

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]()
}
22 changes: 0 additions & 22 deletions modules/git/repo_language_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,13 @@ package git
import (
"strings"
"unicode"

"code.gitea.io/gitea/modules/optional"
)

const (
fileSizeLimit int64 = 16 * 1024 // 16 KiB
bigFileSize int64 = 1024 * 1024 // 1 MiB
)

// true if "set"/"true", false if "unset"/"false", none otherwise
func linguistToBool(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 linguistToString(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]()
}

// 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 {
names := map[string]struct {
Expand Down
12 changes: 6 additions & 6 deletions modules/git/repo_language_stats_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,29 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
if checker != nil {
attrs, err := checker.CheckPath(f.Name)
if err == nil {
isVendored = linguistToBool(attrs, "linguist-vendored")
isVendored = attributeToBool(attrs, "linguist-vendored")
if isVendored.ValueOrDefault(false) {
return nil
}

isGenerated = linguistToBool(attrs, "linguist-generated")
isGenerated = attributeToBool(attrs, "linguist-generated")
if isGenerated.ValueOrDefault(false) {
return nil
}

isDocumentation = linguistToBool(attrs, "linguist-documentation")
isDocumentation = attributeToBool(attrs, "linguist-documentation")
if isDocumentation.ValueOrDefault(false) {
return nil
}

isDetectable = linguistToBool(attrs, "linguist-detectable")
isDetectable = attributeToBool(attrs, "linguist-detectable")
if !isDetectable.ValueOrDefault(true) {
return nil
}

hasLanguage := linguistToString(attrs, "linguist-language")
hasLanguage := attributeToString(attrs, "linguist-language")
if hasLanguage.Value() == "" {
hasLanguage = linguistToString(attrs, "gitlab-language")
hasLanguage = attributeToString(attrs, "gitlab-language")
if hasLanguage.Has() {
language := hasLanguage.Value()
if idx := strings.IndexByte(language, '?'); idx >= 0 {
Expand Down
12 changes: 6 additions & 6 deletions modules/git/repo_language_stats_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,29 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
if checker != nil {
attrs, err := checker.CheckPath(f.Name())
if err == nil {
isVendored = linguistToBool(attrs, "linguist-vendored")
isVendored = attributeToBool(attrs, "linguist-vendored")
if isVendored.ValueOrDefault(false) {
continue
}

isGenerated = linguistToBool(attrs, "linguist-generated")
isGenerated = attributeToBool(attrs, "linguist-generated")
if isGenerated.ValueOrDefault(false) {
continue
}

isDocumentation = linguistToBool(attrs, "linguist-documentation")
isDocumentation = attributeToBool(attrs, "linguist-documentation")
if isDocumentation.ValueOrDefault(false) {
continue
}

isDetectable = linguistToBool(attrs, "linguist-detectable")
isDetectable = attributeToBool(attrs, "linguist-detectable")
if !isDetectable.ValueOrDefault(true) {
continue
}

hasLanguage := linguistToString(attrs, "linguist-language")
hasLanguage := attributeToString(attrs, "linguist-language")
if hasLanguage.Value() == "" {
hasLanguage = linguistToString(attrs, "gitlab-language")
hasLanguage = attributeToString(attrs, "gitlab-language")
if hasLanguage.Has() {
language := hasLanguage.Value()
if idx := strings.IndexByte(language, '?'); idx >= 0 {
Expand Down