From 1b832564211258526065aec985c5dd0570ed4df3 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 20 Feb 2019 05:06:42 -0800 Subject: [PATCH] Don't enable the "ScopeEngine" or try to identify scope context for diffs without context Summary: Depends on D20197. Ref T13161. We currently try to build a "ScopeEngine" even for diffs with no context (e.g., `git diff` instead of `git diff -U9999`). Since we don't have any context, we won't really be able to figure out anything useful about scopes. Also, since ScopeEngine is pretty strict about what it accepts, we crash. In these cases, just don't build a ScopeEngine. Test Plan: Viewed a diff I copy/pasted with `git diff` instead of an `arc diff` / `git diff -U99999`, got a sensible diff with no context instead of a fatal. Reviewers: amckinley Reviewed By: amckinley Maniphest Tasks: T13161 Differential Revision: https://secure.phabricator.com/D20198 --- .../render/DifferentialChangesetRenderer.php | 22 ++++++++++++++----- .../DifferentialChangesetTwoUpRenderer.php | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/applications/differential/render/DifferentialChangesetRenderer.php b/src/applications/differential/render/DifferentialChangesetRenderer.php index b7f78b5b68..26de5cb53b 100644 --- a/src/applications/differential/render/DifferentialChangesetRenderer.php +++ b/src/applications/differential/render/DifferentialChangesetRenderer.php @@ -33,7 +33,7 @@ abstract class DifferentialChangesetRenderer extends Phobject { private $canMarkDone; private $objectOwnerPHID; private $highlightingDisabled; - private $scopeEngine; + private $scopeEngine = false; private $depthOnlyLines; private $oldFile = false; @@ -677,13 +677,23 @@ abstract class DifferentialChangesetRenderer extends Phobject { return $views; } - final protected function getScopeEngine() { - if (!$this->scopeEngine) { - $line_map = $this->getNewLineTextMap(); + if ($this->scopeEngine === false) { + $hunk_starts = $this->getHunkStartLines(); - $scope_engine = id(new PhabricatorDiffScopeEngine()) - ->setLineTextMap($line_map); + // If this change is missing context, don't try to identify scopes, since + // we won't really be able to get anywhere. + $has_multiple_hunks = (count($hunk_starts) > 1); + $has_offset_hunks = (head_key($hunk_starts) != 1); + $missing_context = ($has_multiple_hunks || $has_offset_hunks); + + if ($missing_context) { + $scope_engine = null; + } else { + $line_map = $this->getNewLineTextMap(); + $scope_engine = id(new PhabricatorDiffScopeEngine()) + ->setLineTextMap($line_map); + } $this->scopeEngine = $scope_engine; } diff --git a/src/applications/differential/render/DifferentialChangesetTwoUpRenderer.php b/src/applications/differential/render/DifferentialChangesetTwoUpRenderer.php index c37655bb93..7efd29519e 100644 --- a/src/applications/differential/render/DifferentialChangesetTwoUpRenderer.php +++ b/src/applications/differential/render/DifferentialChangesetTwoUpRenderer.php @@ -94,7 +94,7 @@ final class DifferentialChangesetTwoUpRenderer $context_text = null; $context_line = null; - if (!$is_last_block) { + if (!$is_last_block && $scope_engine) { $target_line = $new_lines[$ii + $len]['line']; $context_line = $scope_engine->getScopeStart($target_line); if ($context_line !== null) {