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 inherited from Array

#to_csv

Methods included from Diffable

#diff, #patch, #replacenextlarger, #reverse_hash

Constructor Details

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

Returns a new instance of UnifiedDiff.



23
24
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
# File 'lib/redmine/unified_diff.rb', line 23

def initialize(diff, options={})
  options.assert_valid_keys(:type, :max_lines)
  diff = diff.split("\n") if diff.is_a?(String)
  @diff_type = options[:type] || 'inline'
  lines = 0
  @truncated = false
  diff_table = DiffTable.new(@diff_type)
  diff.each do |line|
    line_encoding = nil
    if line.respond_to?(:force_encoding)
      line_encoding = line.encoding
      # TODO: UTF-16 and Japanese CP932 which is imcompatible with ASCII
      #       In Japan, diffrence between file path encoding
      #       and file contents encoding is popular.
      line.force_encoding('ASCII-8BIT')
    end
    unless diff_table.add_line line
      line.force_encoding(line_encoding) if line_encoding
      self << diff_table if diff_table.length > 0
      diff_table = DiffTable.new(diff_type)
    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_typeObject (readonly)

Returns the value of attribute diff_type.



21
22
23
# File 'lib/redmine/unified_diff.rb', line 21

def diff_type
  @diff_type
end

Instance Method Details

#truncated?Boolean

Returns:

  • (Boolean)


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

def truncated?; @truncated; end