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 11KB

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