]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Support other encodings in ArcanistDiffParser
authorDavid Reuss <david@upsys.dk>
Fri, 28 Oct 2011 15:01:21 +0000 (08:01 -0700)
committerepriestley <git@epriestley.com>
Fri, 28 Oct 2011 15:01:53 +0000 (08:01 -0700)
Test Plan:
Tried viewing a diff detected as binary in diffusion with and without
setting my desired encoding. Worked as expected.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, epriestley
Differential Revision: 1008

src/parser/diff/ArcanistDiffParser.php
src/parser/diff/__init__.php

index f550d4c0a8b922fab86e435658b06c62a0e245c0..a31fc98057918ecedffc25449a74e9bd08f64a60 100644 (file)
@@ -29,6 +29,7 @@ class ArcanistDiffParser {
   protected $isGit;
   protected $isMercurial;
   protected $detectBinaryFiles = false;
+  protected $tryEncoding;
 
   protected $changes = array();
   private $forcePath;
@@ -47,6 +48,10 @@ class ArcanistDiffParser {
     return $this;
   }
 
+  public function setTryEncoding($encoding) {
+    $this->tryEncoding = $encoding;
+  }
+
   public function forcePath($path) {
     $this->forcePath = $path;
     return $this;
@@ -807,6 +812,21 @@ class ArcanistDiffParser {
       $is_binary = false;
       if ($this->detectBinaryFiles) {
         $is_binary = !phutil_is_utf8($corpus);
+
+        if ($is_binary && $this->tryEncoding) {
+          $is_binary = ArcanistDiffUtils::isHeuristicBinaryFile($corpus);
+          if (!$is_binary) {
+              // NOTE: This feature is HIGHLY EXPERIMENTAL and will cause a lot
+              // of issues. Use it at your own risk.
+              $corpus = mb_convert_encoding(
+                  $corpus, 'UTF-8', $this->tryEncoding);
+              if (!phutil_is_utf8($corpus)) {
+                  throw new Exception(
+                      'Failed converting hunk to '.$this->tryEncoding);
+              }
+          }
+        }
+
       }
 
       if ($is_binary) {
index 58f3eb33b7ce9068faebf22fafc7972c4dcec55b..fa33bdf13646fb213c63e9b5d88d75f752b8f260 100644 (file)
@@ -6,6 +6,7 @@
 
 
 
+phutil_require_module('arcanist', 'difference');
 phutil_require_module('arcanist', 'parser/diff/change');
 phutil_require_module('arcanist', 'parser/diff/changetype');
 phutil_require_module('arcanist', 'parser/diff/hunk');