forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense_header.rb
46 lines (39 loc) · 1.12 KB
/
license_header.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
module Overcommit::Hook::PreCommit
# Checks for license headers in source files
class LicenseHeader < Base
def run
begin
license_contents = license_lines
rescue Errno::ENOENT
return :fail, "Unable to load license file #{license_file}"
end
messages = applicable_files.map do |file|
check_file(file, license_contents)
end.compact
return :fail, messages.join("\n") if messages.any?
:pass
end
def check_file(file, license_contents)
File.readlines(file).each_with_index do |l, i|
if i >= license_contents.length
break
end
l.chomp!
unless l.end_with?(license_contents[i])
message = "#{file} missing header contents from line #{i} of "\
"#{license_file}: #{license_contents[i]}"
return message
end
end
end
def license_file
config['license_file']
end
def license_lines
@license_regex ||= begin
file_root = Overcommit::Utils.convert_glob_to_absolute(license_file)
File.read(file_root).split("\n")
end
end
end
end