Class: UnifiedDiff::Diff

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

Defined Under Namespace

Classes: UnifiedDiffException

Constant Summary collapse

FILE_PATTERN =
/([^\t\n]+)(?:\t'{2}?([^']+)'{2}?)?/
OLD_FILE_PATTERN =
/^--- #{FILE_PATTERN}/
NEW_FILE_PATTERN =
/^\+\+\+ #{FILE_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

/^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@/
ADDED_PATTERN =
/^\+(.*)/
REMOVED_PATTERN =
/^-(.*)/
UNCHANGED_PATTERN =
/^ (.*)/
NO_NEWLINE_PATTERN =
/^\\(\s+No\s+newline\s+at\s+end\s+of\s+file)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff) ⇒ Diff

Create and parse a unified diff

Parameters:

  • a (String)

    string containing a unified diff



33
34
35
36
# File 'lib/unified_diff/diff.rb', line 33

def initialize(diff)
  @original = diff
  parse
end

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



4
5
6
# File 'lib/unified_diff/diff.rb', line 4

def chunks
  @chunks
end

#modified_fileObject (readonly)

Returns the value of attribute modified_file.



4
5
6
# File 'lib/unified_diff/diff.rb', line 4

def modified_file
  @modified_file
end

#modified_timestampObject (readonly)

Returns the value of attribute modified_timestamp.



4
5
6
# File 'lib/unified_diff/diff.rb', line 4

def modified_timestamp
  @modified_timestamp
end

#originalObject (readonly)

Returns the value of attribute original.



4
5
6
# File 'lib/unified_diff/diff.rb', line 4

def original
  @original
end

#original_fileObject (readonly)

Returns the value of attribute original_file.



4
5
6
# File 'lib/unified_diff/diff.rb', line 4

def original_file
  @original_file
end

#original_timestampObject (readonly)

Returns the value of attribute original_timestamp.



4
5
6
# File 'lib/unified_diff/diff.rb', line 4

def original_timestamp
  @original_timestamp
end

Instance Method Details

#to_sString

Render the diff as it appeared originally

Returns:

  • (String)

    the original unified diff



41
42
43
# File 'lib/unified_diff/diff.rb', line 41

def to_s
  @original
end