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?, #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


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

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


35
36
37
# File 'lib/overcommit/hook/pre_commit/license_header.rb', line 35

def license_file
  config['license_file']
end

#license_linesObject


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

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

#runObject


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

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