@@ -147,8 +147,22 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
147147 ctx .Data ["EditFileTooltip" ] = ctx .Tr ("repo.editor.cannot_edit_non_text_files" )
148148 }
149149
150- var attrs attribute.Attributes
151- attributes := []string {attribute .LinguistGenerated , attribute .LinguistVendored }
150+ // read all needed attributes which will be used later
151+ // there should be no performance different between reading 2 or 4 here
152+ attrsMap , err := attribute .CheckAttributes (ctx , ctx .Repo .GitRepo , ctx .Repo .CommitID , attribute.CheckAttributeOpts {
153+ Filenames : []string {ctx .Repo .TreePath },
154+ Attributes : []string {attribute .LinguistGenerated , attribute .LinguistVendored , attribute .LinguistLanguage , attribute .GitlabLanguage },
155+ })
156+ if err != nil {
157+ ctx .ServerError ("attribute.CheckAttributes" , err )
158+ return
159+ }
160+ attrs := attrsMap [ctx .Repo .TreePath ]
161+ if attrs == nil {
162+ // this case shouldn't happe, just in case.
163+ setting .PanicInDevOrTesting ("no attributes found for %s" , ctx .Repo .TreePath )
164+ attrs = attribute .NewAttributes ()
165+ }
152166
153167 switch {
154168 case isRepresentableAsText :
@@ -212,19 +226,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
212226 ctx .Data ["NumLines" ] = bytes .Count (buf , []byte {'\n' }) + 1
213227 }
214228
215- var language string
216- attributes = append (attributes , attribute .LinguistLanguage , attribute .GitlabLanguage )
217- attrsMap , err := attribute .CheckAttributes (ctx , ctx .Repo .GitRepo , ctx .Repo .CommitID , attribute.CheckAttributeOpts {
218- Filenames : []string {ctx .Repo .TreePath },
219- Attributes : attributes ,
220- })
221- if err != nil {
222- log .Error ("Unable to get file language for %-v:%s. Error: %v" , ctx .Repo .Repository , ctx .Repo .TreePath , err )
223- } else {
224- attrs = attrsMap [ctx .Repo .TreePath ] // then it will be reused out of the switch block
225- language = attrs .GetLanguage ().Value ()
226- }
227-
229+ language := attrs .GetLanguage ().Value ()
228230 fileContent , lexerName , err := highlight .File (blob .Name (), language , buf )
229231 ctx .Data ["LexerName" ] = lexerName
230232 if err != nil {
@@ -294,18 +296,6 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
294296 }
295297 }
296298
297- if attrs == nil {
298- attrsMap , err := attribute .CheckAttributes (ctx , ctx .Repo .GitRepo , ctx .Repo .CommitID , attribute.CheckAttributeOpts {
299- Filenames : []string {ctx .Repo .TreePath },
300- Attributes : attributes ,
301- })
302- if err != nil {
303- ctx .ServerError ("attribute.CheckAttributes" , err )
304- return
305- }
306- attrs = attrsMap [ctx .Repo .TreePath ]
307- }
308-
309299 ctx .Data ["IsVendored" ], ctx .Data ["IsGenerated" ] = attrs .GetVendored ().Value (), attrs .GetGenerated ().Value ()
310300
311301 if fInfo .st .IsImage () && ! fInfo .st .IsSvgImage () {
0 commit comments