File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,6 +7,41 @@ def self.[](*ary)
77 new ( ary )
88 end
99
10+ # @param contents [String] `git diff` result
11+ #
12+ # @return [Patches<Patch>] parsed object
13+ def self . parse ( contents )
14+ body = false
15+ file_name = ''
16+ patch = [ ]
17+ lines = contents . lines
18+ line_count = lines . count
19+ parsed = new
20+ lines . each_with_index do |line , count |
21+ case line . chomp
22+ when /^diff/
23+ unless patch . empty?
24+ parsed << Patch . new ( patch . join ( "\n " ) + "\n " , file : file_name )
25+ patch . clear
26+ file_name = ''
27+ end
28+ body = false
29+ when /^\- \- \- /
30+ when %r{^\+ \+ \+ b/(?<file_name>.*)}
31+ file_name = Regexp . last_match [ :file_name ]
32+ body = true
33+ when /^(?<body>[\ @\+ \- \\ ].*)/
34+ patch << Regexp . last_match [ :body ] if body
35+ if !patch . empty? && body && line_count == count + 1
36+ parsed << Patch . new ( patch . join ( "\n " ) + "\n " , file : file_name )
37+ patch . clear
38+ file_name = ''
39+ end
40+ end
41+ end
42+ parsed
43+ end
44+
1045 # @return [Patches<Patch>]
1146 def initialize ( *args )
1247 super Array . new ( *args )
Original file line number Diff line number Diff line change 11require 'spec_helper'
22
33module GitDiffParser
4- describe DiffParser do
4+ describe Patches do
55 describe '#parse' do
66 let ( :file0 ) { 'lib/saddler/reporter/github.rb' }
77 let ( :body0 ) { File . read ( 'spec/support/fixtures/file0.diff' ) }
@@ -13,7 +13,7 @@ module GitDiffParser
1313 let ( :body3 ) { File . read ( 'spec/support/fixtures/file3.diff' ) }
1414 it 'returns parsed patches' do
1515 diff_body = File . read ( 'spec/support/fixtures/d1bd180-c27866c.diff' )
16- patches = DiffParser . parse ( diff_body )
16+ patches = Patches . parse ( diff_body )
1717
1818 expect ( patches . size ) . to eq 4
1919 expect ( patches [ 0 ] . file ) . to eq file0
You can’t perform that action at this time.
0 commit comments