53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
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
|
|
}
|