diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2024-02-14 19:50:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-14 18:50:31 +0000 |
commit | 94d06be035bac468129903c9f32e785baf3c1c3b (patch) | |
tree | a3abad10adec208a4fe0ff12699afe8eb62e4502 /services | |
parent | a346a8c852a44c9c22eeb06701a384bb17a7ac0b (diff) | |
download | gitea-94d06be035bac468129903c9f32e785baf3c1c3b.tar.gz gitea-94d06be035bac468129903c9f32e785baf3c1c3b.zip |
Extract linguist code to method (#29168)
Diffstat (limited to 'services')
-rw-r--r-- | services/repository/files/content.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/services/repository/files/content.go b/services/repository/files/content.go index c278d7f835..f2a7677688 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -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 +} |