Refactor git attributes (#29356)

This commit is contained in:
KN4CK3R 2024-02-24 19:46:49 +01:00 committed by GitHub
parent 98ab9445d1
commit 4197e28100
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 101 additions and 100 deletions

View file

@ -6,6 +6,8 @@ package git
import (
"strings"
"unicode"
"code.gitea.io/gitea/modules/optional"
)
const (
@ -46,3 +48,20 @@ func mergeLanguageStats(stats map[string]int64) map[string]int64 {
}
return res
}
func TryReadLanguageAttribute(attrs map[string]string) optional.Option[string] {
language := AttributeToString(attrs, AttributeLinguistLanguage)
if language.Value() == "" {
language = AttributeToString(attrs, AttributeGitlabLanguage)
if language.Has() {
raw := language.Value()
// gitlab-language may have additional parameters after the language
// ignore them and just use the main language
// https://docs.gitlab.com/ee/user/project/highlighting.html#override-syntax-highlighting-for-a-file-type
if idx := strings.IndexByte(raw, '?'); idx >= 0 {
language = optional.Some(raw[:idx])
}
}
}
return language
}