aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web/repo/view.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-11-17 20:37:00 +0000
committerGitHub <noreply@github.com>2021-11-17 20:37:00 +0000
commit3c4724d70e4ac7bfc06b97f6fad8936f97479b6b (patch)
tree754286def789b823e020d3ccfafae148d0017b62 /routers/web/repo/view.go
parent81a4fc752833101dd7d6b4f612bccc4b29c98dff (diff)
downloadgitea-3c4724d70e4ac7bfc06b97f6fad8936f97479b6b.tar.gz
gitea-3c4724d70e4ac7bfc06b97f6fad8936f97479b6b.zip
Add .gitattribute assisted language detection to blame, diff and render (#17590)
Use check attribute code to check the assigned language of a file and send that in to chroma as a hint for the language of the file. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers/web/repo/view.go')
-rw-r--r--routers/web/repo/view.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go
index 12b3aef505..938292a37a 100644
--- a/routers/web/repo/view.go
+++ b/routers/web/repo/view.go
@@ -502,7 +502,33 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
lineNums := linesBytesCount(buf)
ctx.Data["NumLines"] = strconv.Itoa(lineNums)
ctx.Data["NumLinesSet"] = true
- ctx.Data["FileContent"] = highlight.File(lineNums, blob.Name(), buf)
+
+ language := ""
+
+ indexFilename, worktree, deleteTemporaryFile, err := ctx.Repo.GitRepo.ReadTreeToTemporaryIndex(ctx.Repo.CommitID)
+ if err == nil {
+ defer deleteTemporaryFile()
+
+ filename2attribute2info, err := ctx.Repo.GitRepo.CheckAttribute(git.CheckAttributeOpts{
+ CachedOnly: true,
+ Attributes: []string{"linguist-language", "gitlab-language"},
+ Filenames: []string{ctx.Repo.TreePath},
+ IndexFile: indexFilename,
+ WorkTree: worktree,
+ })
+ if err != nil {
+ log.Error("Unable to load attributes for %-v:%s. Error: %v", ctx.Repo.Repository, ctx.Repo.TreePath, err)
+ }
+
+ language = filename2attribute2info[ctx.Repo.TreePath]["linguist-language"]
+ if language == "" || language == "unspecified" {
+ language = filename2attribute2info[ctx.Repo.TreePath]["gitlab-language"]
+ }
+ if language == "unspecified" {
+ language = ""
+ }
+ }
+ ctx.Data["FileContent"] = highlight.File(lineNums, blob.Name(), language, buf)
}
if !isLFSFile {
if ctx.Repo.CanEnableEditor() {