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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (ours.size() == 0) {
  99. if (theirs.size() != 0) {
  100. EditList theirsEdits = diffAlg.diff(cmp, base, theirs);
  101. if (!theirsEdits.isEmpty()) {
  102. // we deleted, they modified -> Let their complete content
  103. // conflict with empty text
  104. result.add(1, 0, 0, ConflictState.FIRST_CONFLICTING_RANGE);
  105. result.add(2, 0, theirs.size(),
  106. ConflictState.NEXT_CONFLICTING_RANGE);
  107. } else
  108. // we deleted, they didn't modify -> Let our deletion win
  109. result.add(1, 0, 0, ConflictState.NO_CONFLICT);
  110. } else
  111. // we and they deleted -> return a single chunk of nothing
  112. result.add(1, 0, 0, ConflictState.NO_CONFLICT);
  113. return result;
  114. } else if (theirs.size() == 0) {
  115. EditList oursEdits = diffAlg.diff(cmp, base, ours);
  116. if (!oursEdits.isEmpty()) {
  117. // we modified, they deleted -> Let our complete content
  118. // conflict with empty text
  119. result.add(1, 0, ours.size(),
  120. ConflictState.FIRST_CONFLICTING_RANGE);
  121. result.add(2, 0, 0, ConflictState.NEXT_CONFLICTING_RANGE);
  122. } else
  123. // they deleted, we didn't modify -> Let their deletion win
  124. result.add(2, 0, 0, ConflictState.NO_CONFLICT);
  125. return result;
  126. }
  127. EditList oursEdits = diffAlg.diff(cmp, base, ours);
  128. Iterator<Edit> baseToOurs = oursEdits.iterator();
  129. EditList theirsEdits = diffAlg.diff(cmp, base, theirs);
  130. Iterator<Edit> baseToTheirs = theirsEdits.iterator();
  131. int current = 0; // points to the next line (first line is 0) of base
  132. // which was not handled yet
  133. Edit oursEdit = nextEdit(baseToOurs);
  134. Edit theirsEdit = nextEdit(baseToTheirs);
  135. // iterate over all edits from base to ours and from base to theirs
  136. // leave the loop when there are no edits more for ours or for theirs
  137. // (or both)
  138. while (theirsEdit != END_EDIT || oursEdit != END_EDIT) {
  139. if (oursEdit.getEndA() < theirsEdit.getBeginA()) {
  140. // something was changed in ours not overlapping with any change
  141. // from theirs. First add the common part in front of the edit
  142. // then the edit.
  143. if (current != oursEdit.getBeginA()) {
  144. result.add(0, current, oursEdit.getBeginA(),
  145. ConflictState.NO_CONFLICT);
  146. }
  147. result.add(1, oursEdit.getBeginB(), oursEdit.getEndB(),
  148. ConflictState.NO_CONFLICT);
  149. current = oursEdit.getEndA();
  150. oursEdit = nextEdit(baseToOurs);
  151. } else if (theirsEdit.getEndA() < oursEdit.getBeginA()) {
  152. // something was changed in theirs not overlapping with any
  153. // from ours. First add the common part in front of the edit
  154. // then the edit.
  155. if (current != theirsEdit.getBeginA()) {
  156. result.add(0, current, theirsEdit.getBeginA(),
  157. ConflictState.NO_CONFLICT);
  158. }
  159. result.add(2, theirsEdit.getBeginB(), theirsEdit.getEndB(),
  160. ConflictState.NO_CONFLICT);
  161. current = theirsEdit.getEndA();
  162. theirsEdit = nextEdit(baseToTheirs);
  163. } else {
  164. // here we found a real overlapping modification
  165. // if there is a common part in front of the conflict add it
  166. if (oursEdit.getBeginA() != current
  167. && theirsEdit.getBeginA() != current) {
  168. result.add(0, current, Math.min(oursEdit.getBeginA(),
  169. theirsEdit.getBeginA()), ConflictState.NO_CONFLICT);
  170. }
  171. // set some initial values for the ranges in A and B which we
  172. // want to handle
  173. int oursBeginB = oursEdit.getBeginB();
  174. int theirsBeginB = theirsEdit.getBeginB();
  175. // harmonize the start of the ranges in A and B
  176. if (oursEdit.getBeginA() < theirsEdit.getBeginA()) {
  177. theirsBeginB -= theirsEdit.getBeginA()
  178. - oursEdit.getBeginA();
  179. } else {
  180. oursBeginB -= oursEdit.getBeginA() - theirsEdit.getBeginA();
  181. }
  182. // combine edits:
  183. // Maybe an Edit on one side corresponds to multiple Edits on
  184. // the other side. Then we have to combine the Edits of the
  185. // other side - so in the end we can merge together two single
  186. // edits.
  187. //
  188. // It is important to notice that this combining will extend the
  189. // ranges of our conflict always downwards (towards the end of
  190. // the content). The starts of the conflicting ranges in ours
  191. // and theirs are not touched here.
  192. //
  193. // This combining is an iterative process: after we have
  194. // combined some edits we have to do the check again. The
  195. // combined edits could now correspond to multiple edits on the
  196. // other side.
  197. //
  198. // Example: when this combining algorithm works on the following
  199. // edits
  200. // oursEdits=((0-5,0-5),(6-8,6-8),(10-11,10-11)) and
  201. // theirsEdits=((0-1,0-1),(2-3,2-3),(5-7,5-7))
  202. // it will merge them into
  203. // oursEdits=((0-8,0-8),(10-11,10-11)) and
  204. // theirsEdits=((0-7,0-7))
  205. //
  206. // Since the only interesting thing to us is how in ours and
  207. // theirs the end of the conflicting range is changing we let
  208. // oursEdit and theirsEdit point to the last conflicting edit
  209. Edit nextOursEdit = nextEdit(baseToOurs);
  210. Edit nextTheirsEdit = nextEdit(baseToTheirs);
  211. for (;;) {
  212. if (oursEdit.getEndA() >= nextTheirsEdit.getBeginA()) {
  213. theirsEdit = nextTheirsEdit;
  214. nextTheirsEdit = nextEdit(baseToTheirs);
  215. } else if (theirsEdit.getEndA() >= nextOursEdit.getBeginA()) {
  216. oursEdit = nextOursEdit;
  217. nextOursEdit = nextEdit(baseToOurs);
  218. } else {
  219. break;
  220. }
  221. }
  222. // harmonize the end of the ranges in A and B
  223. int oursEndB = oursEdit.getEndB();
  224. int theirsEndB = theirsEdit.getEndB();
  225. if (oursEdit.getEndA() < theirsEdit.getEndA()) {
  226. oursEndB += theirsEdit.getEndA() - oursEdit.getEndA();
  227. } else {
  228. theirsEndB += oursEdit.getEndA() - theirsEdit.getEndA();
  229. }
  230. // A conflicting region is found. Strip off common lines in
  231. // in the beginning and the end of the conflicting region
  232. // Determine the minimum length of the conflicting areas in OURS
  233. // and THEIRS. Also determine how much bigger the conflicting
  234. // area in THEIRS is compared to OURS. All that is needed to
  235. // limit the search for common areas at the beginning or end
  236. // (the common areas cannot be bigger then the smaller
  237. // conflicting area. The delta is needed to know whether the
  238. // complete conflicting area is common in OURS and THEIRS.
  239. int minBSize = oursEndB - oursBeginB;
  240. int BSizeDelta = minBSize - (theirsEndB - theirsBeginB);
  241. if (BSizeDelta > 0)
  242. minBSize -= BSizeDelta;
  243. int commonPrefix = 0;
  244. while (commonPrefix < minBSize
  245. && cmp.equals(ours, oursBeginB + commonPrefix, theirs,
  246. theirsBeginB + commonPrefix))
  247. commonPrefix++;
  248. minBSize -= commonPrefix;
  249. int commonSuffix = 0;
  250. while (commonSuffix < minBSize
  251. && cmp.equals(ours, oursEndB - commonSuffix - 1, theirs,
  252. theirsEndB - commonSuffix - 1))
  253. commonSuffix++;
  254. minBSize -= commonSuffix;
  255. // Add the common lines at start of conflict
  256. if (commonPrefix > 0)
  257. result.add(1, oursBeginB, oursBeginB + commonPrefix,
  258. ConflictState.NO_CONFLICT);
  259. // Add the conflict (Only if there is a conflict left to report)
  260. if (minBSize > 0 || BSizeDelta != 0) {
  261. result.add(1, oursBeginB + commonPrefix, oursEndB
  262. - commonSuffix,
  263. ConflictState.FIRST_CONFLICTING_RANGE);
  264. result.add(2, theirsBeginB + commonPrefix, theirsEndB
  265. - commonSuffix,
  266. ConflictState.NEXT_CONFLICTING_RANGE);
  267. }
  268. // Add the common lines at end of conflict
  269. if (commonSuffix > 0)
  270. result.add(1, oursEndB - commonSuffix, oursEndB,
  271. ConflictState.NO_CONFLICT);
  272. current = Math.max(oursEdit.getEndA(), theirsEdit.getEndA());
  273. oursEdit = nextOursEdit;
  274. theirsEdit = nextTheirsEdit;
  275. }
  276. }
  277. // maybe we have a common part behind the last edit: copy it to the
  278. // result
  279. if (current < base.size()) {
  280. result.add(0, current, base.size(), ConflictState.NO_CONFLICT);
  281. }
  282. return result;
  283. }
  284. /**
  285. * Helper method which returns the next Edit for an Iterator over Edits.
  286. * When there are no more edits left this method will return the constant
  287. * END_EDIT.
  288. *
  289. * @param it
  290. * the iterator for which the next edit should be returned
  291. * @return the next edit from the iterator or END_EDIT if there no more
  292. * edits
  293. */
  294. private static Edit nextEdit(Iterator<Edit> it) {
  295. return (it.hasNext() ? it.next() : END_EDIT);
  296. }
  297. }