@@ -11,6 +11,7 @@ import (
1111 "strings"
1212
1313 "code.gitea.io/gitea/modules/analyze"
14+ "code.gitea.io/gitea/modules/optional"
1415
1516 "github.com/go-enry/go-enry/v2"
1617 "github.com/go-git/go-git/v5"
@@ -57,25 +58,47 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
5758 return nil
5859 }
5960
60- notVendored := false
61- notGenerated := false
61+ isVendored := optional .None [bool ]()
62+ isGenerated := optional .None [bool ]()
63+ isDocumentation := optional .None [bool ]()
64+ isDetectable := optional .None [bool ]()
6265
6366 if checker != nil {
6467 attrs , err := checker .CheckPath (f .Name )
6568 if err == nil {
66- if vendored , has := attrs ["linguist-vendored" ]; has {
67- if vendored == "set" || vendored == "true" {
68- return nil
69- }
70- notVendored = vendored == "false"
69+ isVendored = attributeToBool (attrs , "linguist-vendored" )
70+ if isVendored .ValueOrDefault (false ) {
71+ return nil
72+ }
73+
74+ isGenerated = attributeToBool (attrs , "linguist-generated" )
75+ if isGenerated .ValueOrDefault (false ) {
76+ return nil
7177 }
72- if generated , has := attrs ["linguist-generated" ]; has {
73- if generated == "set" || generated == "true" {
74- return nil
78+
79+ isDocumentation = attributeToBool (attrs , "linguist-documentation" )
80+ if isDocumentation .ValueOrDefault (false ) {
81+ return nil
82+ }
83+
84+ isDetectable = attributeToBool (attrs , "linguist-detectable" )
85+ if ! isDetectable .ValueOrDefault (true ) {
86+ return nil
87+ }
88+
89+ hasLanguage := attributeToString (attrs , "linguist-language" )
90+ if hasLanguage .Value () == "" {
91+ hasLanguage = attributeToString (attrs , "gitlab-language" )
92+ if hasLanguage .Has () {
93+ language := hasLanguage .Value ()
94+ if idx := strings .IndexByte (language , '?' ); idx >= 0 {
95+ hasLanguage = optional .Some (language [:idx ])
96+ }
7597 }
76- notGenerated = generated == "false"
7798 }
78- if language , has := attrs ["linguist-language" ]; has && language != "unspecified" && language != "" {
99+ if hasLanguage .Value () != "" {
100+ language := hasLanguage .Value ()
101+
79102 // group languages, such as Pug -> HTML; SCSS -> CSS
80103 group := enry .GetLanguageGroup (language )
81104 if len (group ) != 0 {
@@ -85,28 +108,14 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
85108 // this language will always be added to the size
86109 sizes [language ] += f .Size
87110 return nil
88- } else if language , has := attrs ["gitlab-language" ]; has && language != "unspecified" && language != "" {
89- // strip off a ? if present
90- if idx := strings .IndexByte (language , '?' ); idx >= 0 {
91- language = language [:idx ]
92- }
93- if len (language ) != 0 {
94- // group languages, such as Pug -> HTML; SCSS -> CSS
95- group := enry .GetLanguageGroup (language )
96- if len (group ) != 0 {
97- language = group
98- }
99-
100- // this language will always be added to the size
101- sizes [language ] += f .Size
102- return nil
103- }
104111 }
105112 }
106113 }
107114
108- if (! notVendored && analyze .IsVendor (f .Name )) || enry .IsDotFile (f .Name ) ||
109- enry .IsDocumentation (f .Name ) || enry .IsConfiguration (f .Name ) {
115+ if (! isVendored .Has () && analyze .IsVendor (f .Name )) ||
116+ enry .IsDotFile (f .Name ) ||
117+ (! isDocumentation .Has () && enry .IsDocumentation (f .Name )) ||
118+ enry .IsConfiguration (f .Name ) {
110119 return nil
111120 }
112121
@@ -115,12 +124,10 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
115124 if f .Size <= bigFileSize {
116125 content , _ = readFile (f , fileSizeLimit )
117126 }
118- if ! notGenerated && enry .IsGenerated (f .Name , content ) {
127+ if ! isGenerated . Has () && enry .IsGenerated (f .Name , content ) {
119128 return nil
120129 }
121130
122- // TODO: Use .gitattributes file for linguist overrides
123-
124131 language := analyze .GetCodeLanguage (f .Name , content )
125132 if language == enry .OtherLanguage || language == "" {
126133 return nil
@@ -138,7 +145,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
138145 included = langtype == enry .Programming || langtype == enry .Markup
139146 includedLanguage [language ] = included
140147 }
141- if included {
148+ if included || isDetectable . ValueOrDefault ( false ) {
142149 sizes [language ] += f .Size
143150 } else if len (sizes ) == 0 && (firstExcludedLanguage == "" || firstExcludedLanguage == language ) {
144151 firstExcludedLanguage = language
0 commit comments