Module: FileHelper

Included in:
FileManager
Defined in:
lib/file_helper.rb

Instance Method Summary collapse

Instance Method Details

#compare(commit, file_name, lines, context = nil, diff = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/file_helper.rb', line 23

def compare(commit, file_name, lines, context=nil, diff=nil)
  if diff.nil?
    Loggr.instance.info("COMPARE COMMIT: #{commit.id}")
  else
    Loggr.instance.info("COMPARE fdiff: #{fdiff_stat(diff)}")
  end

  real_file = `git show #{commit.id}:#{file_name}`
    .encode("UTF-8", invalid: :replace)
    .lines
    .map(&:chomp)

  return if real_file.length == 0
  if real_file.length != lines.length && diff.nil?
    puts "FILE SIZE MISMATCH:"
    puts "REAL: #{real_file.length}"
    puts "RECONSTRUCT: #{lines.length}"
    byebug
    1
  end

  lines
    .each_with_index
    .map do |line, line_number|
      if real_file[line_number] != line.gsub(/\r$/, "")
        puts "MISMATCH: #{commit.id}:#{file_name}"
        puts "REAL:"
        print_context(real_file, line_number)
        puts "RECONSTRUCT"
        print_context(lines, line_number)
        byebug
        1
      end
    end
end

#fdiff_stat(diff) ⇒ Object



6
7
8
# File 'lib/file_helper.rb', line 6

def fdiff_stat(diff)
  "@@ -#{diff.delete_start},#{diff.delete_count} +#{diff.insert_start},#{diff.insert_count} @@"
end


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/file_helper.rb', line 10

def print_context(lines, from, to=nil)
  if to.nil?
    to = from + 3
    from -= 4
  end
  (from...to).each do |i|
    line = lines[i]
    line = "nil" if line.nil?
    puts "#{i + 1}: #{line}"
  end
  nil
end