class UnifiedDiff::Diff

Constants

ADDED_PATTERN
CHUNK_PATTERN

Match assignment is tricky for CHUNK_PATTERN $1 and $3 are static, but $2 and $4 can be nil

“In many versions of GNU diff, each range can omit the comma and

trailing value s, in which case s defaults to 1. Note that the 
only really interesting value is the l line number of the first 
range; all the other values can be computed from the diff." 
    -- http://en.wikipedia.org/wiki/Diff#Unified_format

Pattern -W,X +Y,Z has $1 = W, $2 = X, $3 = Y, $4 = Z Pattern -W +Y,Z has $1 = W, $2 = nil, $3 = Y, $4 = Z Pattern -W +Y has $1 = W, $2 = nil, $3 = Y, $4 = nil Pattern -W,X +Y has $1 = W, $2 = X, $3 = Y, $4 = nil

FILE_PATTERN
NEW_FILE_PATTERN
NO_NEWLINE_PATTERN
OLD_FILE_PATTERN
REMOVED_PATTERN
UNCHANGED_PATTERN

Attributes

chunks[R]
modified_file[R]
modified_timestamp[R]
original[R]
original_file[R]
original_timestamp[R]

Public Class Methods

new(diff) click to toggle source

Create and parse a unified diff

@param [String] a string containing a unified diff @return [Diff] the parsed diff

# File lib/unified_diff/diff.rb, line 33
def initialize(diff)
  @original = diff
  parse
end

Public Instance Methods

to_s() click to toggle source

Render the diff as it appeared originally

@return [String] the original unified diff

# File lib/unified_diff/diff.rb, line 41
def to_s
  @original
end

Private Instance Methods

parse() click to toggle source
# File lib/unified_diff/diff.rb, line 47
def parse
  @chunks = []
  @original.each_line do |line|
    case line
    when OLD_FILE_PATTERN
      @original_file = $1
      @original_timestamp = Time.parse($2) if $2
    when NEW_FILE_PATTERN
      @modified_file = $1
      @modified_timestamp = Time.parse($2) if $2
    when CHUNK_PATTERN
      old_begin = $1.to_i
      if $2.nil?
        old_end = old_begin + 1
      else
        old_end = old_begin + $2.to_i
      end
      new_begin = $3.to_i
      if $4.nil?
        new_end = new_begin+1
      else
        new_end = new_begin + $4.to_i
      end
      @working_chunk = Chunk.new(original: (old_begin...old_end), modified: (new_begin...new_end))
      @chunks << @working_chunk
    when ADDED_PATTERN
      @working_chunk.send(:insert_addition, $1)
    when REMOVED_PATTERN
      @working_chunk.send(:insert_removal, $1)
    when UNCHANGED_PATTERN
      @working_chunk.send(:insert_unchanged, $1)
    when NO_NEWLINE_PATTERN
      @working_chunk.send(:insert_no_newline_at_eof, $1)
    else
      raise UnifiedDiffException.new("Unknown Line Type for Line:\n#{line}")
    end
  end
end