ThreeWay.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * A class for computing three way merges.
  4. *
  5. * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you did
  8. * not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @package Text_Diff
  11. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  12. */
  13. class Horde_Text_Diff_ThreeWay
  14. {
  15. /**
  16. * Array of changes.
  17. *
  18. * @var array
  19. */
  20. protected $_edits;
  21. /**
  22. * Conflict counter.
  23. *
  24. * @var integer
  25. */
  26. protected $_conflictingBlocks = 0;
  27. /**
  28. * Computes diff between 3 sequences of strings.
  29. *
  30. * @param array $orig The original lines to use.
  31. * @param array $final1 The first version to compare to.
  32. * @param array $final2 The second version to compare to.
  33. */
  34. public function __construct($orig, $final1, $final2)
  35. {
  36. if (extension_loaded('xdiff')) {
  37. $engine = new Horde_Text_Diff_Engine_Xdiff();
  38. } else {
  39. $engine = new Horde_Text_Diff_Engine_Native();
  40. }
  41. $this->_edits = $this->_diff3($engine->diff($orig, $final1),
  42. $engine->diff($orig, $final2));
  43. }
  44. /**
  45. */
  46. public function mergedOutput($label1 = false, $label2 = false)
  47. {
  48. $lines = [];
  49. foreach ($this->_edits as $edit) {
  50. if ($edit->isConflict()) {
  51. /* FIXME: this should probably be moved somewhere else. */
  52. $lines = array_merge($lines,
  53. ['<<<<<<<' . ($label1 ? ' ' . $label1 : '')],
  54. $edit->final1,
  55. ["======="],
  56. $edit->final2,
  57. ['>>>>>>>' . ($label2 ? ' ' . $label2 : '')]);
  58. $this->_conflictingBlocks++;
  59. } else {
  60. $lines = array_merge($lines, $edit->merged());
  61. }
  62. }
  63. return $lines;
  64. }
  65. /**
  66. */
  67. protected function _diff3($edits1, $edits2)
  68. {
  69. $edits = [];
  70. $bb = new Horde_Text_Diff_ThreeWay_BlockBuilder();
  71. $e1 = current($edits1);
  72. $e2 = current($edits2);
  73. while ($e1 || $e2) {
  74. if ($e1 && $e2 &&
  75. $e1 instanceof Horde_Text_Diff_Op_Copy &&
  76. $e2 instanceof Horde_Text_Diff_Op_Copy) {
  77. /* We have copy blocks from both diffs. This is the (only)
  78. * time we want to emit a diff3 copy block. Flush current
  79. * diff3 diff block, if any. */
  80. if ($edit = $bb->finish()) {
  81. $edits[] = $edit;
  82. }
  83. $ncopy = min($e1->norig(), $e2->norig());
  84. assert($ncopy > 0);
  85. $edits[] = new Horde_Text_Diff_ThreeWay_Op_Copy(array_slice($e1->orig, 0, $ncopy));
  86. if ($e1->norig() > $ncopy) {
  87. array_splice($e1->orig, 0, $ncopy);
  88. array_splice($e1->final, 0, $ncopy);
  89. } else {
  90. $e1 = next($edits1);
  91. }
  92. if ($e2->norig() > $ncopy) {
  93. array_splice($e2->orig, 0, $ncopy);
  94. array_splice($e2->final, 0, $ncopy);
  95. } else {
  96. $e2 = next($edits2);
  97. }
  98. } else {
  99. if ($e1 && $e2) {
  100. if ($e1->orig && $e2->orig) {
  101. $norig = min($e1->norig(), $e2->norig());
  102. $orig = array_splice($e1->orig, 0, $norig);
  103. array_splice($e2->orig, 0, $norig);
  104. $bb->input($orig);
  105. }
  106. if ($e1 instanceof Horde_Text_Diff_Op_Copy) {
  107. $bb->out1(array_splice($e1->final, 0, $norig));
  108. }
  109. if ($e2 instanceof Horde_Text_Diff_Op_Copy) {
  110. $bb->out2(array_splice($e2->final, 0, $norig));
  111. }
  112. }
  113. if ($e1 && ! $e1->orig) {
  114. $bb->out1($e1->final);
  115. $e1 = next($edits1);
  116. }
  117. if ($e2 && ! $e2->orig) {
  118. $bb->out2($e2->final);
  119. $e2 = next($edits2);
  120. }
  121. }
  122. }
  123. if ($edit = $bb->finish()) {
  124. $edits[] = $edit;
  125. }
  126. return $edits;
  127. }
  128. }