@@ -44,7 +44,15 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
44
44
checker , deferable := repo .CheckAttributeReader (commitID )
45
45
defer deferable ()
46
46
47
+ // sizes contains the current calculated size of all files by language
47
48
sizes := make (map [string ]int64 )
49
+ // by default we will only count the sizes of programming languages or markup languages
50
+ // unless they are explicitly set using linguist-language
51
+ includedLanguage := map [string ]bool {}
52
+ // or if there's only one language in the repository
53
+ firstExcludedLanguage := ""
54
+ firstExcludedLanguageSize := int64 (0 )
55
+
48
56
err = tree .Files ().ForEach (func (f * object.File ) error {
49
57
if f .Size == 0 {
50
58
return nil
@@ -75,8 +83,8 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
75
83
language = group
76
84
}
77
85
86
+ // this language will always be added to the size
78
87
sizes [language ] += f .Size
79
-
80
88
return nil
81
89
} else if language , has := attrs ["gitlab-language" ]; has && language != "unspecified" && language != "" {
82
90
// strip off a ? if present
@@ -90,6 +98,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
90
98
language = group
91
99
}
92
100
101
+ // this language will always be added to the size
93
102
sizes [language ] += f .Size
94
103
return nil
95
104
}
@@ -124,22 +133,28 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
124
133
language = group
125
134
}
126
135
127
- sizes [language ] += f .Size
136
+ included , checked := includedLanguage [language ]
137
+ if ! checked {
138
+ langtype := enry .GetLanguageType (language )
139
+ included = langtype == enry .Programming || langtype == enry .Markup
140
+ includedLanguage [language ] = included
141
+ }
142
+ if included {
143
+ sizes [language ] += f .Size
144
+ } else if len (sizes ) == 0 && (firstExcludedLanguage == "" || firstExcludedLanguage == language ) {
145
+ firstExcludedLanguage = language
146
+ firstExcludedLanguageSize += f .Size
147
+ }
128
148
129
149
return nil
130
150
})
131
151
if err != nil {
132
152
return nil , err
133
153
}
134
154
135
- // filter special languages unless they are the only language
136
- if len (sizes ) > 1 {
137
- for language := range sizes {
138
- langtype := enry .GetLanguageType (language )
139
- if langtype != enry .Programming && langtype != enry .Markup {
140
- delete (sizes , language )
141
- }
142
- }
155
+ // If there are no included languages add the first excluded language
156
+ if len (sizes ) == 0 && firstExcludedLanguage != "" {
157
+ sizes [firstExcludedLanguage ] = firstExcludedLanguageSize
143
158
}
144
159
145
160
return sizes , nil
0 commit comments