class Overcommit::Hook::PreCommit::LicenseHeader

Checks for license headers in source files

Public Instance Methods

check_file(file, license_contents) click to toggle source
# 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_file() click to toggle source
# File lib/overcommit/hook/pre_commit/license_header.rb, line 37
def license_file
  config['license_file']
end
license_lines() click to toggle source
# 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
run() click to toggle source
# 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