<?php
-// $Id: diff3.php 7964 2011-03-05 17:05:30Z vargenau $
+
// diff3.php
//
// A class for computing three way diffs
require_once 'lib/difflib.php';
-class _Diff3_Block {
- var $type = 'diff3';
+class _Diff3_Block
+{
+ public $type = 'diff3';
- function _Diff3_Block ($orig = false, $final1 = false, $final2 = false) {
+ function _Diff3_Block($orig = false, $final1 = false, $final2 = false)
+ {
$this->orig = $orig ? $orig : array();
$this->final1 = $final1 ? $final1 : array();
$this->final2 = $final2 ? $final2 : array();
}
- function merged () {
+ function merged()
+ {
if (!isset($this->_merged)) {
if ($this->final1 === $this->final2)
$this->_merged = &$this->final1;
elseif ($this->final1 === $this->orig)
- $this->_merged = &$this->final2;
- elseif ($this->final2 === $this->orig)
- $this->_merged = &$this->final1;
- else
+ $this->_merged = &$this->final2; elseif ($this->final2 === $this->orig)
+ $this->_merged = &$this->final1; else
$this->_merged = false;
}
return $this->_merged;
}
- function is_conflict () {
+ function is_conflict()
+ {
return $this->merged() === false;
}
}
+class _Diff3_CopyBlock extends _Diff3_Block
+{
+ public $type = 'copy';
-class _Diff3_CopyBlock extends _Diff3_Block {
- var $type = 'copy';
-
- function _Diff3_CopyBlock ($lines = false) {
+ function _Diff3_CopyBlock($lines = false)
+ {
$this->orig = $lines ? $lines : array();
$this->final1 = &$this->orig;
$this->final2 = &$this->orig;
}
- function merged() {
+ function merged()
+ {
return $this->orig;
}
- function is_conflict () {
+ function is_conflict()
+ {
return false;
}
}
-class _Diff3_BlockBuilder {
- function _Diff3_BlockBuilder () {
+class _Diff3_BlockBuilder
+{
+ function _Diff3_BlockBuilder()
+ {
$this->_init();
}
- function _init() {
+ function _init()
+ {
$this->orig = $this->final1 = $this->final2 = array();
}
-
- function _append (&$array, $lines) {
+ function _append(&$array, $lines)
+ {
array_splice($array, sizeof($array), 0, $lines);
}
- function input($lines) {
+ function input($lines)
+ {
if ($lines)
$this->_append($this->orig, $lines);
}
- function out1($lines) {
+ function out1($lines)
+ {
if ($lines)
$this->_append($this->final1, $lines);
}
- function out2($lines) {
+ function out2($lines)
+ {
if ($lines)
$this->_append($this->final2, $lines);
}
- function is_empty() {
+ function is_empty()
+ {
return !$this->orig && !$this->final1 && !$this->final2;
}
- function finish() {
+ function finish()
+ {
if ($this->is_empty())
return false;
else {
return $block;
}
}
-};
-
+}
-class Diff3 {
- function Diff3 ($orig, $final1, $final2) {
+class Diff3
+{
+ function Diff3($orig, $final1, $final2)
+ {
$eng = new _DiffEngine;
- $this->ConflictingBlocks = 0; //Conflict counter
+ $this->ConflictingBlocks = 0; //Conflict counter
$this->blocks = $this->__diff3($eng->diff($orig, $final1),
- $eng->diff($orig, $final2));
+ $eng->diff($orig, $final2));
}
- function __diff3($edits1, $edits2) {
+ function __diff3($edits1, $edits2)
+ {
$blocks = array();
$bb = new _Diff3_BlockBuilder;
if ($e1->norig() > $ncopy) {
array_splice($e1->orig, 0, $ncopy);
array_splice($e1->final, 0, $ncopy);
- }
- else
+ } else
$e1 = next($edits1);
if ($e2->norig() > $ncopy) {
array_splice($e2->orig, 0, $ncopy);
array_splice($e2->final, 0, $ncopy);
- }
- else
+ } else
$e2 = next($edits2);
- }
- else {
+ } else {
if ($e1 && $e2) {
if ($e1->orig && $e2->orig) {
$norig = min($e1->norig(), $e2->norig());
if ($e2->type == 'copy')
$bb->out2(array_splice($e2->final, 0, $norig));
}
- if ($e1 && ! $e1->orig) {
+ if ($e1 && !$e1->orig) {
$bb->out1($e1->final);
$e1 = next($edits1);
}
- if ($e2 && ! $e2->orig) {
+ if ($e2 && !$e2->orig) {
$bb->out2($e2->final);
$e2 = next($edits2);
}
return $blocks;
}
-
- function merged_output($label1 = false, $label2 = false) {
+ function merged_output($label1 = false, $label2 = false)
+ {
$lines = array();
foreach ($this->blocks as $block) {
if ($block->is_conflict()) {
// FIXME: this should probably be moved somewhere else...
$lines = array_merge($lines,
- array("<<<<<<<" . ($label1 ? " $label1" : '')),
- $block->final1,
- array("======="),
- $block->final2,
- array(">>>>>>>" . ($label2 ? " $label2" : '')));
+ array("<<<<<<<" . ($label1 ? " $label1" : '')),
+ $block->final1,
+ array("======="),
+ $block->final2,
+ array(">>>>>>>" . ($label2 ? " $label2" : '')));
$this->ConflictingBlocks++;
- }
- else {
+ } else {
$lines = array_merge($lines, $block->merged());
}
}