Sha256: 3a00307dd936623e183f5db968b3fe884c0933cac8add529778cbc258a5325f6

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module Overcommit::Hook::PreCommit
  # Checks for line endings in files.
  #
  # WARNING: Works with Git 2.10.0 or newer.
  class LineEndings < Base
    def run
      messages = []

      offending_files.map do |file_name|
        file = File.open(file_name)
        file.each_line do |line|
          # remove configured line-ending
          line.gsub!(/#{config['eol']}/, '')

          # detect any left over line-ending characters
          next unless line.end_with?("\n", "\r")

          messages << Overcommit::Hook::Message.new(
            :error,
            file_name,
            file.lineno,
            "#{file_name}:#{file.lineno}:#{line.inspect}"
          )
        end
      end

      messages
    end

    private

    def offending_files
      result = execute(%w[git ls-files --eol -z --], args: applicable_files)
      raise 'Unable to access git tree' unless result.success?

      result.stdout.split("\0").map do |file_info|
        i, _w, _attr, path = file_info.split
        next if i == 'l/-text' # ignore binary files
        next if i == "l/#{eol}"
        path
      end.compact
    end

    def eol
      @eol ||=  case config['eol']
                when "\n"
                  'lf'
                when "\r\n"
                  'crlf'
                else
                  raise 'Invalid `eol` option specified: must be "\n" or "\r\n"'
                end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
overcommit-0.39.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.39.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.38.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.37.0 lib/overcommit/hook/pre_commit/line_endings.rb