gitea源码

language_stats.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package languagestats
  4. import (
  5. "context"
  6. "strings"
  7. "unicode"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/git/attribute"
  10. )
  11. const (
  12. fileSizeLimit int64 = 16 * 1024 // 16 KiB
  13. bigFileSize int64 = 1024 * 1024 // 1 MiB
  14. )
  15. // mergeLanguageStats mergers language names with different cases. The name with most upper case letters is used.
  16. func mergeLanguageStats(stats map[string]int64) map[string]int64 {
  17. names := map[string]struct {
  18. uniqueName string
  19. upperCount int
  20. }{}
  21. countUpper := func(s string) (count int) {
  22. for _, r := range s {
  23. if unicode.IsUpper(r) {
  24. count++
  25. }
  26. }
  27. return count
  28. }
  29. for name := range stats {
  30. cnt := countUpper(name)
  31. lower := strings.ToLower(name)
  32. if cnt >= names[lower].upperCount {
  33. names[lower] = struct {
  34. uniqueName string
  35. upperCount int
  36. }{uniqueName: name, upperCount: cnt}
  37. }
  38. }
  39. res := make(map[string]int64, len(names))
  40. for name, num := range stats {
  41. res[names[strings.ToLower(name)].uniqueName] += num
  42. }
  43. return res
  44. }
  45. // GetFileLanguage tries to get the (linguist) language of the file content
  46. func GetFileLanguage(ctx context.Context, gitRepo *git.Repository, treeish, treePath string) (string, error) {
  47. attributesMap, err := attribute.CheckAttributes(ctx, gitRepo, treeish, attribute.CheckAttributeOpts{
  48. Attributes: []string{attribute.LinguistLanguage, attribute.GitlabLanguage},
  49. Filenames: []string{treePath},
  50. })
  51. if err != nil {
  52. return "", err
  53. }
  54. return attributesMap[treePath].GetLanguage().Value(), nil
  55. }