5 // A class for computing three way diffs
7 // Copyright (C) 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
8 // You may copy this code freely under the conditions of the GPL.
11 require_once 'lib/difflib.php';
17 function _Diff3_Block($orig = false, $final1 = false, $final2 = false)
19 $this->orig = $orig ? $orig : array();
20 $this->final1 = $final1 ? $final1 : array();
21 $this->final2 = $final2 ? $final2 : array();
26 if (!isset($this->_merged)) {
27 if ($this->final1 === $this->final2)
28 $this->_merged = &$this->final1;
29 elseif ($this->final1 === $this->orig)
30 $this->_merged = &$this->final2; elseif ($this->final2 === $this->orig)
31 $this->_merged = &$this->final1; else
32 $this->_merged = false;
34 return $this->_merged;
37 function is_conflict()
39 return $this->merged() === false;
43 class _Diff3_CopyBlock extends _Diff3_Block
47 function _Diff3_CopyBlock($lines = false)
49 $this->orig = $lines ? $lines : array();
50 $this->final1 = &$this->orig;
51 $this->final2 = &$this->orig;
59 function is_conflict()
65 class _Diff3_BlockBuilder
67 function _Diff3_BlockBuilder()
74 $this->orig = $this->final1 = $this->final2 = array();
77 function _append(&$array, $lines)
79 array_splice($array, sizeof($array), 0, $lines);
82 function input($lines)
85 $this->_append($this->orig, $lines);
91 $this->_append($this->final1, $lines);
97 $this->_append($this->final2, $lines);
102 return !$this->orig && !$this->final1 && !$this->final2;
107 if ($this->is_empty())
110 $block = new _Diff3_Block($this->orig, $this->final1, $this->final2);
121 function Diff3($orig, $final1, $final2)
123 $eng = new _DiffEngine;
124 $this->ConflictingBlocks = 0; //Conflict counter
125 $this->blocks = $this->__diff3($eng->diff($orig, $final1),
126 $eng->diff($orig, $final2));
129 function __diff3($edits1, $edits2)
132 $bb = new _Diff3_BlockBuilder;
134 $e1 = current($edits1);
135 $e2 = current($edits2);
142 if ($e1 && $e2 && $e1->type == 'copy' && $e2->type == 'copy') {
143 // We have copy blocks from both diffs. This is the (only)
144 // time we want to emit a diff3 copy block.
145 // Flush current diff3 diff block, if any
146 if ($block = $bb->finish())
149 $ncopy = min($e1->norig(), $e2->norig());
151 $blocks[] = new _Diff3_CopyBlock(array_slice($e1->orig, 0, $ncopy));
153 if ($e1->norig() > $ncopy) {
154 array_splice($e1->orig, 0, $ncopy);
155 array_splice($e1->final, 0, $ncopy);
159 if ($e2->norig() > $ncopy) {
160 array_splice($e2->orig, 0, $ncopy);
161 array_splice($e2->final, 0, $ncopy);
166 if ($e1->orig && $e2->orig) {
167 $norig = min($e1->norig(), $e2->norig());
168 $orig = array_splice($e1->orig, 0, $norig);
169 array_splice($e2->orig, 0, $norig);
173 if ($e1->type == 'copy')
174 $bb->out1(array_splice($e1->final, 0, $norig));
176 if ($e2->type == 'copy')
177 $bb->out2(array_splice($e2->final, 0, $norig));
179 if ($e1 && !$e1->orig) {
180 $bb->out1($e1->final);
183 if ($e2 && !$e2->orig) {
184 $bb->out2($e2->final);
190 if ($block = $bb->finish())
196 function merged_output($label1 = false, $label2 = false)
199 foreach ($this->blocks as $block) {
200 if ($block->is_conflict()) {
201 // FIXME: this should probably be moved somewhere else...
202 $lines = array_merge($lines,
203 array("<<<<<<<" . ($label1 ? " $label1" : '')),
207 array(">>>>>>>" . ($label2 ? " $label2" : '')));
208 $this->ConflictingBlocks++;
210 $lines = array_merge($lines, $block->merged());
221 // c-hanging-comment-ender-p: nil
222 // indent-tabs-mode: nil