forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_size.rb
47 lines (38 loc) · 937 Bytes
/
file_size.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Checks for oversized files before committing.
class FileSize < Base
def run
return :pass if oversized_files.empty?
oversized_files.map do |file|
error_message_for(file)
end
end
def description
"Check for files over #{size_limit_bytes} bytes"
end
private
def oversized_files
@oversized_files ||= build_oversized_file_list
end
def build_oversized_file_list
applicable_files.select do |file|
File.exist?(file) && file_size(file) > size_limit_bytes
end
end
def size_limit_bytes
config.fetch('size_limit_bytes')
end
def error_message_for(file)
Overcommit::Hook::Message.new(
:error,
file,
nil,
"#{file} is #{file_size(file)} bytes"
)
end
def file_size(file)
File.size(file)
end
end
end