Sha256: ad6cf5c47e0de17a84baa5d07ea70274adfd50d0f75f07adb4bdff042a469f6c

Contents?: true

Size: 981 Bytes

Versions: 4

Compression:

Stored size: 981 Bytes

Contents

module GitDiffParser
  class DiffParser
    def self.parse(contents)
      body = false
      file_name = ''
      patch = []
      lines = contents.lines
      line_count = lines.count
      parsed = Patches.new
      lines.each_with_index do |line, count|
        case 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]
          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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
git_diff_parser-2.1.2 lib/git_diff_parser/diff_parser.rb
git_diff_parser-2.1.1 lib/git_diff_parser/diff_parser.rb
git_diff_parser-2.1.0 lib/git_diff_parser/diff_parser.rb
git_diff_parser-2.0.0 lib/git_diff_parser/diff_parser.rb