Skip to content

Commit 49436c2

Browse files
KN4CK3Rzeripathlafrikslunny
authored
Keep languages defined in .gitattributes (#21403)
Fixes #21377 This marks all "defined" languages in the `.gitattributes` file so they are not removed if they are not of type `programming` or `markup`. ![grafik](https://user-images.githubusercontent.com/1666336/194942021-1e641b60-bb8a-49c6-9a1c-413e7c4ba17d.png) Co-authored-by: zeripath <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 434622a commit 49436c2

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

modules/git/repo_language_stats_gogit.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

modules/git/repo_language_stats_nogogit.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
6767

6868
contentBuf := bytes.Buffer{}
6969
var content []byte
70+
71+
// sizes contains the current calculated size of all files by language
7072
sizes := make(map[string]int64)
73+
// by default we will only count the sizes of programming languages or markup languages
74+
// unless they are explicitly set using linguist-language
75+
includedLanguage := map[string]bool{}
76+
// or if there's only one language in the repository
77+
firstExcludedLanguage := ""
78+
firstExcludedLanguageSize := int64(0)
79+
7180
for _, f := range entries {
7281
select {
7382
case <-repo.Ctx.Done():
@@ -107,6 +116,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
107116
language = group
108117
}
109118

119+
// this language will always be added to the size
110120
sizes[language] += f.Size()
111121
continue
112122
} else if language, has := attrs["gitlab-language"]; has && language != "unspecified" && language != "" {
@@ -121,6 +131,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
121131
language = group
122132
}
123133

134+
// this language will always be added to the size
124135
sizes[language] += f.Size()
125136
continue
126137
}
@@ -180,18 +191,24 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
180191
language = group
181192
}
182193

183-
sizes[language] += f.Size()
194+
included, checked := includedLanguage[language]
195+
if !checked {
196+
langtype := enry.GetLanguageType(language)
197+
included = langtype == enry.Programming || langtype == enry.Markup
198+
includedLanguage[language] = included
199+
}
200+
if included {
201+
sizes[language] += f.Size()
202+
} else if len(sizes) == 0 && (firstExcludedLanguage == "" || firstExcludedLanguage == language) {
203+
firstExcludedLanguage = language
204+
firstExcludedLanguageSize += f.Size()
205+
}
184206
continue
185207
}
186208

187-
// filter special languages unless they are the only language
188-
if len(sizes) > 1 {
189-
for language := range sizes {
190-
langtype := enry.GetLanguageType(language)
191-
if langtype != enry.Programming && langtype != enry.Markup {
192-
delete(sizes, language)
193-
}
194-
}
209+
// If there are no included languages add the first excluded language
210+
if len(sizes) == 0 && firstExcludedLanguage != "" {
211+
sizes[firstExcludedLanguage] = firstExcludedLanguageSize
195212
}
196213

197214
return sizes, nil

0 commit comments

Comments
 (0)