2013-12-07 02:43:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhragmentPatchUtil extends Phobject {
|
|
|
|
|
2014-06-09 20:36:49 +02:00
|
|
|
const EMPTY_HASH = '0000000000000000000000000000000000000000';
|
2013-12-07 02:43:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the DiffMatchPatch patch between two Phabricator files.
|
|
|
|
*
|
|
|
|
* @phutil-external-symbol class diff_match_patch
|
|
|
|
*/
|
2013-12-08 02:14:34 +01:00
|
|
|
public static function calculatePatch(
|
2013-12-07 02:43:49 +01:00
|
|
|
PhabricatorFile $old = null,
|
|
|
|
PhabricatorFile $new = null) {
|
|
|
|
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
require_once $root.'/externals/diff_match_patch/diff_match_patch.php';
|
|
|
|
|
|
|
|
$old_hash = self::EMPTY_HASH;
|
|
|
|
$new_hash = self::EMPTY_HASH;
|
|
|
|
|
|
|
|
if ($old !== null) {
|
|
|
|
$old_hash = $old->getContentHash();
|
|
|
|
}
|
|
|
|
if ($new !== null) {
|
|
|
|
$new_hash = $new->getContentHash();
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:36:49 +02:00
|
|
|
$old_content = '';
|
|
|
|
$new_content = '';
|
2013-12-07 02:43:49 +01:00
|
|
|
|
|
|
|
if ($old_hash === $new_hash) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($old_hash !== self::EMPTY_HASH) {
|
|
|
|
$old_content = $old->loadFileData();
|
|
|
|
} else {
|
2014-06-09 20:36:49 +02:00
|
|
|
$old_content = '';
|
2013-12-07 02:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($new_hash !== self::EMPTY_HASH) {
|
|
|
|
$new_content = $new->loadFileData();
|
|
|
|
} else {
|
2014-06-09 20:36:49 +02:00
|
|
|
$new_content = '';
|
2013-12-07 02:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$dmp = new diff_match_patch();
|
|
|
|
$dmp_patches = $dmp->patch_make($old_content, $new_content);
|
|
|
|
return $dmp->patch_toText($dmp_patches);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|