forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhragmentPatchUtil.php
53 lines (41 loc) · 1.22 KB
/
PhragmentPatchUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
final class PhragmentPatchUtil extends Phobject {
const EMPTY_HASH = "0000000000000000000000000000000000000000";
/**
* Calculate the DiffMatchPatch patch between two Phabricator files.
*
* @phutil-external-symbol class diff_match_patch
*/
public static function calculatePatch(
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();
}
$old_content = "";
$new_content = "";
if ($old_hash === $new_hash) {
return null;
}
if ($old_hash !== self::EMPTY_HASH) {
$old_content = $old->loadFileData();
} else {
$old_content = "";
}
if ($new_hash !== self::EMPTY_HASH) {
$new_content = $new->loadFileData();
} else {
$new_content = "";
}
$dmp = new diff_match_patch();
$dmp_patches = $dmp->patch_make($old_content, $new_content);
return $dmp->patch_toText($dmp_patches);
}
}