Commit 6cf017d6 authored by epriestley's avatar epriestley
Browse files

Fix an unusual issue with intradiff highlighting of files with uncommon end-of-file modifications

Summary:
Fixes T13539. See that task for discussion and a reproduction case.

This algorithm currently counts "\ No newline at end of file" lines as though they were normal source lines. This can cause offset issues in the rare case that a diff contains two of these lines (for each side of the file) and has changes between them (because the last line of the file was modified between the diffs).

Instead, don't count "\" as a display line.

Test Plan:
  - See T13539 and PHI1740.
  - Before: got fatals on the "wild" diff and the synthetic simplified version.
  - After: clean intradiff rendering in both cases.

Maniphest Tasks: T13539

Differential Revision: https://secure.phabricator.com/D21267
parent 0c51885c
......@@ -582,8 +582,13 @@ final class DifferentialHunkParser extends Phobject {
$changes = $hunk->getSplitLines();
foreach ($changes as $line) {
$diff_type = $line[0]; // Change type in diff of diffs.
$is_same = ($diff_type === ' ');
$is_add = ($diff_type === '+');
$is_rem = ($diff_type === '-');
$orig_type = $line[1]; // Change type in the original diff.
if ($diff_type == ' ') {
if ($is_same) {
// Use the same key for lines that are next to each other.
if ($olds_cursor > $news_cursor) {
$key = $olds_cursor + 1;
......@@ -594,17 +599,32 @@ final class DifferentialHunkParser extends Phobject {
$news[$key] = null;
$olds_cursor = $key;
$news_cursor = $key;
} else if ($diff_type == '-') {
} else if ($is_rem) {
$olds[] = array($n_old, $orig_type);
$olds_cursor++;
} else if ($diff_type == '+') {
} else if ($is_add) {
$news[] = array($n_new, $orig_type);
$news_cursor++;
} else {
throw new Exception(
pht(
'Found unknown intradiff source line, expected a line '.
'beginning with "+", "-", or " " (space): %s.',
$line));
}
// See T13539. Don't increment the line count if this line was removed,
// or if the line is a "No newline at end of file" marker.
$not_a_line = ($orig_type === '-' || $orig_type === '\\');
if ($not_a_line) {
continue;
}
if (($diff_type == '-' || $diff_type == ' ') && $orig_type != '-') {
if ($is_same || $is_rem) {
$n_old++;
}
if (($diff_type == '+' || $diff_type == ' ') && $orig_type != '-') {
if ($is_same || $is_add) {
$n_new++;
}
}
......@@ -623,14 +643,18 @@ final class DifferentialHunkParser extends Phobject {
list($n, $type) = $olds[$i];
if ($type == '+' ||
($type == ' ' && isset($news[$i]) && $news[$i][1] != ' ')) {
$highlight_old[] = $offsets_old[$n];
if (isset($offsets_old[$n])) {
$highlight_old[] = $offsets_old[$n];
}
}
}
if (isset($news[$i])) {
list($n, $type) = $news[$i];
if ($type == '+' ||
($type == ' ' && isset($olds[$i]) && $olds[$i][1] != ' ')) {
$highlight_new[] = $offsets_new[$n];
if (isset($offsets_new[$n])) {
$highlight_new[] = $offsets_new[$n];
}
}
}
}
......
......@@ -4,19 +4,19 @@ final class DifferentialChangesetParserTestCase extends PhabricatorTestCase {
public function testDiffChangesets() {
$hunk = new DifferentialHunk();
$hunk->setChanges("+a\n b\n-c");
$hunk->setChanges("+a\n b\n-c\n");
$hunk->setNewOffset(1);
$hunk->setNewLen(2);
$left = new DifferentialChangeset();
$left->attachHunks(array($hunk));
$tests = array(
"+a\n b\n-c" => array(array(), array()),
"+a\n x\n-c" => array(array(), array()),
"+aa\n b\n-c" => array(array(1), array(11)),
" b\n-c" => array(array(1), array()),
"+a\n b\n c" => array(array(), array(13)),
"+a\n x\n c" => array(array(), array(13)),
"+a\n b\n-c\n" => array(array(), array()),
"+a\n x\n-c\n" => array(array(), array()),
"+aa\n b\n-c\n" => array(array(1), array(11)),
" b\n-c\n" => array(array(1), array()),
"+a\n b\n c\n" => array(array(), array(13)),
"+a\n x\n c\n" => array(array(), array(13)),
);
foreach ($tests as $changes => $expected) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment