forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_size.rb
67 lines (54 loc) · 1.36 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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_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,
error_text_for(file)
)
end
def error_text_for(file)
error_text_format % {
path: relative_path_for(file),
limit: size_limit_bytes,
size: file_size(file)
}
end
def error_text_format
'%<path>s is over the file size limit of %<limit>s bytes (is %<size>s bytes)'
end
def file_size(file)
File.size(file)
end
def relative_path_for(file)
Pathname.new(file).relative_path_from(repo_root_path)
end
def repo_root_path
@repo_root_path ||= Pathname.new(Overcommit::Utils.repo_root)
end
end
end