Sha256: c903d5cc56f188f1ea7499174294b25d204b13044fcb0631306718b621f37220

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

require_relative 'difference'
require_relative 'line'
require_relative '../ui/color'

module GitCrecord
  module Diff
    class Hunk < Difference
      def initialize(head)
        @head = head
        @expanded = true
        super()
      end

      def to_s
        @head
      end

      def x_offset
        3
      end

      def <<(line)
        subs << Line.new(line)
        self
      end

      def generate_diff
        return nil unless selected
        [generate_header, *subs.map(&:generate_diff).compact].join("\n")
      end

      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

      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
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
git-crecord-1.0.8 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.7 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.6 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.5 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.4 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.3 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.2 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.1 lib/git_crecord/diff/hunk.rb
git-crecord-1.0.0 lib/git_crecord/diff/hunk.rb