Sha256: bfb9268161df004af757f350bb0ec1b00621ecc2c44aca05ec39ae4d9d5a6ef7

Contents?: true

Size: 1.12 KB

Versions: 11

Compression:

Stored size: 1.12 KB

Contents

require 'yaml'
require 'pre-commit/checks/plugin'

module PreCommit
  module Checks
    class Yaml < Plugin
      def call(staged_files)
        staged_files = staged_files.grep(/\.(yml|yaml)$/)
        return if staged_files.empty?

        errors = staged_files.map {|file| load_file(file)}.compact

        errors.join("\n") + "\n" unless errors.empty?
      end

      def load_file(file)
        if YAML.respond_to?(:safe_load)
          safe_load_file(file)
        else
          normal_load_file(file)
        end

      rescue Psych::SyntaxError => e
        e.message
      end

      def safe_load_file(file)
        YAML.safe_load(File.read(file), [::Symbol], [], true, file)

        nil
      rescue Psych::DisallowedClass
        $stdout.puts "Warning: Skipping '#{file}' because it contains serialized ruby objects."
      end

      def normal_load_file(file)
        YAML.load_file(file)

        nil
      rescue ArgumentError
        $stdout.puts "Warning: Skipping '#{file}' because it contains serialized ruby objects."
      end

      def self.description
        'Runs yaml to detect errors.'
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pre-commit-0.37.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.36.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.35.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.34.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.33.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.32.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.31.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.30.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.29.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.28.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.27.0 lib/plugins/pre_commit/checks/yaml.rb