Class: Shiba::Review::ExplainDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/shiba/review/explain_diff.rb

Overview

Given an explain log and a diff, returns any explain logs that appear to be caused by the diff.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, options) ⇒ ExplainDiff

Returns a new instance of ExplainDiff.



16
17
18
19
# File 'lib/shiba/review/explain_diff.rb', line 16

def initialize(log, options)
  @log     = log
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/shiba/review/explain_diff.rb', line 14

def options
  @options
end

Instance Method Details

#diff_requested_by_user?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/shiba/review/explain_diff.rb', line 21

def diff_requested_by_user?
  [ "staged", "unstaged", "branch", "diff" ].any? { |key| @options.key?(key) }
end

#problemsObject

Returns detected problem queries with their line numbers. Query problem format is [ [ “path:lineno”, explain ]… ]



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/shiba/review/explain_diff.rb', line 27

def problems
  return @problems if @problems

  @problems = explains_with_backtrace_in_diff.select do |explain|
    explain["severity"] && explain["severity"] != 'none'
  end

  if options["verbose"]
    $stderr.puts @problems
    $stderr.puts "Updated lines: #{updated_lines}"
  end

  @problems.map! do |problem|
    line = diff_line_from_backtrace(problem["backtrace"])
    next if line.nil?

    [ line, problem ]
  end
  @problems.compact!

  @problems
end

#resultObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shiba/review/explain_diff.rb', line 50

def result
  msg = nil

  if changed_files.empty?
    if options['verbose']
      msg = "No changes found. Are you sure you specified the correct branch?"
    end
    return Result.new(:pass, msg)
  end

  if problems.empty?
    msg = "No problems found caused by the diff"
    return Result.new(:pass, msg)
  end

  return Result.new(:fail, "Potential problems")
end