diff options
author | zeripath <art27@cantab.net> | 2021-11-17 20:37:00 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-17 20:37:00 +0000 |
commit | 3c4724d70e4ac7bfc06b97f6fad8936f97479b6b (patch) | |
tree | 754286def789b823e020d3ccfafae148d0017b62 /modules/git/repo_language_stats_gogit.go | |
parent | 81a4fc752833101dd7d6b4f612bccc4b29c98dff (diff) | |
download | gitea-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 'modules/git/repo_language_stats_gogit.go')
-rw-r--r-- | modules/git/repo_language_stats_gogit.go | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/modules/git/repo_language_stats_gogit.go b/modules/git/repo_language_stats_gogit.go index d37827c3de..037ec41ec6 100644 --- a/modules/git/repo_language_stats_gogit.go +++ b/modules/git/repo_language_stats_gogit.go @@ -11,11 +11,10 @@ import ( "bytes" "context" "io" - "os" + "strings" "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/util" "github.com/go-enry/go-enry/v2" "github.com/go-git/go-git/v5" @@ -48,35 +47,28 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err var checker *CheckAttributeReader if CheckGitVersionAtLeast("1.7.8") == nil { - indexFilename, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID) + indexFilename, workTree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID) if err == nil { defer deleteTemporaryFile() - tmpWorkTree, err := os.MkdirTemp("", "empty-work-dir") - if err == nil { - defer func() { - _ = util.RemoveAll(tmpWorkTree) + checker = &CheckAttributeReader{ + Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"}, + Repo: repo, + IndexFile: indexFilename, + WorkTree: workTree, + } + ctx, cancel := context.WithCancel(DefaultContext) + if err := checker.Init(ctx); err != nil { + log.Error("Unable to open checker for %s. Error: %v", commitID, err) + } else { + go func() { + err = checker.Run() + if err != nil { + log.Error("Unable to open checker for %s. Error: %v", commitID, err) + cancel() + } }() - - checker = &CheckAttributeReader{ - Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language"}, - Repo: repo, - IndexFile: indexFilename, - WorkTree: tmpWorkTree, - } - ctx, cancel := context.WithCancel(DefaultContext) - if err := checker.Init(ctx); err != nil { - log.Error("Unable to open checker for %s. Error: %v", commitID, err) - } else { - go func() { - err = checker.Run() - if err != nil { - log.Error("Unable to open checker for %s. Error: %v", commitID, err) - cancel() - } - }() - } - defer cancel() } + defer cancel() } } @@ -114,6 +106,21 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err sizes[language] += f.Size return nil + } else if language, has := attrs["gitlab-language"]; has && language != "unspecified" && language != "" { + // strip off a ? if present + if idx := strings.IndexByte(language, '?'); idx >= 0 { + language = language[:idx] + } + if len(language) != 0 { + // group languages, such as Pug -> HTML; SCSS -> CSS + group := enry.GetLanguageGroup(language) + if len(group) != 0 { + language = group + } + + sizes[language] += f.Size + return nil + } } } } |