@@ -80,6 +80,9 @@ public function parse($string)
8080 $ diff = new Diff ($ fromMatch ['file ' ], $ toMatch ['file ' ]);
8181 ++$ i ;
8282 } else {
83+ if (preg_match ('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/ ' , $ lines [$ i ])) {
84+ continue ;
85+ }
8386 $ collected [] = $ lines [$ i ];
8487 }
8588 }
@@ -100,45 +103,34 @@ private function parseFileDiff(Diff $diff, array $lines)
100103 {
101104 $ chunks = array ();
102105
103- while (count ($ lines )) {
104- while (!preg_match ('(^@@ \\s+-(?P<start> \\d+)(?:, \\s*(?P<startrange> \\d+))? \\s+ \\+(?P<end> \\d+)(?:, \\s*(?P<endrange> \\d+))? \\s+@@) ' , $ last = array_shift ($ lines ), $ match )) {
105- if ($ last === null ) {
106- break 2 ;
107- }
106+ foreach ($ lines as $ line ) {
107+ if (preg_match ('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/ ' , $ line , $ match )) {
108+ $ chunk = new Chunk (
109+ $ match ['start ' ],
110+ isset ($ match ['startrange ' ]) ? max (1 , $ match ['startrange ' ]) : 1 ,
111+ $ match ['end ' ],
112+ isset ($ match ['endrange ' ]) ? max (1 , $ match ['endrange ' ]) : 1
113+ );
114+
115+ $ chunks [] = $ chunk ;
116+ $ diffLines = array ();
117+ continue ;
108118 }
109119
110- $ chunk = new Chunk (
111- $ match ['start ' ],
112- isset ($ match ['startrange ' ]) ? max (1 , $ match ['startrange ' ]) : 1 ,
113- $ match ['end ' ],
114- isset ($ match ['endrange ' ]) ? max (1 , $ match ['endrange ' ]) : 1
115- );
116-
117- $ diffLines = array ();
118- $ last = null ;
119-
120- while (count ($ lines ) &&
121- (preg_match ('(^(?P<type>[+ -])?(?P<line>.*)) ' , $ last = array_shift ($ lines ), $ match ) ||
122- (strpos ($ last , '\\ No newline at end of file ' ) === 0 ))) {
123- if (count ($ match )) {
124- $ type = Line::UNCHANGED ;
120+ if (preg_match ('/^(?P<type>[+ -])?(?P<line>.*)/ ' , $ line , $ match )) {
121+ $ type = Line::UNCHANGED ;
125122
126- if ($ match ['type ' ] == '+ ' ) {
127- $ type = Line::ADDED ;
128- } elseif ($ match ['type ' ] == '- ' ) {
129- $ type = Line::REMOVED ;
130- }
131-
132- $ diffLines [] = new Line ($ type , $ match ['line ' ]);
123+ if ($ match ['type ' ] == '+ ' ) {
124+ $ type = Line::ADDED ;
125+ } elseif ($ match ['type ' ] == '- ' ) {
126+ $ type = Line::REMOVED ;
133127 }
134- }
135128
136- $ chunk -> setLines ( $ diffLines );
129+ $ diffLines [] = new Line ( $ type , $ match [ ' line ' ] );
137130
138- $ chunks [] = $ chunk ;
139-
140- if ($ last !== null ) {
141- array_unshift ($ lines , $ last );
131+ if (isset ($ chunk )) {
132+ $ chunk ->setLines ($ diffLines );
133+ }
142134 }
143135 }
144136
0 commit comments