Module: Govuk::Lint::Diff

Defined in:
lib/govuk/lint/diff.rb

Defined Under Namespace

Modules: EnabledLines, TargetFinder

Class Method Summary collapse

Class Method Details

.changed_filesObject



44
45
46
47
48
# File 'lib/govuk/lint/diff.rb', line 44

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

.changed_linesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/govuk/lint/diff.rb', line 26

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

.commit_optionsObject



50
51
52
# File 'lib/govuk/lint/diff.rb', line 50

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

.enable!(args) ⇒ Object



19
20
21
22
23
24
# File 'lib/govuk/lint/diff.rb', line 19

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