module DirtyCop

require 'pry'

Public Instance Methods

bury_evidence?(file, line) click to toggle source
# File lib/dirty/cop.rb, line 46
def bury_evidence?(file, line)
  !report_offense_at?(file, line)
end
changed_files(ref) click to toggle source
# File lib/dirty/cop.rb, line 104
def changed_files(ref)
  @changed_files ||= git_diff_name_only
    .lines
    .map(&:chomp)
    .grep(/\.rb$/)
    .map { |file| File.absolute_path(file) }
end
changed_files_and_lines(ref) click to toggle source
# File lib/dirty/cop.rb, line 116
def changed_files_and_lines(ref)
  result = {}

  changed_files(ref).each do |file|
    result[file] = changed_lines(file, ref)
  end

  result
end
changed_lines(file, ref) click to toggle source
# File lib/dirty/cop.rb, line 130
def changed_lines(file, ref)
  ranges = git_diff(file, ref)
    .each_line
    .grep(/@@ -(\d+)(?:,)?(\d+)? \+(\d+)(?:,)?(\d+)? @@/) {
      [
        Regexp.last_match[3].to_i,
        (Regexp.last_match[4] || 1).to_i
      ]
    }.reverse

  mask = Set.new

  ranges.each do |changed_line_number, number_of_changed_lines|
    number_of_changed_lines.times do |line_delta|
      mask << changed_line_number + line_delta
    end
  end

  mask.to_a
end
changed_lines_for_file(file) click to toggle source
# File lib/dirty/cop.rb, line 100
def changed_lines_for_file(file)
  changed_files_and_lines(ref)[file] || []
end
cover_up_unmodified(ref, only_changed_lines = true) click to toggle source
# File lib/dirty/cop.rb, line 60
def cover_up_unmodified(ref, only_changed_lines = true)
  @line_filter ||= changed_files_and_lines(ref) if only_changed_lines
end
eat_a_donut() click to toggle source
# File lib/dirty/cop.rb, line 151
def eat_a_donut
  puts "#{$PROGRAM_NAME}: The dirty cop Alex Murphy could have been"
  puts
  puts File.read(__FILE__)[/(?:^#(?:[^!].*)?\n)+/s].gsub(/^#/, '   ')
  exit
end
files_to_inspect(whitelisted_files, args) click to toggle source
# File lib/dirty/cop.rb, line 54
def files_to_inspect(whitelisted_files, args)
  return @files ||= args unless args.empty?

  @files ||= (changed_files(ref) & whitelisted_files)
end
git_diff(file, ref) click to toggle source
# File lib/dirty/cop.rb, line 126
def git_diff(file, ref)
  `git diff -p -U0 #{ref} #{file}`
end
git_diff_name_only() click to toggle source
# File lib/dirty/cop.rb, line 112
def git_diff_name_only
  `git diff --diff-filter=AM --name-only #{ref}`
end
process_bribe() click to toggle source
# File lib/dirty/cop.rb, line 64
def process_bribe
  # leaving this unused method as a placeholder of flags that purportedly work
  # (potential feature set)
  only_changed_lines = true

  # I am specifying the ref above instead of getting it from args since
  # getting it from args seems to be broken
  # ref = nil
  # loop do
  #   arg = ARGV.shift
  #   case arg
  #   when '--local'
  #     ref = `git rev-parse --abbrev-ref --symbolic-full-name @{u}`.chomp
  #     exit 1 unless $?.success?
  #   when '--against'
  #     ref = ARGV.shift
  #   when '--uncommitted', '--index'
  #     ref = 'HEAD'
  #   when '--branch'
  #     ref = `git merge-base HEAD master`.chomp
  #   when '--courage'
  #     only_changed_lines = false
  #   else
  #     ARGV.unshift arg
  #     break
  #   end
  # end
  # return unless ref

  cover_up_unmodified ref, only_changed_lines
end
ref() click to toggle source
# File lib/dirty/cop.rb, line 50
def ref
  'HEAD'
end
report_offense_at?(file, line) click to toggle source
# File lib/dirty/cop.rb, line 96
def report_offense_at?(file, line)
  changed_lines_for_file(file).include? line
end