Module: Daddy::Cucumber::Diff

Defined in:
lib/daddy/cucumber/helpers/diff.rb

Instance Method Summary collapse

Instance Method Details

#diff(file_a, file_b, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/daddy/cucumber/helpers/diff.rb', line 38

def diff(file_a, file_b, options = {})
  a = File.read(file_a).gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;')
  b = File.read(file_b).gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;')
  diff = format_diff(Differ.diff(a, b))

  show_filename(file_a, options)
  puts "<pre>#{diff}</pre>"
end

#git_diff(file, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/daddy/cucumber/helpers/diff.rb', line 7

def git_diff(file, options = {})
  options[:as] = 'edit' unless options.has_key?(:as)
  remote = options.has_key?(:remote) ? options[:remote] : true 

  git = Daddy::Git.new
  
  if options[:between]
    a = git.show_previous(file, :commit => options[:between], :remote => remote)
  else
    a = File.read(file)
  end
  a = a.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;')

  if options[:and]
    b = git.show_previous(file, :commit => options[:and], :remote => remote)
  else
    b = git.show_previous(file, :remote => remote)
  end
  b = b.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;')

  diff = format_diff(Differ.diff(a, b), options)

  show_filename(file, options)
  puts "<pre>#{diff}</pre>"
end

#git_diff_name(*includes) ⇒ Object



33
34
35
36
# File 'lib/daddy/cucumber/helpers/diff.rb', line 33

def git_diff_name(*includes)
  git = Daddy::Git.new
  puts '<pre>' + git.git_diff_name(*includes) + '</pre>'
end

#show(file, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/daddy/cucumber/helpers/diff.rb', line 47

def show(file, options = {})
  show_filename(file, options)

  if options[:commit].present?
    git = Daddy::Git.new
    content = git.show_previous(file, :commit => options[:commit], :remote => options[:remote])
    lines = content.split(/\r\n|\r|\n/).map{|line| line + "\n"}
  else
    lines = File.readlines(file)
  end

  if options[:from] and options[:to]
    from = options[:from] - 1
    to = options[:to] - 1
    lines = lines[from..to]
  elsif options[:from]
    from = options[:from] - 1
    lines = lines[from..-1]
  elsif options[:to]
    to = options[:to] - 1
    lines = lines[0..to]
  end

  lines = lines.join.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;')
  puts "<pre>#{lines}</pre>"

end