Skip to content

Commit 1a52244

Browse files
committed
merge two functions call as one
1 parent 9db6749 commit 1a52244

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

routers/web/repo/view_file.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"code.gitea.io/gitea/modules/charset"
2020
"code.gitea.io/gitea/modules/git"
2121
"code.gitea.io/gitea/modules/git/attribute"
22-
"code.gitea.io/gitea/modules/git/languagestats"
2322
"code.gitea.io/gitea/modules/highlight"
2423
"code.gitea.io/gitea/modules/log"
2524
"code.gitea.io/gitea/modules/markup"
@@ -148,6 +147,9 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
148147
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
149148
}
150149

150+
var attrs attribute.Attributes
151+
attributes := []string{attribute.LinguistGenerated, attribute.LinguistVendored}
152+
151153
switch {
152154
case isRepresentableAsText:
153155
if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
@@ -210,9 +212,17 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
210212
ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
211213
}
212214

213-
language, err := languagestats.GetFileLanguage(ctx, ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath)
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+
})
214221
if err != nil {
215222
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()
216226
}
217227

218228
fileContent, lexerName, err := highlight.File(blob.Name(), language, buf)
@@ -284,21 +294,20 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
284294
}
285295
}
286296

287-
if ctx.Repo.GitRepo != nil {
288-
checker, err := attribute.NewBatchChecker(ctx.Repo.GitRepo, ctx.Repo.CommitID, attribute.LinguistGenerated, attribute.LinguistVendored)
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+
})
289302
if err != nil {
290-
ctx.ServerError("NewAttributeChecker", err)
303+
ctx.ServerError("attribute.CheckAttributes", err)
291304
return
292305
}
293-
defer checker.Close()
294-
295-
attrs, err := checker.CheckPath(ctx.Repo.TreePath)
296-
if err == nil {
297-
ctx.Data["IsVendored"] = attrs.GetVendored().Value()
298-
ctx.Data["IsGenerated"] = attrs.GetGenerated().Value()
299-
}
306+
attrs = attrsMap[ctx.Repo.TreePath]
300307
}
301308

309+
ctx.Data["IsVendored"], ctx.Data["IsGenerated"] = attrs.GetVendored().Value(), attrs.GetGenerated().Value()
310+
302311
if fInfo.st.IsImage() && !fInfo.st.IsSvgImage() {
303312
img, _, err := image.DecodeConfig(bytes.NewReader(buf))
304313
if err == nil {

0 commit comments

Comments
 (0)