class GitDiff::File

Attributes

a_blob[R]
a_path[R]
b_blob[R]
b_mode[R]
b_path[R]
binary[R]
current_hunk[RW]
hunks[R]

Public Class Methods

from_string(string) click to toggle source
# File lib/git_diff/file.rb, line 7
def self.from_string(string)
  if path_info = %r{^diff --git(?: a/(\S+))?(?: b/(\S+))?}.match(string)
    File.new(
      a_path: path_info.captures[0] || "/dev/null",
      b_path: path_info.captures[1] || "/dev/null"
    )
  end
end
new(a_path: "/dev/null", b_path: "/dev/null") click to toggle source
# File lib/git_diff/file.rb, line 16
def initialize(a_path: "/dev/null", b_path: "/dev/null")
  @hunks = []
  @a_path = a_path
  @b_path = b_path
end

Public Instance Methods

<<(string) click to toggle source
# File lib/git_diff/file.rb, line 22
def <<(string)
  return if extract_diff_meta_data(string)

  if (range_info = RangeInfo.from_string(string))
    add_hunk Hunk.new(range_info)
  else
    append_to_current_hunk string
  end
end
stats() click to toggle source
# File lib/git_diff/file.rb, line 32
def stats
  @stats ||= Stats.total(collector)
end

Private Instance Methods

add_hunk(hunk) click to toggle source
# File lib/git_diff/file.rb, line 44
def add_hunk(hunk)
  self.current_hunk = hunk
  hunks << current_hunk
  @binary = false
end
append_to_current_hunk(string) click to toggle source
# File lib/git_diff/file.rb, line 50
def append_to_current_hunk(string)
  current_hunk << string
end
collector() click to toggle source
# File lib/git_diff/file.rb, line 40
def collector
  GitDiff::StatsCollector::Rollup.new(hunks)
end
extract_diff_meta_data(string) click to toggle source
# File lib/git_diff/file.rb, line 54
def extract_diff_meta_data(string)
  if a_path_info = %r{^[-]{3} /dev/null(.*)$}.match(string)
    @a_path = "/dev/null"
  elsif a_path_info = %r{^[-]{3} "?a/(.*)$}.match(string)
    @a_path = a_path_info[1]
  elsif b_path_info = %r{^[+]{3} /dev/null(.*)$}.match(string)
    @b_path = "/dev/null"
  elsif b_path_info = %r{^[+]{3} "?b/(.*)$}.match(string)
    @b_path = b_path_info[1]
  elsif blob_info = /^index ([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+) ?(.+)?$/.match(string)
    @a_blob, @b_blob, @b_mode = *blob_info.captures
  elsif /^new file mode [0-9]{6}$/.match(string)
    @a_path = "/dev/null"
  elsif /^deleted file mode [0-9]{6}$/.match(string)
    @b_path = "/dev/null"
  elsif mode_info = /^(old|new) mode ([0-9]{6})$/.match(string)
    if mode_info.captures[0] == "old"
      @a_mode = mode_info.captures[1]
    else
      @b_mode = mode_info.captures[1]
    end
  elsif copy_rename_info = /^(copy|rename) (from|to) (.*)$/.match(string)
    if copy_rename_info.captures[1] == "from"
      @a_path = copy_rename_info.captures[2]
    else
      @b_path = copy_rename_info.captures[2]
    end
  elsif binary_info = %r{^Binary files (?:/dev/null|"?a/(.*)) and (?:/dev/null|"?b/(.*)) differ$}.match(string)
    @binary = true
    @a_path ||= binary_info[1] || "/dev/null"
    @b_path ||= binary_info[2] || "/dev/null"
  elsif /^similarity/.match(string)
    # trash
    true
  end
end