Extract linguist code to method (#29168)

(cherry picked from commit 94d06be035bac468129903c9f32e785baf3c1c3b)
This commit is contained in:
KN4CK3R 2024-02-14 19:50:31 +01:00 committed by Earl Warren
parent 52ef33b931
commit d565d85160
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 41 additions and 48 deletions

View file

@ -270,3 +270,34 @@ func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git
Content: content,
}, nil
}
// TryGetContentLanguage tries to get the (linguist) language of the file content
func TryGetContentLanguage(gitRepo *git.Repository, commitID, treePath string) (string, error) {
indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(commitID)
if err != nil {
return "", err
}
defer deleteTemporaryFile()
filename2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{
CachedOnly: true,
Attributes: []string{"linguist-language", "gitlab-language"},
Filenames: []string{treePath},
IndexFile: indexFilename,
WorkTree: worktree,
})
if err != nil {
return "", err
}
language := filename2attribute2info[treePath]["linguist-language"]
if language == "" || language == "unspecified" {
language = filename2attribute2info[treePath]["gitlab-language"]
}
if language == "unspecified" {
language = ""
}
return language, nil
}