mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
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>
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/highlight"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/templates"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
@@ -204,6 +205,31 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st
|
||||
func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]*models.UserCommit, previousCommits map[string]string) {
|
||||
repoLink := ctx.Repo.RepoLink
|
||||
|
||||
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 = ""
|
||||
}
|
||||
}
|
||||
var lines = make([]string, 0)
|
||||
rows := make([]*blameRow, 0)
|
||||
|
||||
@@ -248,7 +274,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
|
||||
line += "\n"
|
||||
}
|
||||
fileName := fmt.Sprintf("%v", ctx.Data["FileName"])
|
||||
line = highlight.Code(fileName, line)
|
||||
line = highlight.Code(fileName, language, line)
|
||||
|
||||
br.Code = gotemplate.HTML(line)
|
||||
rows = append(rows, br)
|
||||
|
@@ -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() {
|
||||
|
Reference in New Issue
Block a user