3
* A class for computing three way diffs.
5
* $Horde: framework/Text_Diff/Diff/ThreeWay.php,v 1.3.2.3 2008/01/04 10:37:27 jan Exp $
7
* Copyright 2007-2008 The Horde Project (http://www.horde.org/)
9
* See the enclosed file COPYING for license information (LGPL). If you did
10
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
17
require_once 'Text/Diff.php';
20
* A class for computing three way diffs.
23
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
25
class Text_Diff_ThreeWay extends Text_Diff {
32
var $_conflictingBlocks = 0;
35
* Computes diff between 3 sequences of strings.
37
* @param array $orig The original lines to use.
38
* @param array $final1 The first version to compare to.
39
* @param array $final2 The second version to compare to.
41
function Text_Diff_ThreeWay($orig, $final1, $final2)
43
if (extension_loaded('xdiff')) {
44
$engine = new Text_Diff_Engine_xdiff();
46
$engine = new Text_Diff_Engine_native();
49
$this->_edits = $this->_diff3($engine->diff($orig, $final1),
50
$engine->diff($orig, $final2));
55
function mergedOutput($label1 = false, $label2 = false)
58
foreach ($this->_edits as $edit) {
59
if ($edit->isConflict()) {
60
/* FIXME: this should probably be moved somewhere else. */
61
$lines = array_merge($lines,
62
array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')),
66
array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
67
$this->_conflictingBlocks++;
69
$lines = array_merge($lines, $edit->merged());
79
function _diff3($edits1, $edits2)
82
$bb = new Text_Diff_ThreeWay_BlockBuilder();
84
$e1 = current($edits1);
85
$e2 = current($edits2);
87
if ($e1 && $e2 && is_a($e1, 'Text_Diff_Op_copy') && is_a($e2, 'Text_Diff_Op_copy')) {
88
/* We have copy blocks from both diffs. This is the (only)
89
* time we want to emit a diff3 copy block. Flush current
90
* diff3 diff block, if any. */
91
if ($edit = $bb->finish()) {
95
$ncopy = min($e1->norig(), $e2->norig());
97
$edits[] = new Text_Diff_ThreeWay_Op_copy(array_slice($e1->orig, 0, $ncopy));
99
if ($e1->norig() > $ncopy) {
100
array_splice($e1->orig, 0, $ncopy);
101
array_splice($e1->final, 0, $ncopy);
106
if ($e2->norig() > $ncopy) {
107
array_splice($e2->orig, 0, $ncopy);
108
array_splice($e2->final, 0, $ncopy);
114
if ($e1->orig && $e2->orig) {
115
$norig = min($e1->norig(), $e2->norig());
116
$orig = array_splice($e1->orig, 0, $norig);
117
array_splice($e2->orig, 0, $norig);
121
if (is_a($e1, 'Text_Diff_Op_copy')) {
122
$bb->out1(array_splice($e1->final, 0, $norig));
125
if (is_a($e2, 'Text_Diff_Op_copy')) {
126
$bb->out2(array_splice($e2->final, 0, $norig));
130
if ($e1 && ! $e1->orig) {
131
$bb->out1($e1->final);
134
if ($e2 && ! $e2->orig) {
135
$bb->out2($e2->final);
141
if ($edit = $bb->finish()) {
152
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
156
class Text_Diff_ThreeWay_Op {
158
function Text_Diff_ThreeWay_Op($orig = false, $final1 = false, $final2 = false)
160
$this->orig = $orig ? $orig : array();
161
$this->final1 = $final1 ? $final1 : array();
162
$this->final2 = $final2 ? $final2 : array();
167
if (!isset($this->_merged)) {
168
if ($this->final1 === $this->final2) {
169
$this->_merged = &$this->final1;
170
} elseif ($this->final1 === $this->orig) {
171
$this->_merged = &$this->final2;
172
} elseif ($this->final2 === $this->orig) {
173
$this->_merged = &$this->final1;
175
$this->_merged = false;
179
return $this->_merged;
182
function isConflict()
184
return $this->merged() === false;
191
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
195
class Text_Diff_ThreeWay_Op_copy extends Text_Diff_ThreeWay_Op {
197
function Text_Diff_ThreeWay_Op_Copy($lines = false)
199
$this->orig = $lines ? $lines : array();
200
$this->final1 = &$this->orig;
201
$this->final2 = &$this->orig;
209
function isConflict()
218
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
222
class Text_Diff_ThreeWay_BlockBuilder {
224
function Text_Diff_ThreeWay_BlockBuilder()
229
function input($lines)
232
$this->_append($this->orig, $lines);
236
function out1($lines)
239
$this->_append($this->final1, $lines);
243
function out2($lines)
246
$this->_append($this->final2, $lines);
252
return !$this->orig && !$this->final1 && !$this->final2;
257
if ($this->isEmpty()) {
260
$edit = new Text_Diff_ThreeWay_Op($this->orig, $this->final1, $this->final2);
268
$this->orig = $this->final1 = $this->final2 = array();
271
function _append(&$array, $lines)
273
array_splice($array, sizeof($array), 0, $lines);