]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Respect custom set encoding in patch/export workflows
authorDavid Reuss <david@upsys.dk>
Wed, 14 Mar 2012 14:08:06 +0000 (07:08 -0700)
committerepriestley <git@epriestley.com>
Wed, 14 Mar 2012 14:08:21 +0000 (07:08 -0700)
Summary:
In cases where a codebase is not UTF-8, we will attempt an conversion,
if an alternative encoding is given/configured.

This is now possible in two ways:

  - by configuring one under repository tracking in diffusion
  - by passing an --encoding option to the workflow

If the first is not available we will make a conduit call
to do an extra check and see if an encoding is configured directly with
phabricator.

Test Plan:
Tried various diffs with known encodings (mostly ISO-8859-1), and passed
it in, via stdin, or downloaded a known problematic revision from
phabricator, and they applied where they otherwise failed.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, epriestley
Maniphest Tasks: T452

Differential Revision: https://secure.phabricator.com/D1880

src/parser/bundle/ArcanistBundle.php
src/workflow/base/ArcanistBaseWorkflow.php
src/workflow/diff/ArcanistDiffWorkflow.php
src/workflow/export/ArcanistExportWorkflow.php
src/workflow/export/__init__.php
src/workflow/patch/ArcanistPatchWorkflow.php

index 0a089be57d9ac9a4abd2e5d2ed8376cd78805b5b..efd5a28959e2ffd9d14746f2f134cd97287364df 100644 (file)
@@ -30,6 +30,7 @@ final class ArcanistBundle {
   private $projectID;
   private $baseRevision;
   private $revisionID;
+  private $encoding;
 
   public function setConduit(ConduitClient $conduit) {
     $this->conduit = $conduit;
@@ -47,6 +48,15 @@ final class ArcanistBundle {
     $this->baseRevision = $base_revision;
   }
 
+  public function setEncoding($encoding) {
+    $this->encoding = $encoding;
+    return $this;
+  }
+
+  public function getEncoding() {
+    return $this->encoding;
+  }
+
   public function getBaseRevision() {
     return $this->baseRevision;
   }
@@ -86,12 +96,14 @@ final class ArcanistBundle {
       $project_name  = idx($meta_info, 'projectName');
       $base_revision = idx($meta_info, 'baseRevision');
       $revision_id   = idx($meta_info, 'revisionID');
+      $encoding      = idx($meta_info, 'encoding');
     // this arc bundle was probably made before we started storing meta info
     } else {
       $version       = 0;
       $project_name  = null;
       $base_revision = null;
       $revision_id   = null;
+      $encoding      = null;
     }
 
     $future = new ExecFuture(
@@ -117,6 +129,7 @@ final class ArcanistBundle {
     $obj->setProjectID($project_name);
     $obj->setBaseRevision($base_revision);
     $obj->setRevisionID($revision_id);
+    $obj->setEncoding($encoding);
 
     return $obj;
   }
@@ -168,6 +181,7 @@ final class ArcanistBundle {
       'projectName'  => $this->getProjectID(),
       'baseRevision' => $this->getBaseRevision(),
       'revisionID'   => $this->getRevisionID(),
+      'encoding'     => $this->getEncoding(),
     );
 
     $dir = Filesystem::createTemporaryDirectory();
@@ -230,7 +244,8 @@ final class ArcanistBundle {
       $result[] = $this->buildHunkChanges($change->getHunks());
     }
 
-    return implode("\n", $result)."\n";
+    $diff = implode("\n", $result)."\n";
+    return $this->convertNonUTF8Diff($diff);
   }
 
   public function toGitPatch() {
@@ -374,7 +389,24 @@ final class ArcanistBundle {
       }
       $result[] = $change_body;
     }
-    return implode("\n", $result)."\n";
+
+    $diff = implode("\n", $result)."\n";
+    return $this->convertNonUTF8Diff($diff);
+  }
+
+  private function convertNonUTF8Diff($diff) {
+    $try_encoding_is_non_utf8 =
+      ($this->encoding && strtoupper($this->encoding) != 'UTF-8');
+    if ($try_encoding_is_non_utf8) {
+      $diff = mb_convert_encoding($diff, $this->encoding, 'UTF-8');
+      if (!$diff) {
+        throw new Exception(
+          "Attempted conversion of diff to encoding ".
+          "'{$this->encoding}' failed. Have you specified ".
+          "the proper encoding correctly?");
+      }
+    }
+    return $diff;
   }
 
   public function getChanges() {
@@ -504,6 +536,7 @@ final class ArcanistBundle {
   }
 
   private function buildHunkChanges(array $hunks) {
+
     $result = array();
     foreach ($hunks as $hunk) {
       $small_hunks = $this->breakHunkIntoSmallHunks($hunk);
index 90909f083fa396d4b4b5d224e3c4b9851542e37f..f5f9cecc57feb80946e65b8ede4148925c853eee 100644 (file)
@@ -64,6 +64,8 @@ abstract class ArcanistBaseWorkflow {
   private $arguments;
   private $command;
 
+  private $repositoryEncoding;
+
   private $arcanistConfiguration;
   private $parentWorkflow;
   private $workingDirectory;
@@ -1126,4 +1128,18 @@ abstract class ArcanistBaseWorkflow {
     return $repository_api->getPath('.arc/'.$path);
   }
 
+  protected function getRepositoryEncoding() {
+    if ($this->repositoryEncoding) {
+      return $this->repositoryEncoding;
+    }
+
+    $project_info = $this->getConduit()->callMethodSynchronous(
+      'arcanist.projectinfo',
+      array(
+        'name' => $this->getWorkingCopy()->getProjectID(),
+      ));
+    $this->repositoryEncoding = nonempty($project_info['encoding'], 'UTF-8');
+    return $this->repositoryEncoding;
+  }
+
 }
index f8345ee9082075830411426d014d1227d3e6ed60..18e365b1a32a2ecf8f6c8b5f76b812bb18b055da 100644 (file)
@@ -751,7 +751,7 @@ EOTEXT
       }
     }
 
-    $try_encoding = null;
+    $try_encoding = nonempty($this->getArgument('encoding'), null);
 
     $utf8_problems = array();
     foreach ($changes as $change) {
@@ -764,29 +764,21 @@ EOTEXT
           // liberal about what they're willing to process.
           $is_binary = ArcanistDiffUtils::isHeuristicBinaryFile($corpus);
           if (!$is_binary) {
-            $try_encoding = nonempty($this->getArgument('encoding'), null);
-            if ($try_encoding === null) {
-              // Make a call to check if there's an encoding specified for this
-              // project.
+
+            if (!$try_encoding) {
               try {
-                  $project_info = $this->getConduit()->callMethodSynchronous(
-                      'arcanist.projectinfo',
-                      array(
-                          'name' => $this->getWorkingCopy()->getProjectID(),
-                      ));
-                  $try_encoding = nonempty($project_info['encoding'], false);
+                $try_encoding = $this->getRepositoryEncoding();
               } catch (ConduitClientException $e) {
-                  if ($e->getErrorCode() == 'ERR-BAD-ARCANIST-PROJECT') {
-                      echo phutil_console_wrap(
-                          "Lookup of encoding in arcanist project failed\n".
-                          $e->getMessage()
-                      );
-                      $try_encoding = false;
-                  } else {
-                      throw $e;
-                  }
+                if ($e->getErrorCode() == 'ERR-BAD-ARCANIST-PROJECT') {
+                  echo phutil_console_wrap(
+                    "Lookup of encoding in arcanist project failed\n".
+                    $e->getMessage());
+                } else {
+                  throw $e;
+                }
               }
             }
+
             if ($try_encoding) {
               // NOTE: This feature is HIGHLY EXPERIMENTAL and will cause a lot
               // of issues. Use it at your own risk.
index 7b8873bd1eb448ba00d764b03e498112a030ff57..1d933f5f165f885c7d2b37d73fadd711fe904345 100644 (file)
@@ -75,6 +75,11 @@ EOTEXT
           "Export change as an arc bundle. This format can represent all ".
           "changes. These bundles can be applied with 'arc patch'.",
       ),
+      'encoding' => array(
+        'param' => 'encoding',
+        'help' =>
+          "Attempt to convert non UTF-8 patch into specified encoding.",
+      ),
       'revision' => array(
         'param' => 'revision_id',
         'help' =>
@@ -209,6 +214,19 @@ EOTEXT
         break;
     }
 
+    $try_encoding = nonempty($this->getArgument('encoding'), null);
+    if (!$try_encoding) {
+      try {
+        $try_encoding = $this->getRepositoryEncoding();
+      } catch (ConduitClientException $e) {
+        $try_encoding = null;
+      }
+    }
+
+    if ($try_encoding) {
+      $bundle->setEncoding($try_encoding);
+    }
+
     $format = $this->getFormat();
 
     switch ($format) {
index 7a729db8522fabdebc99a3b735af353030fb60f7..cae0583526cd33ae27a0a06113420e6e4eecf9cb 100644 (file)
@@ -12,6 +12,7 @@ phutil_require_module('arcanist', 'parser/diff');
 phutil_require_module('arcanist', 'workflow/base');
 
 phutil_require_module('phutil', 'console');
+phutil_require_module('phutil', 'utils');
 
 
 phutil_require_source('ArcanistExportWorkflow.php');
index 8953c94bb3c303cee9ca7e6fc949e560f01c9e8d..2241fe66446ef44991e838cb09371c76dc2d9e79 100644 (file)
@@ -81,6 +81,11 @@ EOTEXT
         'help' =>
           "Apply changes from a git patchfile or unified patchfile.",
       ),
+      'encoding' => array(
+        'param' => 'encoding',
+        'help' =>
+          "Attempt to convert non UTF-8 patch into specified encoding.",
+      ),
       'update' => array(
         'supports' => array(
           'git', 'svn', 'hg'
@@ -359,6 +364,20 @@ EOTEXT
         throw $ex;
       }
     }
+
+    $try_encoding = nonempty($this->getArgument('encoding'), null);
+    if (!$try_encoding) {
+      try {
+        $try_encoding = $this->getRepositoryEncoding();
+      } catch (ConduitClientException $e) {
+        $try_encoding = null;
+      }
+    }
+
+    if ($try_encoding) {
+      $bundle->setEncoding($try_encoding);
+    }
+
     $force = $this->getArgument('force', false);
     if ($force) {
       // force means don't do any sanity checks about the patch