Sha256: 2d5f210412d99f1ecb6cb90fdace9ace911deabbc491abb56f7f8e0f3bee8378

Contents?: true

Size: 1.93 KB

Versions: 19

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

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)
        begin
          messages += check_file(file, file_name)
        rescue ArgumentError => ex
          # File is likely a binary file which this check should ignore, but
          # print a warning just in case
          messages << Overcommit::Hook::Message.new(
            :warning,
            file_name,
            file.lineno,
            "#{file_name}:#{file.lineno}:#{ex.message}"
          )
        end
      end

      messages
    end

    private

    def check_file(file, file_name)
      messages_for_file = []

      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_for_file << Overcommit::Hook::Message.new(
          :error,
          file_name,
          file.lineno,
          "#{file_name}:#{file.lineno}:#{line.inspect}"
        )
      end

      messages_for_file
    end

    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|
        info, path = file_info.split("\t")
        i = info.split.first
        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

19 entries across 19 versions & 2 rubygems

Version Path
overcommit-0.58.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.57.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.56.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.55.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.54.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.54.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-jeygeethanmedia-0.53.1.2 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-jeygeethanmedia-0.53.1.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-jeygeethanmedia-0.53.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.53.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.52.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.52.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.51.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.50.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.49.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.49.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.48.1 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.48.0 lib/overcommit/hook/pre_commit/line_endings.rb
overcommit-0.47.0 lib/overcommit/hook/pre_commit/line_endings.rb