Created
June 28, 2013 06:15
-
-
Save Shyru/5882802 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* Copyright (c) 2012 File Factory | |
* @author Daniel Haas <daniel@file-factory.de> | |
*/ | |
/** | |
* Checks if there is a diff between $_file and $_otherFile. | |
* Returns true if there is a diff, false otherwise. | |
* | |
* First checks on filesize, then uses file_get_contents to compare files. | |
* | |
*/ | |
function diff_php_basic($_file,$_otherFile) | |
{ | |
if (filesize($_file)!=filesize($_otherFile)) return true; | |
else if (file_get_contents($_file)!=file_get_contents($_otherFile)) return true; | |
else return false; | |
} | |
/** | |
* Checks if there is a diff between $_file and $_otherFile. | |
* Returns true if there is a diff, false otherwise. | |
* | |
* First checks on filesize, then uses md5_file to compare files. | |
* | |
*/ | |
function diff_php_md5($_file,$_otherFile) | |
{ | |
if (filesize($_file)!=filesize($_otherFile)) return true; | |
else if (md5_file($_file)!=md5_file($_otherFile)) return true; | |
else return false; | |
} | |
/** | |
* Checks if there is a diff between $_file and $_otherFile. | |
* Returns true if there is a diff, false otherwise. | |
* | |
* Uses the external diff commandline utility and calls it with backtick operator. | |
* | |
*/ | |
function diff_external_diff_backtick($_file,$_otherFile) | |
{ | |
$diffOutput=`diff "$_file" "$_otherFile"`; | |
if ($diffOutput) return true; | |
else return false; | |
} | |
/** | |
* Checks if there is a diff between $_file and $_otherFile. | |
* Returns true if there is a diff, false otherwise. | |
* | |
* Uses the external diff commandline utility and calls it with exec() | |
* | |
*/ | |
function diff_external_diff_exec($_file,$_otherFile) | |
{ | |
$output=array(); | |
exec("diff \"$_file\" \"$_otherFile\"",$output); | |
$diffOutput=implode("",$output); | |
if ($diffOutput) return true; | |
else return false; | |
} | |
/** | |
* Calculates the differences between the two directories. | |
* Returns an array of files-names that differs. | |
* | |
* @param string $_directory The first directory | |
* @param string $_otherDirectory The second directory | |
* @param string $_diffMethod The diff method to use | |
* @return array Array with diffs | |
*/ | |
function diff_directories($_directory,$_otherDirectory,$_diffMethod) | |
{ | |
$diffs=array(); | |
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_directory), RecursiveIteratorIterator::SELF_FIRST); | |
foreach($objects as $name => $object){ | |
if ($object->isFile()) | |
{ | |
//echo "$name\n"; | |
$otherFile=str_replace($_directory,$_otherDirectory,$name); | |
//echo "$otherFile\n"; | |
if ($_diffMethod($name,$otherFile)) $diffs[]=$name; | |
} | |
} | |
return $diffs; | |
} | |
if ($argc<3) die("You must pass the path to two directories which are roughly the same."); | |
//diff_php_basic consumes max_filesize*2 amount of memory, so is disabled by default | |
$differs=array("diff_php_md5","diff_external_diff_backtick","diff_external_diff_exec"); | |
foreach ($differs as $differ) | |
{ | |
echo "Benching $differ:\n"; | |
$start=microtime(true); | |
$diffs=diff_directories($argv[1],$argv[2],$differ); | |
$end=microtime(true); | |
$timeTaken=round($end-$start,3); | |
echo "-> Found ".count($diffs)." differences, took $timeTaken seconds time.\n\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment