Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MergeResult.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.merge;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import org.eclipse.jgit.diff.Sequence;
  14. import org.eclipse.jgit.merge.MergeChunk.ConflictState;
  15. import org.eclipse.jgit.util.IntList;
  16. /**
  17. * The result of merging a number of {@link org.eclipse.jgit.diff.Sequence}
  18. * objects. These sequences have one common predecessor sequence. The result of
  19. * a merge is a list of MergeChunks. Each MergeChunk contains either a range (a
  20. * subsequence) from one of the merged sequences, a range from the common
  21. * predecessor or a conflicting range from one of the merged sequences. A
  22. * conflict will be reported as multiple chunks, one for each conflicting range.
  23. * The first chunk for a conflict is marked specially to distinguish the border
  24. * between two consecutive conflicts.
  25. * <p>
  26. * This class does not know anything about how to present the merge result to
  27. * the end-user. MergeFormatters have to be used to construct something human
  28. * readable.
  29. *
  30. * @param <S>
  31. * type of sequence.
  32. */
  33. public class MergeResult<S extends Sequence> implements Iterable<MergeChunk> {
  34. private final List<S> sequences;
  35. final IntList chunks = new IntList();
  36. private boolean containsConflicts = false;
  37. /**
  38. * Creates a new empty MergeResult
  39. *
  40. * @param sequences
  41. * contains the common predecessor sequence at position 0
  42. * followed by the merged sequences. This list should not be
  43. * modified anymore during the lifetime of this
  44. * {@link org.eclipse.jgit.merge.MergeResult}.
  45. */
  46. public MergeResult(List<S> sequences) {
  47. this.sequences = sequences;
  48. }
  49. /**
  50. * Adds a new range from one of the merged sequences or from the common
  51. * predecessor. This method can add conflicting and non-conflicting ranges
  52. * controlled by the conflictState parameter
  53. *
  54. * @param srcIdx
  55. * determines from which sequence this range comes. An index of
  56. * x specifies the x+1 element in the list of sequences
  57. * specified to the constructor
  58. * @param begin
  59. * the first element from the specified sequence which should be
  60. * included in the merge result. Indexes start with 0.
  61. * @param end
  62. * specifies the end of the range to be added. The element this
  63. * index points to is the first element which not added to the
  64. * merge result. All elements between begin (including begin) and
  65. * this element are added.
  66. * @param conflictState
  67. * when set to NO_CONLICT a non-conflicting range is added.
  68. * This will end implicitly all open conflicts added before.
  69. */
  70. public void add(int srcIdx, int begin, int end, ConflictState conflictState) {
  71. chunks.add(conflictState.ordinal());
  72. chunks.add(srcIdx);
  73. chunks.add(begin);
  74. chunks.add(end);
  75. if (conflictState != ConflictState.NO_CONFLICT)
  76. containsConflicts = true;
  77. }
  78. /**
  79. * Returns the common predecessor sequence and the merged sequence in one
  80. * list. The common predecessor is the first element in the list
  81. *
  82. * @return the common predecessor at position 0 followed by the merged
  83. * sequences.
  84. */
  85. public List<S> getSequences() {
  86. return sequences;
  87. }
  88. static final ConflictState[] states = ConflictState.values();
  89. /** {@inheritDoc} */
  90. @Override
  91. public Iterator<MergeChunk> iterator() {
  92. return new Iterator<MergeChunk>() {
  93. int idx;
  94. @Override
  95. public boolean hasNext() {
  96. return (idx < chunks.size());
  97. }
  98. @Override
  99. public MergeChunk next() {
  100. ConflictState state = states[chunks.get(idx++)];
  101. int srcIdx = chunks.get(idx++);
  102. int begin = chunks.get(idx++);
  103. int end = chunks.get(idx++);
  104. return new MergeChunk(srcIdx, begin, end, state);
  105. }
  106. @Override
  107. public void remove() {
  108. throw new UnsupportedOperationException();
  109. }
  110. };
  111. }
  112. /**
  113. * Whether this merge result contains conflicts
  114. *
  115. * @return true if this merge result contains conflicts
  116. */
  117. public boolean containsConflicts() {
  118. return containsConflicts;
  119. }
  120. /**
  121. * Sets explicitly whether this merge should be seen as containing a
  122. * conflict or not. Needed because during RecursiveMerger we want to do
  123. * content-merges and take the resulting content (even with conflict
  124. * markers!) as new conflict-free content
  125. *
  126. * @param containsConflicts
  127. * whether this merge should be seen as containing a conflict or
  128. * not.
  129. * @since 3.5
  130. */
  131. protected void setContainsConflicts(boolean containsConflicts) {
  132. this.containsConflicts = containsConflicts;
  133. }
  134. }