Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MergeAlgorithm.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.diff.SequenceComparator;
  52. import org.eclipse.jgit.merge.MergeChunk.ConflictState;
  53. /**
  54. * Provides the merge algorithm which does a three-way merge on content provided
  55. * as RawText. Makes use of {@link MyersDiff} to compute the diffs.
  56. */
  57. public final class MergeAlgorithm {
  58. /**
  59. * Since this class provides only static methods I add a private default
  60. * constructor to prevent instantiation.
  61. */
  62. private MergeAlgorithm() {
  63. }
  64. // An special edit which acts as a sentinel value by marking the end the
  65. // list of edits
  66. private final static Edit END_EDIT = new Edit(Integer.MAX_VALUE,
  67. Integer.MAX_VALUE);
  68. /**
  69. * Does the three way merge between a common base and two sequences.
  70. *
  71. * @param <S>
  72. * type of sequence.
  73. * @param cmp comparison method for this execution.
  74. * @param base the common base sequence
  75. * @param ours the first sequence to be merged
  76. * @param theirs the second sequence to be merged
  77. * @return the resulting content
  78. */
  79. public static <S extends Sequence> MergeResult<S> merge(
  80. SequenceComparator<S> cmp, S base, S ours, S theirs) {
  81. List<S> sequences = new ArrayList<S>(3);
  82. sequences.add(base);
  83. sequences.add(ours);
  84. sequences.add(theirs);
  85. MergeResult result = new MergeResult<S>(sequences);
  86. EditList oursEdits = MyersDiff.INSTANCE.diff(cmp, base, ours);
  87. Iterator<Edit> baseToOurs = oursEdits.iterator();
  88. EditList theirsEdits = MyersDiff.INSTANCE.diff(cmp, base, theirs);
  89. Iterator<Edit> baseToTheirs = theirsEdits.iterator();
  90. int current = 0; // points to the next line (first line is 0) of base
  91. // which was not handled yet
  92. Edit oursEdit = nextEdit(baseToOurs);
  93. Edit theirsEdit = nextEdit(baseToTheirs);
  94. // iterate over all edits from base to ours and from base to theirs
  95. // leave the loop when there are no edits more for ours or for theirs
  96. // (or both)
  97. while (theirsEdit != END_EDIT || oursEdit != END_EDIT) {
  98. if (oursEdit.getEndA() <= theirsEdit.getBeginA()) {
  99. // something was changed in ours not overlapping with any change
  100. // from theirs. First add the common part in front of the edit
  101. // then the edit.
  102. if (current != oursEdit.getBeginA()) {
  103. result.add(0, current, oursEdit.getBeginA(),
  104. ConflictState.NO_CONFLICT);
  105. }
  106. result.add(1, oursEdit.getBeginB(), oursEdit.getEndB(),
  107. ConflictState.NO_CONFLICT);
  108. current = oursEdit.getEndA();
  109. oursEdit = nextEdit(baseToOurs);
  110. } else if (theirsEdit.getEndA() <= oursEdit.getBeginA()) {
  111. // something was changed in theirs not overlapping with any
  112. // from ours. First add the common part in front of the edit
  113. // then the edit.
  114. if (current != theirsEdit.getBeginA()) {
  115. result.add(0, current, theirsEdit.getBeginA(),
  116. ConflictState.NO_CONFLICT);
  117. }
  118. result.add(2, theirsEdit.getBeginB(), theirsEdit.getEndB(),
  119. ConflictState.NO_CONFLICT);
  120. current = theirsEdit.getEndA();
  121. theirsEdit = nextEdit(baseToTheirs);
  122. } else {
  123. // here we found a real overlapping modification
  124. // if there is a common part in front of the conflict add it
  125. if (oursEdit.getBeginA() != current
  126. && theirsEdit.getBeginA() != current) {
  127. result.add(0, current, Math.min(oursEdit.getBeginA(),
  128. theirsEdit.getBeginA()), ConflictState.NO_CONFLICT);
  129. }
  130. // set some initial values for the ranges in A and B which we
  131. // want to handle
  132. int oursBeginB = oursEdit.getBeginB();
  133. int theirsBeginB = theirsEdit.getBeginB();
  134. // harmonize the start of the ranges in A and B
  135. if (oursEdit.getBeginA() < theirsEdit.getBeginA()) {
  136. theirsBeginB -= theirsEdit.getBeginA()
  137. - oursEdit.getBeginA();
  138. } else {
  139. oursBeginB -= oursEdit.getBeginA() - theirsEdit.getBeginA();
  140. }
  141. // combine edits:
  142. // Maybe an Edit on one side corresponds to multiple Edits on
  143. // the other side. Then we have to combine the Edits of the
  144. // other side - so in the end we can merge together two single
  145. // edits.
  146. //
  147. // It is important to notice that this combining will extend the
  148. // ranges of our conflict always downwards (towards the end of
  149. // the content). The starts of the conflicting ranges in ours
  150. // and theirs are not touched here.
  151. //
  152. // This combining is an iterative process: after we have
  153. // combined some edits we have to do the check again. The
  154. // combined edits could now correspond to multiple edits on the
  155. // other side.
  156. //
  157. // Example: when this combining algorithm works on the following
  158. // edits
  159. // oursEdits=((0-5,0-5),(6-8,6-8),(10-11,10-11)) and
  160. // theirsEdits=((0-1,0-1),(2-3,2-3),(5-7,5-7))
  161. // it will merge them into
  162. // oursEdits=((0-8,0-8),(10-11,10-11)) and
  163. // theirsEdits=((0-7,0-7))
  164. //
  165. // Since the only interesting thing to us is how in ours and
  166. // theirs the end of the conflicting range is changing we let
  167. // oursEdit and theirsEdit point to the last conflicting edit
  168. Edit nextOursEdit = nextEdit(baseToOurs);
  169. Edit nextTheirsEdit = nextEdit(baseToTheirs);
  170. for (;;) {
  171. if (oursEdit.getEndA() > nextTheirsEdit.getBeginA()) {
  172. theirsEdit = nextTheirsEdit;
  173. nextTheirsEdit = nextEdit(baseToTheirs);
  174. } else if (theirsEdit.getEndA() > nextOursEdit.getBeginA()) {
  175. oursEdit = nextOursEdit;
  176. nextOursEdit = nextEdit(baseToOurs);
  177. } else {
  178. break;
  179. }
  180. }
  181. // harmonize the end of the ranges in A and B
  182. int oursEndB = oursEdit.getEndB();
  183. int theirsEndB = theirsEdit.getEndB();
  184. if (oursEdit.getEndA() < theirsEdit.getEndA()) {
  185. oursEndB += theirsEdit.getEndA() - oursEdit.getEndA();
  186. } else {
  187. theirsEndB += oursEdit.getEndA() - theirsEdit.getEndA();
  188. }
  189. // A conflicting region is found. Strip off common lines in
  190. // in the beginning and the end of the conflicting region
  191. int conflictLen = Math.min(oursEndB - oursBeginB, theirsEndB
  192. - theirsBeginB);
  193. int commonPrefix = 0;
  194. while (commonPrefix < conflictLen
  195. && cmp.equals(ours, oursBeginB + commonPrefix, theirs,
  196. theirsBeginB + commonPrefix))
  197. commonPrefix++;
  198. conflictLen -= commonPrefix;
  199. int commonSuffix = 0;
  200. while (commonSuffix < conflictLen
  201. && cmp.equals(ours, oursEndB - commonSuffix - 1, theirs,
  202. theirsEndB - commonSuffix - 1))
  203. commonSuffix++;
  204. conflictLen -= commonSuffix;
  205. // Add the common lines at start of conflict
  206. if (commonPrefix > 0)
  207. result.add(1, oursBeginB, oursBeginB + commonPrefix,
  208. ConflictState.NO_CONFLICT);
  209. // Add the conflict
  210. if (conflictLen > 0) {
  211. result.add(1, oursBeginB + commonPrefix, oursEndB
  212. - commonSuffix,
  213. ConflictState.FIRST_CONFLICTING_RANGE);
  214. result.add(2, theirsBeginB + commonPrefix, theirsEndB
  215. - commonSuffix,
  216. ConflictState.NEXT_CONFLICTING_RANGE);
  217. }
  218. // Add the common lines at end of conflict
  219. if (commonSuffix > 0)
  220. result.add(1, oursEndB - commonSuffix, oursEndB,
  221. ConflictState.NO_CONFLICT);
  222. current = Math.max(oursEdit.getEndA(), theirsEdit.getEndA());
  223. oursEdit = nextOursEdit;
  224. theirsEdit = nextTheirsEdit;
  225. }
  226. }
  227. // maybe we have a common part behind the last edit: copy it to the
  228. // result
  229. if (current < base.size()) {
  230. result.add(0, current, base.size(), ConflictState.NO_CONFLICT);
  231. }
  232. return result;
  233. }
  234. /**
  235. * Helper method which returns the next Edit for an Iterator over Edits.
  236. * When there are no more edits left this method will return the constant
  237. * END_EDIT.
  238. *
  239. * @param it
  240. * the iterator for which the next edit should be returned
  241. * @return the next edit from the iterator or END_EDIT if there no more
  242. * edits
  243. */
  244. private static Edit nextEdit(Iterator<Edit> it) {
  245. return (it.hasNext() ? it.next() : END_EDIT);
  246. }
  247. }