[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phragment/util/ -> PhragmentPatchUtil.php (source)

   1  <?php
   2  
   3  final class PhragmentPatchUtil extends Phobject {
   4  
   5    const EMPTY_HASH = '0000000000000000000000000000000000000000';
   6  
   7    /**
   8     * Calculate the DiffMatchPatch patch between two Phabricator files.
   9     *
  10     * @phutil-external-symbol class diff_match_patch
  11     */
  12    public static function calculatePatch(
  13      PhabricatorFile $old = null,
  14      PhabricatorFile $new = null) {
  15  
  16      $root = dirname(phutil_get_library_root('phabricator'));
  17      require_once $root.'/externals/diff_match_patch/diff_match_patch.php';
  18  
  19      $old_hash = self::EMPTY_HASH;
  20      $new_hash = self::EMPTY_HASH;
  21  
  22      if ($old !== null) {
  23        $old_hash = $old->getContentHash();
  24      }
  25      if ($new !== null) {
  26        $new_hash = $new->getContentHash();
  27      }
  28  
  29      $old_content = '';
  30      $new_content = '';
  31  
  32      if ($old_hash === $new_hash) {
  33        return null;
  34      }
  35  
  36      if ($old_hash !== self::EMPTY_HASH) {
  37        $old_content = $old->loadFileData();
  38      } else {
  39        $old_content = '';
  40      }
  41  
  42      if ($new_hash !== self::EMPTY_HASH) {
  43        $new_content = $new->loadFileData();
  44      } else {
  45        $new_content = '';
  46      }
  47  
  48      $dmp = new diff_match_patch();
  49      $dmp_patches = $dmp->patch_make($old_content, $new_content);
  50      return $dmp->patch_toText($dmp_patches);
  51    }
  52  
  53  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1