You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MergeAlgorithm.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import java.util.ArrayList;
  45. import java.util.Iterator;
  46. import java.util.List;
  47. import org.eclipse.jgit.diff.Edit;
  48. import org.eclipse.jgit.diff.EditList;
  49. import org.eclipse.jgit.diff.MyersDiff;
  50. import org.eclipse.jgit.diff.Sequence;
  51. import org.eclipse.jgit.merge.MergeChunk.ConflictState;
  52. /**
  53. * Provides the merge algorithm which does a three-way merge on content provided
  54. * as RawText. Makes use of {@link MyersDiff} to compute the diffs.
  55. */
  56. public final class MergeAlgorithm {
  57. /**
  58. * Since this class provides only static methods I add a private default
  59. * constructor to prevent instantiation.
  60. */
  61. private MergeAlgorithm() {
  62. }
  63. // An special edit which acts as a sentinel value by marking the end the
  64. // list of edits
  65. private final static Edit END_EDIT = new Edit(Integer.MAX_VALUE,
  66. Integer.MAX_VALUE);
  67. /**
  68. * Does the three way merge between a common base and two sequences.
  69. *
  70. * @param base the common base sequence
  71. * @param ours the first sequence to be merged
  72. * @param theirs the second sequence to be merged
  73. * @return the resulting content
  74. */
  75. public static MergeResult merge(Sequence base, Sequence ours,
  76. Sequence theirs) {
  77. List<Sequence> sequences = new ArrayList<Sequence>(3);
  78. sequences.add(base);
  79. sequences.add(ours);
  80. sequences.add(theirs);
  81. MergeResult result = new MergeResult(sequences);
  82. EditList oursEdits = new MyersDiff(base, ours).getEdits();
  83. Iterator<Edit> baseToOurs = oursEdits.iterator();
  84. EditList theirsEdits = new MyersDiff(base, theirs).getEdits();
  85. Iterator<Edit> baseToTheirs = theirsEdits.iterator();
  86. int current = 0; // points to the next line (first line is 0) of base
  87. // which was not handled yet
  88. Edit oursEdit = nextEdit(baseToOurs);
  89. Edit theirsEdit = nextEdit(baseToTheirs);
  90. // iterate over all edits from base to ours and from base to theirs
  91. // leave the loop when there are no edits more for ours or for theirs
  92. // (or both)
  93. while (theirsEdit != END_EDIT || oursEdit != END_EDIT) {
  94. if (oursEdit.getEndA() <= theirsEdit.getBeginA()) {
  95. // something was changed in ours not overlapping with any change
  96. // from theirs. First add the common part in front of the edit
  97. // then the edit.
  98. if (current != oursEdit.getBeginA()) {
  99. result.add(0, current, oursEdit.getBeginA(),
  100. ConflictState.NO_CONFLICT);
  101. }
  102. result.add(1, oursEdit.getBeginB(), oursEdit.getEndB(),
  103. ConflictState.NO_CONFLICT);
  104. current = oursEdit.getEndA();
  105. oursEdit = nextEdit(baseToOurs);
  106. } else if (theirsEdit.getEndA() <= oursEdit.getBeginA()) {
  107. // something was changed in theirs not overlapping with any
  108. // from ours. First add the common part in front of the edit
  109. // then the edit.
  110. if (current != theirsEdit.getBeginA()) {
  111. result.add(0, current, theirsEdit.getBeginA(),
  112. ConflictState.NO_CONFLICT);
  113. }
  114. result.add(2, theirsEdit.getBeginB(), theirsEdit.getEndB(),
  115. ConflictState.NO_CONFLICT);
  116. current = theirsEdit.getEndA();
  117. theirsEdit = nextEdit(baseToTheirs);
  118. } else {
  119. // here we found a real overlapping modification
  120. // if there is a common part in front of the conflict add it
  121. if (oursEdit.getBeginA() != current
  122. && theirsEdit.getBeginA() != current) {
  123. result.add(0, current, Math.min(oursEdit.getBeginA(),
  124. theirsEdit.getBeginA()), ConflictState.NO_CONFLICT);
  125. }
  126. // set some initial values for the ranges in A and B which we
  127. // want to handle
  128. int oursBeginB = oursEdit.getBeginB();
  129. int theirsBeginB = theirsEdit.getBeginB();
  130. // harmonize the start of the ranges in A and B
  131. if (oursEdit.getBeginA() < theirsEdit.getBeginA()) {
  132. theirsBeginB -= theirsEdit.getBeginA()
  133. - oursEdit.getBeginA();
  134. } else {
  135. oursBeginB -= oursEdit.getBeginA() - theirsEdit.getBeginA();
  136. }
  137. // combine edits:
  138. // Maybe an Edit on one side corresponds to multiple Edits on
  139. // the other side. Then we have to combine the Edits of the
  140. // other side - so in the end we can merge together two single
  141. // edits.
  142. //
  143. // It is important to notice that this combining will extend the
  144. // ranges of our conflict always downwards (towards the end of
  145. // the content). The starts of the conflicting ranges in ours
  146. // and theirs are not touched here.
  147. //
  148. // This combining is an iterative process: after we have
  149. // combined some edits we have to do the check again. The
  150. // combined edits could now correspond to multiple edits on the
  151. // other side.
  152. //
  153. // Example: when this combining algorithm works on the following
  154. // edits
  155. // oursEdits=((0-5,0-5),(6-8,6-8),(10-11,10-11)) and
  156. // theirsEdits=((0-1,0-1),(2-3,2-3),(5-7,5-7))
  157. // it will merge them into
  158. // oursEdits=((0-8,0-8),(10-11,10-11)) and
  159. // theirsEdits=((0-7,0-7))
  160. //
  161. // Since the only interesting thing to us is how in ours and
  162. // theirs the end of the conflicting range is changing we let
  163. // oursEdit and theirsEdit point to the last conflicting edit
  164. Edit nextOursEdit = nextEdit(baseToOurs);
  165. Edit nextTheirsEdit = nextEdit(baseToTheirs);
  166. for (;;) {
  167. if (oursEdit.getEndA() > nextTheirsEdit.getBeginA()) {
  168. theirsEdit = nextTheirsEdit;
  169. nextTheirsEdit = nextEdit(baseToTheirs);
  170. } else if (theirsEdit.getEndA() > nextOursEdit.getBeginA()) {
  171. oursEdit = nextOursEdit;
  172. nextOursEdit = nextEdit(baseToOurs);
  173. } else {
  174. break;
  175. }
  176. }
  177. // harmonize the end of the ranges in A and B
  178. int oursEndB = oursEdit.getEndB();
  179. int theirsEndB = theirsEdit.getEndB();
  180. if (oursEdit.getEndA() < theirsEdit.getEndA()) {
  181. oursEndB += theirsEdit.getEndA() - oursEdit.getEndA();
  182. } else {
  183. theirsEndB += oursEdit.getEndA() - theirsEdit.getEndA();
  184. }
  185. // Add the conflict
  186. result.add(1, oursBeginB, oursEndB,
  187. ConflictState.FIRST_CONFLICTING_RANGE);
  188. result.add(2, theirsBeginB, theirsEndB,
  189. ConflictState.NEXT_CONFLICTING_RANGE);
  190. current = Math.max(oursEdit.getEndA(), theirsEdit.getEndA());
  191. oursEdit = nextOursEdit;
  192. theirsEdit = nextTheirsEdit;
  193. }
  194. }
  195. // maybe we have a common part behind the last edit: copy it to the
  196. // result
  197. if (current < base.size()) {
  198. result.add(0, current, base.size(), ConflictState.NO_CONFLICT);
  199. }
  200. return result;
  201. }
  202. /**
  203. * Helper method which returns the next Edit for an Iterator over Edits.
  204. * When there are no more edits left this method will return the constant
  205. * END_EDIT.
  206. *
  207. * @param it
  208. * the iterator for which the next edit should be returned
  209. * @return the next edit from the iterator or END_EDIT if there no more
  210. * edits
  211. */
  212. private static Edit nextEdit(Iterator<Edit> it) {
  213. return (it.hasNext() ? it.next() : END_EDIT);
  214. }
  215. }