summaryrefslogtreecommitdiffstats
path: root/modules/git/repo_language_stats.go
diff options
context:
space:
mode:
authorCirno the Strongest <1447794+CirnoT@users.noreply.github.com>2020-05-31 00:58:55 +0200
committerGitHub <noreply@github.com>2020-05-31 01:58:55 +0300
commit9d652002c63d03d44083c4410881a457a9390e2f (patch)
tree6e3d7c37add64a2d4e51f2b3a930ee16ec0c5533 /modules/git/repo_language_stats.go
parentea4c139cd2f7e5174627a40aa8a9973fabf508ff (diff)
downloadgitea-9d652002c63d03d44083c4410881a457a9390e2f.tar.gz
gitea-9d652002c63d03d44083c4410881a457a9390e2f.zip
Fix language stat calculation (#11692)
* Fix language stat calculation * Group languages and ignore 0 size files * remove unneeded code
Diffstat (limited to 'modules/git/repo_language_stats.go')
-rw-r--r--modules/git/repo_language_stats.go31
1 files changed, 27 insertions, 4 deletions
diff --git a/modules/git/repo_language_stats.go b/modules/git/repo_language_stats.go
index d623d6f57d..06d7d6aba0 100644
--- a/modules/git/repo_language_stats.go
+++ b/modules/git/repo_language_stats.go
@@ -19,6 +19,20 @@ import (
const fileSizeLimit int64 = 16 * 1024 * 1024
+// specialLanguages defines list of languages that are excluded from the calculation
+// unless they are the only language present in repository. Only languages which under
+// normal circumstances are not considered to be code should be listed here.
+var specialLanguages = []string{
+ "XML",
+ "JSON",
+ "TOML",
+ "YAML",
+ "INI",
+ "SVG",
+ "Text",
+ "Markdown",
+}
+
// GetLanguageStats calculates language stats for git repository at specified commit
func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, error) {
r, err := git.PlainOpen(repo.Path)
@@ -43,7 +57,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
sizes := make(map[string]int64)
err = tree.Files().ForEach(func(f *object.File) error {
- if enry.IsVendor(f.Name) || enry.IsDotFile(f.Name) ||
+ if f.Size == 0 || enry.IsVendor(f.Name) || enry.IsDotFile(f.Name) ||
enry.IsDocumentation(f.Name) || enry.IsConfiguration(f.Name) {
return nil
}
@@ -58,7 +72,13 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
language := analyze.GetCodeLanguage(f.Name, content)
if language == enry.OtherLanguage || language == "" {
- language = "other"
+ return nil
+ }
+
+ // group languages, such as Pug -> HTML; SCSS -> CSS
+ group := enry.GetLanguageGroup(language)
+ if group != "" {
+ language = group
}
sizes[language] += f.Size
@@ -69,8 +89,11 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
return nil, err
}
- if len(sizes) == 0 {
- sizes["other"] = 0
+ // filter special languages unless they are the only language
+ if len(sizes) > 1 {
+ for _, language := range specialLanguages {
+ delete(sizes, language)
+ }
}
return sizes, nil