code.gitea.io/gitea@v1.22.3/modules/git/repo_language_stats.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package git
     5  
     6  import (
     7  	"strings"
     8  	"unicode"
     9  
    10  	"code.gitea.io/gitea/modules/optional"
    11  )
    12  
    13  const (
    14  	fileSizeLimit int64 = 16 * 1024   // 16 KiB
    15  	bigFileSize   int64 = 1024 * 1024 // 1 MiB
    16  )
    17  
    18  // mergeLanguageStats mergers language names with different cases. The name with most upper case letters is used.
    19  func mergeLanguageStats(stats map[string]int64) map[string]int64 {
    20  	names := map[string]struct {
    21  		uniqueName string
    22  		upperCount int
    23  	}{}
    24  
    25  	countUpper := func(s string) (count int) {
    26  		for _, r := range s {
    27  			if unicode.IsUpper(r) {
    28  				count++
    29  			}
    30  		}
    31  		return count
    32  	}
    33  
    34  	for name := range stats {
    35  		cnt := countUpper(name)
    36  		lower := strings.ToLower(name)
    37  		if cnt >= names[lower].upperCount {
    38  			names[lower] = struct {
    39  				uniqueName string
    40  				upperCount int
    41  			}{uniqueName: name, upperCount: cnt}
    42  		}
    43  	}
    44  
    45  	res := make(map[string]int64, len(names))
    46  	for name, num := range stats {
    47  		res[names[strings.ToLower(name)].uniqueName] += num
    48  	}
    49  	return res
    50  }
    51  
    52  func TryReadLanguageAttribute(attrs map[string]string) optional.Option[string] {
    53  	language := AttributeToString(attrs, AttributeLinguistLanguage)
    54  	if language.Value() == "" {
    55  		language = AttributeToString(attrs, AttributeGitlabLanguage)
    56  		if language.Has() {
    57  			raw := language.Value()
    58  			// gitlab-language may have additional parameters after the language
    59  			// ignore them and just use the main language
    60  			// https://docs.gitlab.com/ee/user/project/highlighting.html#override-syntax-highlighting-for-a-file-type
    61  			if idx := strings.IndexByte(raw, '?'); idx >= 0 {
    62  				language = optional.Some(raw[:idx])
    63  			}
    64  		}
    65  	}
    66  	return language
    67  }