git-pre-commit-hook/check_largefiles.go

33 lines
581 B
Go
Raw Permalink Normal View History

2020-02-16 09:48:17 +00:00
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
)
const (
largeFileLimit = 1 << 20 // 1MiB
)
func checkLargeFiles() error {
return filepath.Walk(".", checkLargeFilesW)
}
func checkLargeFilesW(path string, info os.FileInfo, err error) error {
switch {
case err != nil:
return err
case info.Size() > largeFileLimit:
return fmt.Errorf("committing large file: %s (%d bytes)",
aurora.Red(path), info.Size())
case info.IsDir() && strings.HasPrefix(info.Name(), "."):
return filepath.SkipDir
default:
return nil
}
}