Class: Grit::Status::StatusFile

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/status.rb

Instance Method Summary collapse

Instance Method Details

#diff_stringObject



4
5
6
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
32
33
# File 'lib/grit/status.rb', line 4

def diff_string
  old = @base.object(self.sha_repo)
  new = @base.object(self.sha_index)
  data_old = old.content.split(/\n/).map! { |e| e.chomp }
  data_new = new.content.split(/\n/).map! { |e| e.chomp }
  diffs = Difference::LCS.diff(data_old, data_new)
  file_length_difference = 0
  lines = 3
  oldhunk = hunk = nil
  output = "--- a/#{self.path}\n+++ b/#{self.path}"
  format = :unified
  diffs.each do |piece|
    begin
      hunk = Difference::LCS::Hunk.new(data_old, data_new, piece, lines, file_length_difference)
      file_length_difference = hunk.file_length_difference
      next unless oldhunk
      if lines > 0 && hunk.overlaps?(oldhunk)
        hunk.unshift(oldhunk)
      else
        output << oldhunk.diff(format)
      end
    ensure
      oldhunk = hunk
      output << "\n"
    end
  end
  output << oldhunk.diff(format)
  output << "\n"
  output
end