]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Strip colorized diffs, show line numbers in context
authorepriestley <git@epriestley.com>
Wed, 14 Mar 2012 13:34:00 +0000 (06:34 -0700)
committerepriestley <git@epriestley.com>
Wed, 14 Mar 2012 13:34:00 +0000 (06:34 -0700)
Summary:
  - When users pipe in colorized diffs, strip the colors instead of failing.
  - When showing context, show line numbers (we do show 3 lines around the failure, the failure was just on the first line).
  - Remove an irrelevant TODO comment (we handle this elsewhere now).

Test Plan: Unit tests.

Reviewers: Makinde, btrahan

Reviewed By: Makinde

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

src/parser/diff/ArcanistDiffParser.php
src/parser/diff/__tests__/ArcanistDiffParserTestCase.php
src/parser/diff/__tests__/data/colorized.hggitdiff [new file with mode: 0644]

index f8405bd24b7cfa4cec66b1e71416c0302d8b2732..08cafae693df966fc5fde06b18c1a541481de88c 100644 (file)
@@ -936,12 +936,37 @@ final class ArcanistDiffParser {
   }
 
   protected function didStartParse($text) {
-    // TODO: Removed an fb_utf8ize() call here. -epriestley
 
     // Eat leading whitespace. This may happen if the first change in the diff
     // is an SVN property change.
     $text = ltrim($text);
 
+    // Try to strip ANSI color codes from colorized diffs. ANSI color codes
+    // might be present in two cases:
+    //
+    //   - You piped a colorized diff into 'arc --raw' or similar (normally
+    //     we're able to disable colorization on diffs we control the generation
+    //     of).
+    //   - You're diffing a file which actually contains ANSI color codes.
+    //
+    // The former is vastly more likely, but we try to distinguish between the
+    // two cases by testing for a color code at the beginning of a line. If
+    // we find one, we know it's a colorized diff (since the beginning of the
+    // line should be "+", "-" or " " if the code is in the diff text).
+    //
+    // While it's possible a diff might be colorized and fail this test, it's
+    // unlikely, and it covers hg's color extension which seems to be the most
+    // stubborn about colorizing text despite stdout not being a TTY.
+    //
+    // We might incorrectly strip color codes from a colorized diff of a text
+    // file with color codes inside it, but this case is stupid and pathological
+    // and you've dug your own grave.
+
+    $ansi_color_pattern = '\x1B\[[\d;]*m';
+    if (preg_match('/^'.$ansi_color_pattern.'/m', $text)) {
+      $text = preg_replace('/'.$ansi_color_pattern.'/', '', $text);
+    }
+
     $this->text = explode("\n", $text);
     $this->line = 0;
   }
@@ -981,8 +1006,9 @@ final class ArcanistDiffParser {
     $context = '';
     for ($ii = $min; $ii <= $max; $ii++) {
       $context .= sprintf(
-        "%8.8s %s\n",
+        "%8.8s %6.6s   %s\n",
         ($ii == $this->line) ? '>>>  ' : '',
+        $ii + 1,
         $this->text[$ii]);
     }
 
index 3903de08caf5ec7426e39944c93c2243e63a827d..0508993c4900538de4e5bd947e0162b0873144b6 100644 (file)
@@ -38,6 +38,9 @@ final class ArcanistDiffParserTestCase extends ArcanistPhutilTestCase {
     $changes = $parser->parseDiff($contents);
 
     switch ($file) {
+      case 'colorized.hggitdiff':
+        $this->assertEqual(1, count($changes));
+        break;
       case 'basic-missing-both-newlines-plus.udiff':
       case 'basic-missing-both-newlines.udiff':
       case 'basic-missing-new-newline-plus.udiff':
diff --git a/src/parser/diff/__tests__/data/colorized.hggitdiff b/src/parser/diff/__tests__/data/colorized.hggitdiff
new file mode 100644 (file)
index 0000000..117d043
--- /dev/null
@@ -0,0 +1,8 @@
+\e[0;1mdiff --git a/FILE b/FILE\e[0m
+\e[0;31;1m--- a/FILE\e[0m
+\e[0;32;1m+++ b/FILE\e[0m
+\e[0;35m@@ -1,3 +1,4 @@\e[0m
+ asdf
+ asdf
+ quack
+\e[0;32m+z\e[0m