Class: Redmine::UnifiedDiff

Inherits:
Array
  • Object
show all
Defined in:
lib/redmine/unified_diff.rb

Overview

Class used to parse unified diffs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringArrayDiff::Diffable

#diff, #patch, #replacenextlarger, #reverse_hash

Constructor Details

#initialize(diff, options = {}) ⇒ UnifiedDiff

Returns a new instance of UnifiedDiff.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/redmine/unified_diff.rb', line 25

def initialize(diff, options={})
  super()
  options.assert_valid_keys(:type, :style, :max_lines)
  diff = diff.split("\n") if diff.is_a?(String)
  @diff_type = options[:type] || 'inline'
  @diff_style = options[:style]
  # remove git footer
  if diff.length > 1 &&
       diff[-2] =~ /^--/ &&
       diff[-1] =~ /^[0-9]/
    diff.pop(2)
  end
  lines = 0
  @truncated = false
  diff_table = DiffTable.new(diff_type, diff_style)
  diff.each do |line_raw|
    line = Redmine::CodesetUtil.to_utf8_by_setting(line_raw)
    unless diff_table.add_line(line)
      self << diff_table if diff_table.length > 0
      diff_table = DiffTable.new(diff_type, diff_style)
    end
    lines += 1
    if options[:max_lines] && lines > options[:max_lines]
      @truncated = true
      break
    end
  end
  self << diff_table unless diff_table.empty?
  self
end

Instance Attribute Details

#diff_styleObject (readonly)

Returns the value of attribute diff_style.



23
24
25
# File 'lib/redmine/unified_diff.rb', line 23

def diff_style
  @diff_style
end

#diff_typeObject (readonly)

Returns the value of attribute diff_type.



23
24
25
# File 'lib/redmine/unified_diff.rb', line 23

def diff_type
  @diff_type
end

Instance Method Details

#truncated?Boolean

Returns:

  • (Boolean)


56
# File 'lib/redmine/unified_diff.rb', line 56

def truncated?; @truncated; end