Class: Overcommit::Hook::PreCommit::LicenseHeader

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/license_header.rb

Overview

Checks for license headers in source files

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#check_file(file, license_contents) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/overcommit/hook/pre_commit/license_header.rb', line 22

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

#license_fileObject



37
38
39
# File 'lib/overcommit/hook/pre_commit/license_header.rb', line 37

def license_file
  config['license_file']
end

#license_linesObject



41
42
43
44
45
46
# File 'lib/overcommit/hook/pre_commit/license_header.rb', line 41

def license_lines
  @license_lines ||= begin
    file_root = Overcommit::Utils.convert_glob_to_absolute(license_file)
    File.read(file_root).split("\n")
  end
end

#runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/overcommit/hook/pre_commit/license_header.rb', line 6

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