class GitDiffParser::Patches

The array of patch

Public Class Methods

[](*ary) click to toggle source

@return [Patches<Patch>]

# File lib/git_diff_parser/patches.rb, line 6
def self.[](*ary)
  new(ary)
end
new(*args) click to toggle source

@return [Patches<Patch>]

Calls superclass method
# File lib/git_diff_parser/patches.rb, line 55
def initialize(*args)
  super Array.new(*args)
end
parse(contents) click to toggle source

@param contents [String] `git diff` result

@return [Patches<Patch>] parsed object

# File lib/git_diff_parser/patches.rb, line 13
def self.parse(contents)
  body = false
  file_name = ''
  patch = []
  lines = contents.lines
  line_count = lines.count
  parsed = new
  lines.each_with_index do |line, count|
    case parsed.scrub_string(line.chomp)
    when /^diff/
      unless patch.empty?
        parsed << Patch.new(patch.join("\n") + "\n", file: file_name)
        patch.clear
        file_name = ''
      end
      body = false
    when /^\-\-\-/
    when %r{^\+\+\+ b/(?<file_name>.*)}
      file_name = Regexp.last_match[:file_name].rstrip
      body = true
    when /^(?<body>[\ @\+\-\\].*)/
      patch << Regexp.last_match[:body] if body
      if !patch.empty? && body && line_count == count + 1
        parsed << Patch.new(patch.join("\n") + "\n", file: file_name)
        patch.clear
        file_name = ''
      end
    end
  end
  parsed
end

Public Instance Methods

files() click to toggle source

@return [Array<String>] file path

# File lib/git_diff_parser/patches.rb, line 60
def files
  map(&:file)
end
find_patch_by_file(file) click to toggle source

@param file [String] file path

@return [Patch, nil]

# File lib/git_diff_parser/patches.rb, line 72
def find_patch_by_file(file)
  find { |patch| patch.file == file }
end
find_patch_by_secure_hash(secure_hash) click to toggle source

@param secure_hash [String] target sha1 hash

@return [Patch, nil]

# File lib/git_diff_parser/patches.rb, line 79
def find_patch_by_secure_hash(secure_hash)
  find { |patch| patch.secure_hash == secure_hash }
end
scrub_string(line) click to toggle source

@return [String]

# File lib/git_diff_parser/patches.rb, line 46
def scrub_string(line)
  if RUBY_VERSION >= '2.1'
    line.scrub
  else
    line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
  end
end
secure_hashes() click to toggle source

@return [Array<String>] target sha1 hash

# File lib/git_diff_parser/patches.rb, line 65
def secure_hashes
  map(&:secure_hash)
end