|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @version $Id: diff.php 8765 2008-08-16 22:18:25Z aptx $
|
|
|
ef5584 |
* @copyright (c) 2006 phpBB Group
|
|
|
ef5584 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
ef5584 |
*
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @ignore
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
if (!defined('IN_PHPBB'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
exit;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Code from pear.php.net, Text_Diff-1.0.0 package
|
|
|
ef5584 |
* http://pear.php.net/package/Text_Diff/
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Modified by phpBB Group to meet our coding standards
|
|
|
ef5584 |
* and being able to integrate into phpBB
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* General API for generating and formatting diffs - the differences between
|
|
|
ef5584 |
* two sequences of strings.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff
|
|
|
ef5584 |
{
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Array of changes.
|
|
|
ef5584 |
* @var array
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
var $_edits;
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Computes diffs between sequences of strings.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @param array $from_lines An array of strings. Typically these are lines from a file.
|
|
|
ef5584 |
* @param array $to_lines An array of strings.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function diff(&$from_content, &$to_content, $preserve_cr = true)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$diff_engine = new diff_engine();
|
|
|
ef5584 |
$this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Returns the array of differences.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function get_diff()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return $this->_edits;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Computes a reversed diff.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Example:
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* $diff = new diff($lines1, $lines2);
|
|
|
ef5584 |
* $rev = $diff->reverse();
|
|
|
ef5584 |
*
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @return diff A Diff object representing the inverse of the original diff.
|
|
|
ef5584 |
* Note that we purposely don't return a reference here, since
|
|
|
ef5584 |
* this essentially is a clone() method.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function reverse()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (version_compare(zend_version(), '2', '>'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$rev = clone($this);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$rev = $this;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$rev->_edits = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$rev->_edits[] = $edit->reverse();
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $rev;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Checks for an empty diff.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @return boolean True if two sequences were identical.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function is_empty()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (!is_a($edit, 'diff_op_copy'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
return true;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Computes the length of the Longest Common Subsequence (LCS).
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This is mostly for diagnostic purposes.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @return integer The length of the LCS.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function lcs()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lcs = 0;
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (is_a($edit, 'diff_op_copy'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lcs += sizeof($edit->orig);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
return $lcs;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Gets the original set of lines.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This reconstructs the $from_lines parameter passed to the constructor.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @return array The original sequence of strings.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function get_original()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->orig)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
array_splice($lines, sizeof($lines), 0, $edit->orig);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
return $lines;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Gets the final set of lines.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This reconstructs the $to_lines parameter passed to the constructor.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @return array The sequence of strings.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function get_final()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->final)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
array_splice($lines, sizeof($lines), 0, $edit->final);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
return $lines;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Removes trailing newlines from a line of text. This is meant to be used with array_walk().
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @param string &$line The line to trim.
|
|
|
ef5584 |
* @param integer $key The index of the line in the array. Not used.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function trim_newlines(&$line, $key)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$line = str_replace(array("\n", "\r"), '', $line);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Checks a diff for validity.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This is here only for debugging purposes.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function _check($from_lines, $to_lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (serialize($from_lines) != serialize($this->get_original()))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
trigger_error("[diff] Reconstructed original doesn't match", E_USER_ERROR);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if (serialize($to_lines) != serialize($this->get_final()))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
trigger_error("[diff] Reconstructed final doesn't match", E_USER_ERROR);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$rev = $this->reverse();
|
|
|
ef5584 |
|
|
|
ef5584 |
if (serialize($to_lines) != serialize($rev->get_original()))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
trigger_error("[diff] Reversed original doesn't match", E_USER_ERROR);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if (serialize($from_lines) != serialize($rev->get_final()))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
trigger_error("[diff] Reversed final doesn't match", E_USER_ERROR);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$prevtype = null;
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($prevtype == get_class($edit))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$prevtype = get_class($edit);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return true;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class mapped_diff extends diff
|
|
|
ef5584 |
{
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Computes a diff between sequences of strings.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This can be used to compute things like case-insensitve diffs, or diffs
|
|
|
ef5584 |
* which ignore changes in white-space.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @param array $from_lines An array of strings.
|
|
|
ef5584 |
* @param array $to_lines An array of strings.
|
|
|
ef5584 |
* @param array $mapped_from_lines This array should have the same size number of elements as $from_lines.
|
|
|
ef5584 |
* The elements in $mapped_from_lines and $mapped_to_lines are what is actually
|
|
|
ef5584 |
* compared when computing the diff.
|
|
|
ef5584 |
* @param array $mapped_to_lines This array should have the same number of elements as $to_lines.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function mapped_diff(&$from_lines, &$to_lines, &$mapped_from_lines, &$mapped_to_lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (sizeof($from_lines) != sizeof($mapped_from_lines) || sizeof($to_lines) != sizeof($mapped_to_lines))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
parent::diff($mapped_from_lines, $mapped_to_lines);
|
|
|
ef5584 |
|
|
|
ef5584 |
$xi = $yi = 0;
|
|
|
ef5584 |
for ($i = 0; $i < sizeof($this->_edits); $i++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$orig = &$this->_edits[$i]->orig;
|
|
|
ef5584 |
if (is_array($orig))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$orig = array_slice($from_lines, $xi, sizeof($orig));
|
|
|
ef5584 |
$xi += sizeof($orig);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$final = &$this->_edits[$i]->final;
|
|
|
ef5584 |
if (is_array($final))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$final = array_slice($to_lines, $yi, sizeof($final));
|
|
|
ef5584 |
$yi += sizeof($final);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
var $orig;
|
|
|
ef5584 |
var $final;
|
|
|
ef5584 |
|
|
|
ef5584 |
function &reverse()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
trigger_error('[diff] Abstract method', E_USER_ERROR);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function norig()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return ($this->orig) ? sizeof($this->orig) : 0;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function nfinal()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return ($this->final) ? sizeof($this->final) : 0;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff_op_copy extends diff_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff_op_copy($orig, $final = false)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (!is_array($final))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$final = $orig;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$this->orig = $orig;
|
|
|
ef5584 |
$this->final = $final;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function &reverse()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$reverse = new diff_op_copy($this->final, $this->orig);
|
|
|
ef5584 |
return $reverse;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff_op_delete extends diff_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff_op_delete($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->orig = $lines;
|
|
|
ef5584 |
$this->final = false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function &reverse()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$reverse = new diff_op_add($this->orig);
|
|
|
ef5584 |
return $reverse;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff_op_add extends diff_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff_op_add($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->final = $lines;
|
|
|
ef5584 |
$this->orig = false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function &reverse()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$reverse = new diff_op_delete($this->final);
|
|
|
ef5584 |
return $reverse;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff_op_change extends diff_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff_op_change($orig, $final)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->orig = $orig;
|
|
|
ef5584 |
$this->final = $final;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function &reverse()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$reverse = new diff_op_change($this->final, $this->orig);
|
|
|
ef5584 |
return $reverse;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* A class for computing three way diffs.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff3 extends diff
|
|
|
ef5584 |
{
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Conflict counter.
|
|
|
ef5584 |
* @var integer
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
var $_conflicting_blocks = 0;
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Computes diff between 3 sequences of strings.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @param array $orig The original lines to use.
|
|
|
ef5584 |
* @param array $final1 The first version to compare to.
|
|
|
ef5584 |
* @param array $final2 The second version to compare to.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function diff3(&$orig, &$final1, &$final2)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$diff_engine = new diff_engine();
|
|
|
ef5584 |
|
|
|
ef5584 |
$diff_1 = $diff_engine->diff($orig, $final1);
|
|
|
ef5584 |
$diff_2 = $diff_engine->diff($orig, $final2);
|
|
|
ef5584 |
|
|
|
ef5584 |
unset($engine);
|
|
|
ef5584 |
|
|
|
ef5584 |
$this->_edits = $this->_diff3($diff_1, $diff_2);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Return merged output
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @param string $label1 the cvs file version/label from the original set of lines
|
|
|
ef5584 |
* @param string $label2 the cvs file version/label from the new set of lines
|
|
|
ef5584 |
* @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
|
|
|
ef5584 |
* @param bool $get_conflicts if set to true only the number of conflicts is returned
|
|
|
ef5584 |
* @param bool $merge_new if set to true the merged output will have the new file contents on a conflicting merge
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @return mixed the merged output
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function merged_output($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN', $get_conflicts = false, $merge_new = false)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
global $user;
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($get_conflicts)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->is_conflict())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_conflicting_blocks++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $this->_conflicting_blocks;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
|
|
|
ef5584 |
$label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;
|
|
|
ef5584 |
$label_sep = (!empty($user->lang[$label_sep])) ? $user->lang[$label_sep] : $label_sep;
|
|
|
ef5584 |
|
|
|
ef5584 |
$lines = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->is_conflict())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (!$merge_new)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')), $edit->final1, array('=======' . ($label_sep ? ' ' . $label_sep : '')), $edit->final2, array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, $edit->final1);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$this->_conflicting_blocks++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, $edit->merged());
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $lines;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Merge the output and use the new file code for conflicts
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function merged_new_output()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->is_conflict())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, $edit->final2);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, $edit->merged());
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $lines;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Merge the output and use the original file code for conflicts
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function merged_orig_output()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->is_conflict())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, $edit->final1);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lines = array_merge($lines, $edit->merged());
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $lines;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Get conflicting block(s)
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function get_conflicts()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$conflicts = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
foreach ($this->_edits as $edit)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($edit->is_conflict())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$conflicts[] = array($edit->final1, $edit->final2);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $conflicts;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function _diff3(&$edits1, &$edits2)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits = array();
|
|
|
ef5584 |
$bb = new diff3_block_builder();
|
|
|
ef5584 |
|
|
|
ef5584 |
$e1 = current($edits1);
|
|
|
ef5584 |
$e2 = current($edits2);
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($e1 || $e2)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($e1 && $e2 && is_a($e1, 'diff_op_copy') && is_a($e2, 'diff_op_copy'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// We have copy blocks from both diffs. This is the (only) time we want to emit a diff3 copy block.
|
|
|
ef5584 |
// Flush current diff3 diff block, if any.
|
|
|
ef5584 |
if ($edit = $bb->finish())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits[] = $edit;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$ncopy = min($e1->norig(), $e2->norig());
|
|
|
ef5584 |
$edits[] = new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($e1->norig() > $ncopy)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
array_splice($e1->orig, 0, $ncopy);
|
|
|
ef5584 |
array_splice($e1->final, 0, $ncopy);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$e1 = next($edits1);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($e2->norig() > $ncopy)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
array_splice($e2->orig, 0, $ncopy);
|
|
|
ef5584 |
array_splice($e2->final, 0, $ncopy);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$e2 = next($edits2);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($e1 && $e2)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($e1->orig && $e2->orig)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$norig = min($e1->norig(), $e2->norig());
|
|
|
ef5584 |
$orig = array_splice($e1->orig, 0, $norig);
|
|
|
ef5584 |
array_splice($e2->orig, 0, $norig);
|
|
|
ef5584 |
$bb->input($orig);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$norig = 0;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if (is_a($e1, 'diff_op_copy'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$bb->out1(array_splice($e1->final, 0, $norig));
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if (is_a($e2, 'diff_op_copy'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$bb->out2(array_splice($e2->final, 0, $norig));
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($e1 && ! $e1->orig)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$bb->out1($e1->final);
|
|
|
ef5584 |
$e1 = next($edits1);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($e2 && ! $e2->orig)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$bb->out2($e2->final);
|
|
|
ef5584 |
$e2 = next($edits2);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($edit = $bb->finish())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits[] = $edit;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $edits;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff3_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff3_op($orig = false, $final1 = false, $final2 = false)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->orig = $orig ? $orig : array();
|
|
|
ef5584 |
$this->final1 = $final1 ? $final1 : array();
|
|
|
ef5584 |
$this->final2 = $final2 ? $final2 : array();
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function merged()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (!isset($this->_merged))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($this->final1 === $this->final2)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_merged = &$this->final1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else if ($this->final1 === $this->orig)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_merged = &$this->final2;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else if ($this->final2 === $this->orig)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_merged = &$this->final1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_merged = false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $this->_merged;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function is_conflict()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return ($this->merged() === false) ? true : false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff3_op_copy extends diff3_op
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff3_op_copy($lines = false)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->orig = $lines ? $lines : array();
|
|
|
ef5584 |
$this->final1 = &$this->orig;
|
|
|
ef5584 |
$this->final2 = &$this->orig;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function merged()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return $this->orig;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function is_conflict()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff3_block_builder
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff3_block_builder()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_init();
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function input($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_append($this->orig, $lines);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function out1($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_append($this->final1, $lines);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function out2($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_append($this->final2, $lines);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function is_empty()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return !$this->orig && !$this->final1 && !$this->final2;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function finish()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($this->is_empty())
|
|
|
ef5584 |
{
|
|
|
ef5584 |
return false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edit = new diff3_op($this->orig, $this->final1, $this->final2);
|
|
|
ef5584 |
$this->_init();
|
|
|
ef5584 |
return $edit;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function _init()
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->orig = $this->final1 = $this->final2 = array();
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function _append(&$array, $lines)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
array_splice($array, sizeof($array), 0, $lines);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
?>
|