]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add isHeuristicBinaryFile(), a git-like check for binariness of fiels
authorepriestley <git@epriestley.com>
Mon, 24 Oct 2011 17:33:34 +0000 (10:33 -0700)
committerepriestley <git@epriestley.com>
Mon, 24 Oct 2011 18:55:11 +0000 (11:55 -0700)
Summary:
This is separated from D812. See D812, D1008, D1009. Separation allows us to
land these patches safely.

See T452 for a broader discussion of the issues involved.

Test Plan: See D812.

Reviewers: davidreuss, jungejason, nh, tuomaspelkonen, aran

Reviewed By: davidreuss

CC: aran, davidreuss
Differential Revision: 1040

src/difference/ArcanistDiffUtils.php

index 9949d4648cb35e337881461c500390b148f5f0d3..f41e01dc21e476f2d6cc29803afa093352a16e63 100644 (file)
  */
 final class ArcanistDiffUtils {
 
+  /**
+   * Make a best-effort attempt to determine if a file is definitely binary.
+   *
+   * @return bool If true, the file is almost certainly binary. If false, the
+   *              file might still be binary but is subtle about it.
+   */
+  public static function isHeuristicBinaryFile($data) {
+    // Detect if a file is binary according to the Git heuristic, which is the
+    // presence of NULL ("\0") bytes. Git only examines the first "few" bytes of
+    // each file (8KB or so) as an optimization, but we don't have a reasonable
+    // equivalent in PHP, so just look at all of it.
+    return (strpos($data, "\0") !== false);
+  }
+
   public static function renderDifferences(
     $old,
     $new,