class GitCrecord::Diff::Hunk

Public Class Methods

new(head, reverse: false) click to toggle source
Calls superclass method GitCrecord::Diff::Difference::new
# File lib/git_crecord/diff/hunk.rb, line 10
def initialize(head, reverse: false)
  @head = head
  @expanded = true
  super(reverse: reverse)
end

Public Instance Methods

<<(line) click to toggle source
# File lib/git_crecord/diff/hunk.rb, line 24
def <<(line)
  subs << Line.new(line, reverse: @reverse)
  self
end
generate_diff() click to toggle source
# File lib/git_crecord/diff/hunk.rb, line 29
def generate_diff
  return nil unless selected

  [generate_header, *subs.map(&:generate_diff).compact].join("\n")
end
generate_header() click to toggle source
# File lib/git_crecord/diff/hunk.rb, line 35
def generate_header
  old_start, old_count, new_start, new_count = parse_header
  selectable_subs.each do |sub|
    next if sub.selected

    new_count -= 1 if sub.add?
    new_count += 1 if sub.del?
  end
  "@@ -#{old_start},#{old_count} +#{new_start},#{new_count} @@"
end
parse_header() click to toggle source
# File lib/git_crecord/diff/hunk.rb, line 46
def parse_header
  match = @head.match(/@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@/)
  raise "mismatching hunk-header - '#{@head}'" if match.nil?

  [match[1], match[3] || 1, match[4], match[6] || 1].map(&:to_i)
end
to_s() click to toggle source
# File lib/git_crecord/diff/hunk.rb, line 16
def to_s
  @head
end
x_offset() click to toggle source
# File lib/git_crecord/diff/hunk.rb, line 20
def x_offset
  3
end