@@ -44,7 +44,15 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
4444 checker , deferable := repo .CheckAttributeReader (commitID )
4545 defer deferable ()
4646
47+ // sizes contains the current calculated size of all files by language
4748 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+
4856 err = tree .Files ().ForEach (func (f * object.File ) error {
4957 if f .Size == 0 {
5058 return nil
@@ -75,8 +83,8 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
7583 language = group
7684 }
7785
86+ // this language will always be added to the size
7887 sizes [language ] += f .Size
79-
8088 return nil
8189 } else if language , has := attrs ["gitlab-language" ]; has && language != "unspecified" && language != "" {
8290 // strip off a ? if present
@@ -90,6 +98,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
9098 language = group
9199 }
92100
101+ // this language will always be added to the size
93102 sizes [language ] += f .Size
94103 return nil
95104 }
@@ -124,22 +133,28 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
124133 language = group
125134 }
126135
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+ }
128148
129149 return nil
130150 })
131151 if err != nil {
132152 return nil , err
133153 }
134154
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
143158 }
144159
145160 return sizes , nil
0 commit comments