class Nucop::Formatters::GitDiffFormatter

Public Class Methods

new(output, options = {}) click to toggle source
Calls superclass method
# File lib/nucop/formatters/git_diff_formatter.rb, line 4
def initialize(output, options = {})
  super

  populate_history_from_git
  @offenses_per_file = {}
end

Public Instance Methods

file_finished(file, offenses) click to toggle source
# File lib/nucop/formatters/git_diff_formatter.rb, line 11
def file_finished(file, offenses)
  return unless file_touched?(file)

  offenses_in_changes = offenses_from_git_history(file, offenses)
  @offenses_per_file[file] = offenses_in_changes.size

  # modify parent Formatter to print what we want
  @offenses_for_files[file] = offenses_in_changes if offenses_in_changes.any?
  report_file_as_mark(offenses_in_changes)
end
finished(_inspected_files) click to toggle source
Calls superclass method
# File lib/nucop/formatters/git_diff_formatter.rb, line 22
def finished(_inspected_files)
  @total_offense_count = @offenses_per_file.values.reduce(0, :+)
  @total_correction_count = 0

  super
end

Private Instance Methods

file_touched?(file) click to toggle source
# File lib/nucop/formatters/git_diff_formatter.rb, line 43
def file_touched?(file)
  @git_history.key?(file)
end
offenses_from_git_history(file, offenses) click to toggle source
# File lib/nucop/formatters/git_diff_formatter.rb, line 47
def offenses_from_git_history(file, offenses)
  offenses.select { |offense| @git_history[file].include?(offense.line) }
end
populate_history_from_git() click to toggle source
# File lib/nucop/formatters/git_diff_formatter.rb, line 31
def populate_history_from_git
  commit_spec = ENV["RUBOCOP_COMMIT_SPEC"] || "main"

  diff = `git --no-pager diff #{commit_spec}`

  @git_history = ::GitDiffParser.parse(diff).each_with_object({}) do |patch, acc|
    next if patch.changed_line_numbers.empty?

    acc[File.expand_path(patch.file)] = patch.changed_line_numbers
  end
end