aboutsummaryrefslogtreecommitdiffstats
path: root/modules/git/repo_language_stats_nogogit.go
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2024-02-23 18:24:27 +0100
committerGitHub <noreply@github.com>2024-02-23 17:24:27 +0000
commit2a278b996fd6608973c3ab2a2cfb584e67d5bd8b (patch)
tree17521cc1b9fe37eeb412790d66b3a495d7a20e12 /modules/git/repo_language_stats_nogogit.go
parent7d0903bf90bce6d0ed2fa131ab028a55b8729b73 (diff)
downloadgitea-2a278b996fd6608973c3ab2a2cfb584e67d5bd8b.tar.gz
gitea-2a278b996fd6608973c3ab2a2cfb584e67d5bd8b.zip
Add support for `linguist-detectable` and `linguist-documentation` (#29267)
Add support for `linguist-detectable` and `linguist-documentation` Add tests for the attributes https://github.com/github-linguist/linguist/blob/master/docs/overrides.md#detectable https://github.com/github-linguist/linguist/blob/master/docs/overrides.md#documentation
Diffstat (limited to 'modules/git/repo_language_stats_nogogit.go')
-rw-r--r--modules/git/repo_language_stats_nogogit.go75
1 files changed, 41 insertions, 34 deletions
diff --git a/modules/git/repo_language_stats_nogogit.go b/modules/git/repo_language_stats_nogogit.go
index d68d7d210a..16669924d6 100644
--- a/modules/git/repo_language_stats_nogogit.go
+++ b/modules/git/repo_language_stats_nogogit.go
@@ -12,6 +12,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"
)
@@ -88,25 +89,47 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
continue
}
- notVendored := false
- notGenerated := false
+ 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 {
- if vendored == "set" || vendored == "true" {
- continue
- }
- notVendored = vendored == "false"
+ isVendored = attributeToBool(attrs, "linguist-vendored")
+ if isVendored.ValueOrDefault(false) {
+ continue
+ }
+
+ isGenerated = attributeToBool(attrs, "linguist-generated")
+ if isGenerated.ValueOrDefault(false) {
+ continue
}
- if generated, has := attrs["linguist-generated"]; has {
- if generated == "set" || generated == "true" {
- continue
+
+ isDocumentation = attributeToBool(attrs, "linguist-documentation")
+ if isDocumentation.ValueOrDefault(false) {
+ continue
+ }
+
+ isDetectable = attributeToBool(attrs, "linguist-detectable")
+ if !isDetectable.ValueOrDefault(true) {
+ continue
+ }
+
+ hasLanguage := attributeToString(attrs, "linguist-language")
+ if hasLanguage.Value() == "" {
+ hasLanguage = attributeToString(attrs, "gitlab-language")
+ if hasLanguage.Has() {
+ language := hasLanguage.Value()
+ if idx := strings.IndexByte(language, '?'); idx >= 0 {
+ hasLanguage = optional.Some(language[:idx])
+ }
}
- notGenerated = generated == "false"
}
- if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
+ if hasLanguage.Value() != "" {
+ language := hasLanguage.Value()
+
// group languages, such as Pug -> HTML; SCSS -> CSS
group := enry.GetLanguageGroup(language)
if len(group) != 0 {
@@ -116,29 +139,14 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
// this language will always be added to the size
sizes[language] += f.Size()
continue
- } else if language, has := attrs["gitlab-language"]; has && language != "unspecified" && language != "" {
- // strip off a ? if present
- if idx := strings.IndexByte(language, '?'); idx >= 0 {
- language = language[:idx]
- }
- if len(language) != 0 {
- // group languages, such as Pug -> HTML; SCSS -> CSS
- group := enry.GetLanguageGroup(language)
- if len(group) != 0 {
- language = group
- }
-
- // this language will always be added to the size
- sizes[language] += f.Size()
- continue
- }
}
-
}
}
- if (!notVendored && analyze.IsVendor(f.Name())) || enry.IsDotFile(f.Name()) ||
- enry.IsDocumentation(f.Name()) || enry.IsConfiguration(f.Name()) {
+ if (!isVendored.Has() && analyze.IsVendor(f.Name())) ||
+ enry.IsDotFile(f.Name()) ||
+ (!isDocumentation.Has() && enry.IsDocumentation(f.Name())) ||
+ enry.IsConfiguration(f.Name()) {
continue
}
@@ -170,7 +178,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
return nil, err
}
}
- if !notGenerated && enry.IsGenerated(f.Name(), content) {
+ if !isGenerated.Has() && enry.IsGenerated(f.Name(), content) {
continue
}
@@ -193,13 +201,12 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
included = langType == enry.Programming || langType == enry.Markup
includedLanguage[language] = included
}
- if included {
+ if included || isDetectable.ValueOrDefault(false) {
sizes[language] += f.Size()
} else if len(sizes) == 0 && (firstExcludedLanguage == "" || firstExcludedLanguage == language) {
firstExcludedLanguage = language
firstExcludedLanguageSize += f.Size()
}
- continue
}
// If there are no included languages add the first excluded language