Skip to content

Commit

Permalink
Keep languages defined in .gitattributes (#21403)
Browse files Browse the repository at this point in the history
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 <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
  • Loading branch information
4 people committed Oct 29, 2022
1 parent 434622a commit 49436c2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
35 changes: 25 additions & 10 deletions modules/git/repo_language_stats_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
checker, deferable := repo.CheckAttributeReader(commitID)
defer deferable()

// sizes contains the current calculated size of all files by language
sizes := make(map[string]int64)
// by default we will only count the sizes of programming languages or markup languages
// unless they are explicitly set using linguist-language
includedLanguage := map[string]bool{}
// or if there's only one language in the repository
firstExcludedLanguage := ""
firstExcludedLanguageSize := int64(0)

err = tree.Files().ForEach(func(f *object.File) error {
if f.Size == 0 {
return nil
Expand Down Expand Up @@ -75,8 +83,8 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
language = group
}

// this language will always be added to the size
sizes[language] += f.Size

return nil
} else if language, has := attrs["gitlab-language"]; has && language != "unspecified" && language != "" {
// strip off a ? if present
Expand All @@ -90,6 +98,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
language = group
}

// this language will always be added to the size
sizes[language] += f.Size
return nil
}
Expand Down Expand Up @@ -124,22 +133,28 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
language = group
}

sizes[language] += f.Size
included, checked := includedLanguage[language]
if !checked {
langtype := enry.GetLanguageType(language)
included = langtype == enry.Programming || langtype == enry.Markup
includedLanguage[language] = included
}
if included {
sizes[language] += f.Size
} else if len(sizes) == 0 && (firstExcludedLanguage == "" || firstExcludedLanguage == language) {
firstExcludedLanguage = language
firstExcludedLanguageSize += f.Size
}

return nil
})
if err != nil {
return nil, err
}

// filter special languages unless they are the only language
if len(sizes) > 1 {
for language := range sizes {
langtype := enry.GetLanguageType(language)
if langtype != enry.Programming && langtype != enry.Markup {
delete(sizes, language)
}
}
// If there are no included languages add the first excluded language
if len(sizes) == 0 && firstExcludedLanguage != "" {
sizes[firstExcludedLanguage] = firstExcludedLanguageSize
}

return sizes, nil
Expand Down
35 changes: 26 additions & 9 deletions modules/git/repo_language_stats_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err

contentBuf := bytes.Buffer{}
var content []byte

// sizes contains the current calculated size of all files by language
sizes := make(map[string]int64)
// by default we will only count the sizes of programming languages or markup languages
// unless they are explicitly set using linguist-language
includedLanguage := map[string]bool{}
// or if there's only one language in the repository
firstExcludedLanguage := ""
firstExcludedLanguageSize := int64(0)

for _, f := range entries {
select {
case <-repo.Ctx.Done():
Expand Down Expand Up @@ -107,6 +116,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
language = group
}

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

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

sizes[language] += f.Size()
included, checked := includedLanguage[language]
if !checked {
langtype := enry.GetLanguageType(language)
included = langtype == enry.Programming || langtype == enry.Markup
includedLanguage[language] = included
}
if included {
sizes[language] += f.Size()
} else if len(sizes) == 0 && (firstExcludedLanguage == "" || firstExcludedLanguage == language) {
firstExcludedLanguage = language
firstExcludedLanguageSize += f.Size()
}
continue
}

// filter special languages unless they are the only language
if len(sizes) > 1 {
for language := range sizes {
langtype := enry.GetLanguageType(language)
if langtype != enry.Programming && langtype != enry.Markup {
delete(sizes, language)
}
}
// If there are no included languages add the first excluded language
if len(sizes) == 0 && firstExcludedLanguage != "" {
sizes[firstExcludedLanguage] = firstExcludedLanguageSize
}

return sizes, nil
Expand Down

0 comments on commit 49436c2

Please sign in to comment.