Class: Origen::Utility::Diff

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

Overview

Diff provides an easy way to diff the contents of two files while optionally ignoring any differences in file comments.

differ = Origen::Utility::Diff.new(:ignore_blank_lines => true, :comment_char => "//")

differ.file_a = "#{Origen.root}/my/file1.v"
differ.file_b = "#{Origen.root}/my/file2.v"

if differ.diffs?
  puts "You've changed something!"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Diff

Create a new diff, attributes can be initialized via the options, or can be set later.



28
29
30
31
32
33
34
35
36
37
# File 'lib/origen/utility/diff.rb', line 28

def initialize(options = {})
  @file_a = options[:file_a]
  @file_b = options[:file_b]
  @ignore_blank_lines = options[:ignore_blank_lines]
  @comment_char = options[:comment_char]
  @suspend_string = options[:suspend_string] # permits suspending diff check based on a string
  @resume_string  = options[:resume_string]  # permits resuming diff check based on a string
  @suspend_diff = false
  @resume_diff = false
end

Instance Attribute Details

#comment_charObject

Set this attribute to the comment char used by the given file and comments will be ignored by the diff. An array of strings can be passed in to mask multiple comment identifiers.



24
25
26
# File 'lib/origen/utility/diff.rb', line 24

def comment_char
  @comment_char
end

#file_aObject

Full path to File A, this attribute must be set before calling any diff actions



16
17
18
# File 'lib/origen/utility/diff.rb', line 16

def file_a
  @file_a
end

#file_bObject

Full path to File B, this attribute must be set before calling any diff actions



18
19
20
# File 'lib/origen/utility/diff.rb', line 18

def file_b
  @file_b
end

#ignore_blank_linesObject

When true the diff will ignore blank lines, or lines that contain only whitespace



20
21
22
# File 'lib/origen/utility/diff.rb', line 20

def ignore_blank_lines
  @ignore_blank_lines
end

Instance Method Details

#diffs?Boolean

Returns true if there are differences between the two files based on the current configuration

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/origen/utility/diff.rb', line 41

def diffs?
  initialize_counters
  result = false
  content_a = File.readlines(@file_a)
  content_b = File.readlines(@file_b)

  changes = false
  lines_remaining = true

  while lines_remaining
    a = get_next_line_a(content_a) # Get the next vectors
    b = get_next_line_b(content_b)
    if !a && !b       # If both patterns finished
      lines_remaining = false
    elsif !a || !b    # If only 1 pattern finished
      lines_remaining = false
      changes = true unless @suspend_diff # There are extra vectors in one of the patterns
    elsif a != b # If the vectors don't match
      changes = true unless @suspend_diff
    end
    if @resume_diff # resume checking diffs for subsequent lines
      @suspend_diff = false
      @resume_diff = false
    end
  end

  changes
end