mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package attribute
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/optional"
|
|
)
|
|
|
|
type Attribute string
|
|
|
|
const (
|
|
LinguistVendored = "linguist-vendored"
|
|
LinguistGenerated = "linguist-generated"
|
|
LinguistDocumentation = "linguist-documentation"
|
|
LinguistDetectable = "linguist-detectable"
|
|
LinguistLanguage = "linguist-language"
|
|
GitlabLanguage = "gitlab-language"
|
|
Lockable = "lockable"
|
|
Filter = "filter"
|
|
Diff = "diff"
|
|
)
|
|
|
|
var LinguistAttributes = []string{
|
|
LinguistVendored,
|
|
LinguistGenerated,
|
|
LinguistDocumentation,
|
|
LinguistDetectable,
|
|
LinguistLanguage,
|
|
GitlabLanguage,
|
|
}
|
|
|
|
func (a Attribute) IsUnspecified() bool {
|
|
return a == "" || a == "unspecified"
|
|
}
|
|
|
|
func (a Attribute) ToString() optional.Option[string] {
|
|
if !a.IsUnspecified() {
|
|
return optional.Some(string(a))
|
|
}
|
|
return optional.None[string]()
|
|
}
|
|
|
|
// ToBool converts the attribute value to optional boolean: true if "set"/"true", false if "unset"/"false", none otherwise
|
|
func (a Attribute) ToBool() optional.Option[bool] {
|
|
switch a {
|
|
case "set", "true":
|
|
return optional.Some(true)
|
|
case "unset", "false":
|
|
return optional.Some(false)
|
|
}
|
|
return optional.None[bool]()
|
|
}
|
|
|
|
type Attributes struct {
|
|
m map[string]Attribute
|
|
}
|
|
|
|
func NewAttributes() *Attributes {
|
|
return &Attributes{m: make(map[string]Attribute)}
|
|
}
|
|
|
|
func (attrs *Attributes) Get(name string) Attribute {
|
|
if value, has := attrs.m[name]; has {
|
|
return value
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (attrs *Attributes) GetVendored() optional.Option[bool] {
|
|
return attrs.Get(LinguistVendored).ToBool()
|
|
}
|
|
|
|
func (attrs *Attributes) GetGenerated() optional.Option[bool] {
|
|
return attrs.Get(LinguistGenerated).ToBool()
|
|
}
|
|
|
|
func (attrs *Attributes) GetDocumentation() optional.Option[bool] {
|
|
return attrs.Get(LinguistDocumentation).ToBool()
|
|
}
|
|
|
|
func (attrs *Attributes) GetDetectable() optional.Option[bool] {
|
|
return attrs.Get(LinguistDetectable).ToBool()
|
|
}
|
|
|
|
func (attrs *Attributes) GetLinguistLanguage() optional.Option[string] {
|
|
return attrs.Get(LinguistLanguage).ToString()
|
|
}
|
|
|
|
func (attrs *Attributes) GetGitlabLanguage() optional.Option[string] {
|
|
attrStr := attrs.Get(GitlabLanguage).ToString()
|
|
if attrStr.Has() {
|
|
raw := attrStr.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 before, _, ok := strings.Cut(raw, "?"); ok {
|
|
return optional.Some(before)
|
|
}
|
|
}
|
|
return attrStr
|
|
}
|
|
|
|
func (attrs *Attributes) GetLanguage() optional.Option[string] {
|
|
// prefer linguist-language over gitlab-language
|
|
// if linguist-language is not set, use gitlab-language
|
|
// if both are not set, return none
|
|
language := attrs.GetLinguistLanguage()
|
|
if language.Value() == "" {
|
|
language = attrs.GetGitlabLanguage()
|
|
}
|
|
return language
|
|
}
|