@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Simplify context parsing and add test coverage

Summary:
- Remove 'missingNew', etc. It's impossible for a diff to popluate these, as far as I can tell (I can't generate such a diff, or find any which generate it).
- Add unit tests.

Test Plan: Unit tests, viewed a diff with some missing context.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2009

Differential Revision: https://secure.phabricator.com/D4393

+118 -79
+6 -19
src/applications/differential/parser/DifferentialChangesetParser.php
··· 10 10 protected $oldRender = null; 11 11 12 12 protected $filename = null; 13 - protected $missingOld = array(); 14 - protected $missingNew = array(); 13 + protected $hunkStartLines = array(); 15 14 16 15 protected $comments = array(); 17 16 protected $specialAttributes = array(); ··· 41 40 private $markupEngine; 42 41 private $highlightErrors; 43 42 44 - const CACHE_VERSION = 8; 43 + const CACHE_VERSION = 9; 45 44 const CACHE_MAX_SIZE = 8e6; 46 45 47 46 const ATTR_GENERATED = 'attr:generated'; ··· 74 73 return $this; 75 74 } 76 75 77 - public function setMissingNewLineMarkerMap(array $map) { 78 - $this->missingNew = $map; 79 - return $this; 80 - } 81 - 82 - public function setMissingOldLineMarkerMap(array $map) { 83 - $this->missingOld = $map; 84 - return $this; 85 - } 86 - 87 76 public function setIntraLineDiffs(array $diffs) { 88 77 $this->intra = $diffs; 89 78 return $this; ··· 298 287 'newRender', 299 288 'oldRender', 300 289 'specialAttributes', 301 - 'missingOld', 302 - 'missingNew', 290 + 'hunkStartLines', 303 291 'cacheVersion', 304 292 'cacheHost', 305 293 ); ··· 593 581 $this->setNewLines($hunk_parser->getNewLines()); 594 582 $this->setIntraLineDiffs($hunk_parser->getIntraLineDiffs()); 595 583 $this->setVisibileLinesMask($hunk_parser->getVisibleLinesMask()); 596 - $this->setMissingOldLineMarkerMap($hunk_parser->getOldLineMarkerMap()); 597 - $this->setMissingNewLineMarkerMap($hunk_parser->getNewLineMarkerMap()); 584 + $this->hunkStartLines = $hunk_parser->getHunkStartLines( 585 + $changeset->getHunks()); 598 586 599 587 $new_corpus = $hunk_parser->getNewCorpus(); 600 588 $new_corpus_block = implode('', $new_corpus); ··· 711 699 ->setLineCount($rows) 712 700 ->setOldRender($this->oldRender) 713 701 ->setNewRender($this->newRender) 714 - ->setMissingOldLines($this->missingOld) 715 - ->setMissingNewLines($this->missingNew) 702 + ->setHunkStartLines($this->hunkStartLines) 716 703 ->setOldChangesetID($this->leftSideChangesetID) 717 704 ->setNewChangesetID($this->rightSideChangesetID) 718 705 ->setOldAttachesToNewFile($this->leftSideAttachesToNewFile)
+21 -38
src/applications/differential/parser/DifferentialHunkParser.php
··· 7 7 private $isDeleted; 8 8 private $oldLines; 9 9 private $newLines; 10 - private $oldLineMarkerMap; 11 - private $newLineMarkerMap; 12 10 private $skipIntraLines; 13 11 private $whitespaceMode; 14 12 private $intraLineDiffs; 15 13 private $visibleLinesMask; 14 + 15 + /** 16 + * Get a map of lines on which hunks start, other than line 1. This 17 + * datastructure is used to determine when to render "Context not available." 18 + * in diffs with multiple hunks. 19 + * 20 + * @return dict<int, bool> Map of lines where hunks start, other than line 1. 21 + */ 22 + public function getHunkStartLines(array $hunks) { 23 + assert_instances_of($hunks, 'DifferentialHunk'); 24 + 25 + $map = array(); 26 + foreach ($hunks as $hunk) { 27 + $line = $hunk->getOldOffset(); 28 + if ($line > 1) { 29 + $map[$line] = true; 30 + } 31 + } 32 + 33 + return $map; 34 + } 16 35 17 36 private function setVisibleLinesMask($mask) { 18 37 $this->visibleLinesMask = $mask; ··· 64 83 ); 65 84 } 66 85 return $this->skipIntraLines; 67 - } 68 - 69 - private function setNewLineMarkerMap($new_line_marker_map) { 70 - $this->newLineMarkerMap = $new_line_marker_map; 71 - return $this; 72 - } 73 - public function getNewLineMarkerMap() { 74 - if ($this->newLineMarkerMap === null) { 75 - throw new Exception( 76 - 'You must parseHunksForLineData before accessing this data.' 77 - ); 78 - } 79 - return $this->newLineMarkerMap; 80 - } 81 - 82 - private function setOldLineMarkerMap($old_line_marker_map) { 83 - $this->oldLineMarkerMap = $old_line_marker_map; 84 - return $this; 85 - } 86 - public function getOldLineMarkerMap() { 87 - if ($this->oldLineMarkerMap === null) { 88 - throw new Exception( 89 - 'You must parseHunksForLineData before accessing this data.' 90 - ); 91 - } 92 - return $this->oldLineMarkerMap; 93 86 } 94 87 95 88 private function setNewLines($new_lines) { ··· 419 412 420 413 $old_lines = array(); 421 414 $new_lines = array(); 422 - $old_line_marker_map = array(); 423 - $new_line_marker_map = array(); 424 - 425 415 foreach ($hunks as $hunk) { 426 416 427 417 $lines = $hunk->getChanges(); ··· 443 433 444 434 $old_line = $hunk->getOldOffset(); 445 435 $new_line = $hunk->getNewOffset(); 446 - if ($old_line > 1) { 447 - $old_line_marker_map[$old_line] = true; 448 - } else if ($new_line > 1) { 449 - $new_line_marker_map[$new_line] = true; 450 - } 451 436 452 437 $num_lines = count($lines); 453 438 for ($cursor = 0; $cursor < $num_lines; $cursor++) { ··· 484 469 485 470 $this->setOldLines($old_lines); 486 471 $this->setNewLines($new_lines); 487 - $this->setOldLineMarkerMap($old_line_marker_map); 488 - $this->setNewLineMarkerMap($new_line_marker_map); 489 472 490 473 return $this; 491 474 }
+39 -1
src/applications/differential/parser/__tests__/DifferentialHunkParserTestCase.php
··· 39 39 ); 40 40 } 41 41 42 + private function createHunksFromFile($name) { 43 + $data = Filesystem::readFile(dirname(__FILE__).'/data/'.$name); 44 + 45 + $parser = new ArcanistDiffParser(); 46 + $changes = $parser->parseDiff($data); 47 + if (count($changes) !== 1) { 48 + throw new Exception("Expected 1 changeset for '{$name}'!"); 49 + } 50 + 51 + $diff = DifferentialDiff::newFromRawChanges($changes); 52 + return head($diff->getChangesets())->getHunks(); 53 + } 54 + 42 55 public function testOneLineOldComment() { 43 56 $parser = new DifferentialHunkParser(); 44 57 $hunks = $this->createSingleChange(1, 0, "-a"); ··· 68 81 0); 69 82 $this->assertEqual("", $context); 70 83 } 71 - 84 + 72 85 public function testOverlapFromStartOfHunk() { 73 86 $parser = new DifferentialHunkParser(); 74 87 $hunks = array( ··· 232 245 "-o3\n". 233 246 "+n2", $context); 234 247 } 248 + 249 + public function testMissingContext() { 250 + $tests = array( 251 + 'missing_context.diff' => array( 252 + 4 => true, 253 + ), 254 + 'missing_context_2.diff' => array( 255 + 5 => true, 256 + ), 257 + 'missing_context_3.diff' => array( 258 + 4 => true, 259 + 13 => true, 260 + ), 261 + ); 262 + 263 + foreach ($tests as $name => $expect) { 264 + $hunks = $this->createHunksFromFile($name); 265 + 266 + $parser = new DifferentialHunkParser(); 267 + $actual = $parser->getHunkStartLines($hunks); 268 + 269 + $this->assertEqual($expect, $actual, $name); 270 + } 271 + } 272 + 235 273 } 236 274
+11
src/applications/differential/parser/__tests__/data/missing_context.diff
··· 1 + diff --git a/fruit b/fruit 2 + index a1f7255..b5fb7b8 100644 3 + --- a/fruit 4 + +++ b/fruit 5 + @@ -4,6 +4,5 @@ cherry 6 + date 7 + elderberry 8 + fig 9 + -grape 10 + honeydew 11 +
+12
src/applications/differential/parser/__tests__/data/missing_context_2.diff
··· 1 + diff --git a/fruit b/fruit 2 + index a1f7255..fa84742 100644 3 + --- a/fruit 4 + +++ b/fruit 5 + @@ -5,5 +5,7 @@ date 6 + elderberry 7 + fig 8 + grape 9 + +guava 10 + +gooseberry 11 + honeydew 12 +
+18
src/applications/differential/parser/__tests__/data/missing_context_3.diff
··· 1 + diff --git a/fruit b/fruit 2 + index bf66874..071dd49 100644 3 + --- a/fruit 4 + +++ b/fruit 5 + @@ -4,7 +4,6 @@ date 6 + elderberry 7 + fig 8 + banana 9 + -grape 10 + honeydew 11 + apple 12 + cherry 13 + @@ -13,5 +12,4 @@ elderberry 14 + fig 15 + banana 16 + grape 17 + -honeydew 18 +
+8 -16
src/applications/differential/render/DifferentialChangesetRenderer.php
··· 6 6 private $changeset; 7 7 private $renderingReference; 8 8 private $renderPropertyChangeHeader; 9 - private $missingOldLines; 10 - private $missingNewLines; 9 + private $hunkStartLines; 11 10 private $oldLines; 12 11 private $newLines; 13 12 private $oldComments; ··· 205 204 return $this->oldLines; 206 205 } 207 206 208 - public function setMissingNewLines(array $missing_new_lines) { 209 - $this->missingNewLines = $missing_new_lines; 207 + public function setHunkStartLines(array $hunk_start_lines) { 208 + $this->hunkStartLines = $hunk_start_lines; 210 209 return $this; 211 210 } 212 - protected function getMissingNewLines() { 213 - return $this->missingNewLines; 214 - } 215 211 216 - public function setMissingOldLines(array $missing_old_lines) { 217 - $this->missingOldLines = $missing_old_lines; 218 - return $this; 219 - } 220 - protected function getMissingOldLines() { 221 - return $this->missingOldLines; 212 + protected function getHunkStartLines() { 213 + return $this->hunkStartLines; 222 214 } 223 215 224 216 public function setUser(PhabricatorUser $user) { ··· 260 252 $rows 261 253 ); 262 254 abstract public function renderFileChange( 263 - $old = null, 264 - $new = null, 265 - $id = 0, 255 + $old = null, 256 + $new = null, 257 + $id = 0, 266 258 $vs = 0 267 259 ); 268 260
+3 -5
src/applications/differential/render/DifferentialChangesetTwoUpRenderer.php
··· 44 44 $range_len, 45 45 $rows) { 46 46 47 - $missing_old = $this->getMissingOldLines(); 48 - $missing_new = $this->getMissingNewLines(); 47 + $hunk_starts = $this->getHunkStartLines(); 49 48 50 49 $context_not_available = null; 51 - if ($missing_old || $missing_new) { 50 + if ($hunk_starts) { 52 51 $context_not_available = javelin_render_tag( 53 52 'tr', 54 53 array( ··· 282 281 } 283 282 $n_classes .= ' right'.$n_colspan; 284 283 285 - if (($o_num && !empty($missing_old[$o_num])) || 286 - ($n_num && !empty($missing_new[$n_num]))) { 284 + if (isset($hunk_starts[$o_num])) { 287 285 $html[] = $context_not_available; 288 286 } 289 287