Sha256: c1c7360929cd8a2f81911d96f01fceb97d1f1ee80ea5d9bbf49defcf547fb3a1

Contents?: true

Size: 1.11 KB

Versions: 10

Compression:

Stored size: 1.11 KB

Contents

require 'pre-commit/checks/plugin'

module PreCommit
  module Checks
    class Grep < Plugin
      class PaternNotSet < StandardError
        def message
          "Please define 'pattern' method."
        end
      end

    # overwrite those:

      def files_filter(staged_files)
        staged_files
      end

      def extra_grep
        @extra_grep or ""
      end

      def message
        @message or ""
      end

      def pattern
        @pattern or raise PaternNotSet.new
      end

    # general code:

      def call(staged_files)
        staged_files = files_filter(staged_files)
        return if staged_files.empty?
        errors = `#{grep} #{pattern} #{staged_files.join(" ")}#{extra_grep}`
        return unless $?.success?
        "#{message}#{errors}"
      end

    private

      def grep(grep_version = nil)
        grep_version ||= detect_grep_version
        if grep_version =~ /FreeBSD/
          "grep -EnIH"
        else
          "grep -PnIH"
        end
      end

      def detect_grep_version
        `grep --version | head -n 1 | sed -e 's/^[^0-9.]*\([0-9.]*\)$/\1/'`
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pre-commit-0.18.0 lib/pre-commit/checks/grep.rb
pre-commit-0.17.0 lib/pre-commit/checks/grep.rb
pre-commit-0.16.3 lib/pre-commit/checks/grep.rb
pre-commit-0.16.2 lib/pre-commit/checks/grep.rb
pre-commit-0.16.1 lib/pre-commit/checks/grep.rb
pre-commit-0.16.0 lib/pre-commit/checks/grep.rb
pre-commit-0.15.0 lib/pre-commit/checks/grep.rb
pre-commit-0.14.1 lib/pre-commit/checks/grep.rb
pre-commit-0.14.0 lib/pre-commit/checks/grep.rb
pre-commit-0.13.0 lib/pre-commit/checks/grep.rb