Sha256: d71ed1a46631599141e0707d16669e84d525964c28ef02a1fce51b28f49752ad

Contents?: true

Size: 1.46 KB

Versions: 25

Compression:

Stored size: 1.46 KB

Contents

module Govuk
  module Lint
    module Diff
      module EnabledLines
        def enabled_line?(line_number)
          return true unless processed_source

          super(line_number) &&
            Diff.changed_lines[processed_source.path].include?(line_number)
        end
      end

      module TargetFinder
        def find(args)
          super(args).select { |f| Diff.changed_lines.keys.include? f }
        end
      end

      def self.enable!(args)
        args.delete("--diff")
        @cached = true if args.delete("--cached")
        RuboCop::Cop::Cop.prepend EnabledLines
        RuboCop::TargetFinder.prepend TargetFinder
      end

      def self.changed_lines
        @changed_lines ||= begin
          changes = changed_files.map do |file|
            next unless File.exist?(file)
            [file, `git difftool #{commit_options} \
                    -y \
                    -x 'diff \
                      --new-line-format="%dn " \
                      --unchanged-line-format="" \
                      --changed-group-format="%>"' \
                    #{file}`.split.map(&:to_i)]
          end

          Hash[changes.compact].tap { |h| h.default = [] }
        end
      end

      def self.changed_files
        `git diff #{commit_options} --name-only`.
          split.
          map { |f| File.expand_path(f.chomp, "./") }
      end

      def self.commit_options
        @cached ? "--cached origin/master" : "origin/master HEAD"
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
govuk-lint-4.0.0 lib/govuk/lint/diff.rb
govuk-lint-3.11.5 lib/govuk/lint/diff.rb
govuk-lint-3.11.4 lib/govuk/lint/diff.rb
govuk-lint-3.11.3 lib/govuk/lint/diff.rb
govuk-lint-3.11.2 lib/govuk/lint/diff.rb
govuk-lint-3.11.1 lib/govuk/lint/diff.rb
govuk-lint-3.11.0 lib/govuk/lint/diff.rb
govuk-lint-3.10.0 lib/govuk/lint/diff.rb
govuk-lint-3.9.0 lib/govuk/lint/diff.rb
govuk-lint-3.8.0 lib/govuk/lint/diff.rb
govuk-lint-3.7.0 lib/govuk/lint/diff.rb
govuk-lint-3.6.0 lib/govuk/lint/diff.rb
govuk-lint-3.5.0 lib/govuk/lint/diff.rb
govuk-lint-3.4.0 lib/govuk/lint/diff.rb
govuk-lint-3.3.1 lib/govuk/lint/diff.rb
govuk-lint-3.3.0 lib/govuk/lint/diff.rb
govuk-lint-3.2.0 lib/govuk/lint/diff.rb
govuk-lint-3.1.0 lib/govuk/lint/diff.rb
govuk-lint-3.0.0 lib/govuk/lint/diff.rb
govuk-lint-2.1.0 lib/govuk/lint/diff.rb