class GitSpelunk::Offset::Chunk

Constants

LineBlock

Attributes

lines[R]
minus_length[R]
minus_offset[R]
plus_length[R]
plus_offset[R]

Public Class Methods

new(data) click to toggle source
# File lib/git_spelunk/offset.rb, line 54
def initialize(data)
  @minus_offset, @minus_length, @plus_offset, @plus_length = *extract_stats(data[0])
  @lines = data[1..-1]
end

Public Instance Methods

find_parent_line_number(target) click to toggle source
# File lib/git_spelunk/offset.rb, line 81
def find_parent_line_number(target)
  # separate in blocks of lines with the same prefix

  old_line_number = minus_offset
  new_line_number = plus_offset

  blocks = []
  lines.each do |l|
    next if l =~ /\\ No newline at end of file/
    last_block = blocks.last

    if last_block.nil? || last_block.type != l[0]
      blocks << LineBlock.new(old_line_number, l)
    else
      last_block << l
    end

    if l[0] == "+" || l[0] == " "
      if new_line_number == target
        # important: we don't finish building the structure.
        break
      end

      new_line_number += 1
    end

    if l[0] == "-" || l[0] == " "
      old_line_number += 1
    end
  end

  addition_block = blocks.pop
  last_old_block = blocks.last

  if last_old_block.type == " "
    # if the previous context existed in both, just go to the end of that.
    last_old_block.offset + (last_old_block.size - 1)
  else
    # offset N lines into the block that was removed to create the target block, but don't go beyond the edge of it.
    last_old_block.offset + [addition_block.size - 1, last_old_block.size].min
  end
end
has_line?(line_number) click to toggle source
# File lib/git_spelunk/offset.rb, line 59
def has_line?(line_number)
  plus_offset <= line_number && line_number <= (plus_offset + plus_length)
end

Private Instance Methods

extract_stats(l) click to toggle source
# File lib/git_spelunk/offset.rb, line 126
def extract_stats(l)
  #@@ -1,355 +1,355 @@
  l.scan(STATS_PATTERN).first.map(&:to_i)
end
new_has?(line) click to toggle source
# File lib/git_spelunk/offset.rb, line 136
def new_has?(line)
  # Src line will either have a "-" or will be an unchanged line
  line[0] == '+' || line[0] == " "
end
old_has?(line) click to toggle source
# File lib/git_spelunk/offset.rb, line 131
def old_has?(line)
  # Src line will either have a "+" or will be an unchanged line
  line[0] == '-' || line[0] == " "
end