r112096 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112095‎ | r112096 | r112097 >
Date:10:01, 22 February 2012
Author:hashar
Status:deferred
Tags:
Comment:
(bug 34554) diff chunk fail to parse file add/rm

In my original implementation of chunk parsing, I though the s
parameter was mandatory, it is optional! That raised some exceptions.
Modified paths:
  • /trunk/extensions/CodeReview/backend/DiffHighlighter.php (modified) (history)
  • /trunk/extensions/CodeReview/tests/DiffHighlighterTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CodeReview/tests/DiffHighlighterTest.php
@@ -48,6 +48,37 @@
4949 array( 1, 63, 0, 0 ),
5050 '@@ -1,63 +0,0 @@'
5151 ),
 52+ array(
 53+ # File deletion? Second param is a default value for s
 54+ array( 1, 1, 0, 0 ),
 55+ '@@ -1 +0,0 @@'
 56+ ),
 57+ array(
 58+ # File addition? Last param is a default value for s
 59+ array( 0, 0, 1, 1 ),
 60+ '@@ -0,0 +1 @@'
 61+ ),
 62+ array(
 63+ array( 1, 1, 1, 1 ),
 64+ '@@ -1 +1 @@'
 65+ ),
 66+
 67+ # Some chunks which probably are non sense but yet must be
 68+ # considered valid by our chunk parsing code.
 69+ array(
 70+ array( 25, 1, 26, 1 ),
 71+ '@@ -25 +26 @@'
 72+ ),
 73+ array(
 74+ array( 31, 32, 33, 1 ),
 75+ '@@ -31,32 +33 @@'
 76+ ),
 77+ array(
 78+ array( 41, 1, 42, 43 ),
 79+ '@@ -41 +42,43 @@'
 80+ # note how this has the answer to the ultimate question of
 81+ # life, the universe and everything. Useful to hitchhikers.
 82+ ),
5283 );
5384 }
5485
Index: trunk/extensions/CodeReview/backend/DiffHighlighter.php
@@ -214,6 +214,14 @@
215215 * - l is the starting line number
216216 * - s is the number of lines the change hunk applies to
217217 *
 218+ * 's', for the number of lines, is optional and default to 1.
 219+ * When omitted, the previous comma will be skipped as well. So all
 220+ * following lines are valid too:
 221+ *
 222+ * @@ -l,s +l @@
 223+ * @@ -l +l,s @@
 224+ * @@ -l +l @@
 225+ *
218226 * NOTE: visibility is 'public' since the function covered by tests.
219227 *
220228 * @param $chunk string a one line chunk as described above
@@ -224,16 +232,33 @@
225233
226234 # regex snippet to capture a number
227235 $n = "(\d+)";
 236+ $s = "(?:,(\d+))";
 237+ $matches = preg_match( "/^@@ -$n$s \+$n$s @@$/", $chunkHeader, $m );
 238+ if( $matches === 1 ) {
 239+ array_shift( $m );
 240+ return $m;
 241+ }
228242
229 - $matches = preg_match( "/^@@ -$n,$n \+$n,$n @@$/", $chunkHeader, $m );
230 - array_shift( $m );
 243+ $s_default_value = 1;
231244
232 - if( $matches !== 1 ) {
233 - # We really really should have matched something!
234 - throw new MWException(
235 - __METHOD__ . " given an invalid chunk header: '$chunkHeader'\n"
236 - );
 245+ $matches = preg_match( "/^@@ -$n$s \+$n @@$/", $chunkHeader, $m );
 246+ if( $matches === 1 ) {
 247+ return array( $m[1], $m[2], $m[3], $s_default_value );
237248 }
238 - return $m;
 249+
 250+ $matches = preg_match( "/^@@ -$n \+$n$s @@$/", $chunkHeader, $m );
 251+ if( $matches === 1 ) {
 252+ return array( $m[1], $s_default_value, $m[2], $m[3] );
 253+ }
 254+
 255+ $matches = preg_match( "/^@@ -$n \+$n @@$/", $chunkHeader, $m );
 256+ if( $matches === 1 ) {
 257+ return array( $m[1], $s_default_value, $m[2], $s_default_value );
 258+ }
 259+
 260+ # We really really should have matched something!
 261+ throw new MWException(
 262+ __METHOD__ . " given an invalid chunk header: '$chunkHeader'\n"
 263+ );
239264 }
240265 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r112098MFT to 1.19wmf1 r112096...hashar10:06, 22 February 2012

Status & tagging log