]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Patch rendering fixes
authorAndrew Gallagher <agallagher@fb.com>
Tue, 28 Feb 2012 04:04:43 +0000 (20:04 -0800)
committerAndrew Gallagher <agallagher@fb.com>
Tue, 28 Feb 2012 05:39:12 +0000 (21:39 -0800)
Summary:
Not too familiar with the patch rendering code, but diff should fix
a few issues:
1) The current didn't seem to handle the case of an insertion only
diff (where the original text is empty).  Specifically, it would
still print a replacement line and would merge the line immediately
after the patch location into the patch application.
2) Patches with trailing newlines were rendered with an additional
newline.  This appears to be due to using ##explode## to break the
patch into lines.

Test Plan:
1) Verified that this fixed apache license linting for files that had
no license.
2) Checked that this didn't affect trailing-whitespace patch application.

Reviewers: epriestley, aran

Reviewed By: epriestley

CC: aran, epriestley
Differential Revision: https://secure.phabricator.com/D1715

src/lint/renderer/ArcanistLintRenderer.php

index cdc416e2540d4a3805b6d44470fe6f83299bf04b..f8a458caaf0eb1fc615674c16cff21f70514a0bd 100644 (file)
@@ -134,28 +134,35 @@ final class ArcanistLintRenderer {
     $text_lines = explode("\n", $text);
     $text_length = count($text_lines);
 
-    for (; $cursor < $line_num + $text_length; $cursor++) {
-      $chevron = ($cursor == $line_num);
-      // We may not have any data if, e.g., the old file does not exist.
-      $data = idx($line_data, $cursor, null);
-
-      // Highlight the problem substring.
-      $text_line = $text_lines[$cursor - $line_num];
-      if (strlen($text_line)) {
-        $data = substr_replace(
-          $data,
-          phutil_console_format('##%s##', $text_line),
-          ($cursor == $line_num)
-            ? $message->getChar() - 1
-            : 0,
-          strlen($text_line));
-      }
+    if ($text) {
+      for (; $cursor < $line_num + $text_length; $cursor++) {
+        $chevron = ($cursor == $line_num);
+        // We may not have any data if, e.g., the old file does not exist.
+        $data = idx($line_data, $cursor, null);
+
+        // Highlight the problem substring.
+        $text_line = $text_lines[$cursor - $line_num];
+        if (strlen($text_line)) {
+          $data = substr_replace(
+            $data,
+            phutil_console_format('##%s##', $text_line),
+            ($cursor == $line_num)
+              ? $message->getChar() - 1
+              : 0,
+            strlen($text_line));
+        }
 
-      $out[] = $this->renderLine($cursor, $data, $chevron, $diff);
+        $out[] = $this->renderLine($cursor, $data, $chevron, $diff);
+      }
     }
 
     // Print out replacement text.
     if ($message->isPatchable()) {
+      // Strip trailing newlines, since "explode" will create an extra patch
+      // line for these.
+      if (idx($patch, strlen($patch) - 1, null) === "\n") {
+        $patch = substr($patch, 0, -1);
+      }
       $patch_lines = explode("\n", $patch);
       $patch_length = count($patch_lines);
 
@@ -163,11 +170,15 @@ final class ArcanistLintRenderer {
 
       $len = isset($text_lines[0]) ? strlen($text_lines[0]) : 0;
 
-      $patched = substr_replace(
-        $line_data[$line_num],
-        phutil_console_format('##%s##', $patch_line),
-        $start,
-        $len);
+      $patched = phutil_console_format('##%s##', $patch_line);
+
+      if ($text) {
+        $patched = substr_replace(
+          $line_data[$line_num],
+          $patched,
+          $start,
+          $len);
+      }
 
       $out[] = $this->renderLine(null, $patched, false, '+');