Check committed file sizes

This commit is contained in:
2026-10-07 22:58:38 +00:00
parent eed95896f4
commit f80970a9d4
18 changed files with 460 additions and 6 deletions

View File

@@ -0,0 +1,52 @@
package validation
import (
"path"
"strings"
)
const defaultFileSizeLimit int64 = 1_000_000
func checkFileSize(tree *CandidateTree, settings *FileSizeSettings) error {
limit := defaultFileSizeLimit
if settings.LimitBytes != nil {
limit = *settings.LimitBytes
}
exclusions := normalizedExclusions(settings.Exclusions)
for _, entry := range tree.Entries() {
if entry.Type != "blob" || !entry.HasSize || excludedByPath(entry.Path, exclusions) {
continue
}
if entry.Size > limit {
return &Failure{
Check: FileSize,
Findings: []Finding{{
Path: entry.Path,
Message: "committed file exceeds the selected size limit",
}},
Measurement: &FileSizeMeasurement{Bytes: entry.Size, LimitBytes: limit},
}
}
}
return nil
}
func normalizedExclusions(exclusions []string) []string {
result := make([]string, 0, len(exclusions))
for _, exclusion := range exclusions {
if exclusion == "" {
continue
}
result = append(result, path.Clean(exclusion))
}
return result
}
func excludedByPath(filePath string, exclusions []string) bool {
for _, exclusion := range exclusions {
if exclusion == "." || filePath == exclusion || strings.HasPrefix(filePath, exclusion+"/") {
return true
}
}
return false
}