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.

MergeResult.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.Iterator;
  45. import java.util.List;
  46. import org.eclipse.jgit.diff.Sequence;
  47. import org.eclipse.jgit.merge.MergeChunk.ConflictState;
  48. import org.eclipse.jgit.util.IntList;
  49. /**
  50. * The result of merging a number of {@link org.eclipse.jgit.diff.Sequence}
  51. * objects. These sequences have one common predecessor sequence. The result of
  52. * a merge is a list of MergeChunks. Each MergeChunk contains either a range (a
  53. * subsequence) from one of the merged sequences, a range from the common
  54. * predecessor or a conflicting range from one of the merged sequences. A
  55. * conflict will be reported as multiple chunks, one for each conflicting range.
  56. * The first chunk for a conflict is marked specially to distinguish the border
  57. * between two consecutive conflicts.
  58. * <p>
  59. * This class does not know anything about how to present the merge result to
  60. * the end-user. MergeFormatters have to be used to construct something human
  61. * readable.
  62. *
  63. * @param <S>
  64. * type of sequence.
  65. */
  66. public class MergeResult<S extends Sequence> implements Iterable<MergeChunk> {
  67. private final List<S> sequences;
  68. final IntList chunks = new IntList();
  69. private boolean containsConflicts = false;
  70. /**
  71. * Creates a new empty MergeResult
  72. *
  73. * @param sequences
  74. * contains the common predecessor sequence at position 0
  75. * followed by the merged sequences. This list should not be
  76. * modified anymore during the lifetime of this
  77. * {@link org.eclipse.jgit.merge.MergeResult}.
  78. */
  79. public MergeResult(List<S> sequences) {
  80. this.sequences = sequences;
  81. }
  82. /**
  83. * Adds a new range from one of the merged sequences or from the common
  84. * predecessor. This method can add conflicting and non-conflicting ranges
  85. * controlled by the conflictState parameter
  86. *
  87. * @param srcIdx
  88. * determines from which sequence this range comes. An index of
  89. * x specifies the x+1 element in the list of sequences
  90. * specified to the constructor
  91. * @param begin
  92. * the first element from the specified sequence which should be
  93. * included in the merge result. Indexes start with 0.
  94. * @param end
  95. * specifies the end of the range to be added. The element this
  96. * index points to is the first element which not added to the
  97. * merge result. All elements between begin (including begin) and
  98. * this element are added.
  99. * @param conflictState
  100. * when set to NO_CONLICT a non-conflicting range is added.
  101. * This will end implicitly all open conflicts added before.
  102. */
  103. public void add(int srcIdx, int begin, int end, ConflictState conflictState) {
  104. chunks.add(conflictState.ordinal());
  105. chunks.add(srcIdx);
  106. chunks.add(begin);
  107. chunks.add(end);
  108. if (conflictState != ConflictState.NO_CONFLICT)
  109. containsConflicts = true;
  110. }
  111. /**
  112. * Returns the common predecessor sequence and the merged sequence in one
  113. * list. The common predecessor is the first element in the list
  114. *
  115. * @return the common predecessor at position 0 followed by the merged
  116. * sequences.
  117. */
  118. public List<S> getSequences() {
  119. return sequences;
  120. }
  121. static final ConflictState[] states = ConflictState.values();
  122. /** {@inheritDoc} */
  123. @Override
  124. public Iterator<MergeChunk> iterator() {
  125. return new Iterator<MergeChunk>() {
  126. int idx;
  127. @Override
  128. public boolean hasNext() {
  129. return (idx < chunks.size());
  130. }
  131. @Override
  132. public MergeChunk next() {
  133. ConflictState state = states[chunks.get(idx++)];
  134. int srcIdx = chunks.get(idx++);
  135. int begin = chunks.get(idx++);
  136. int end = chunks.get(idx++);
  137. return new MergeChunk(srcIdx, begin, end, state);
  138. }
  139. @Override
  140. public void remove() {
  141. throw new UnsupportedOperationException();
  142. }
  143. };
  144. }
  145. /**
  146. * Whether this merge result contains conflicts
  147. *
  148. * @return true if this merge result contains conflicts
  149. */
  150. public boolean containsConflicts() {
  151. return containsConflicts;
  152. }
  153. /**
  154. * Sets explicitly whether this merge should be seen as containing a
  155. * conflict or not. Needed because during RecursiveMerger we want to do
  156. * content-merges and take the resulting content (even with conflict
  157. * markers!) as new conflict-free content
  158. *
  159. * @param containsConflicts
  160. * whether this merge should be seen as containing a conflict or
  161. * not.
  162. * @since 3.5
  163. */
  164. protected void setContainsConflicts(boolean containsConflicts) {
  165. this.containsConflicts = containsConflicts;
  166. }
  167. }