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.

StrategySimpleTwoWayInCore.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  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.io.IOException;
  45. import org.eclipse.jgit.dircache.DirCache;
  46. import org.eclipse.jgit.dircache.DirCacheBuilder;
  47. import org.eclipse.jgit.dircache.DirCacheEntry;
  48. import org.eclipse.jgit.errors.UnmergedPathException;
  49. import org.eclipse.jgit.lib.FileMode;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.lib.ObjectInserter;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  54. import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
  55. /**
  56. * Merges two commits together in-memory, ignoring any working directory.
  57. * <p>
  58. * The strategy chooses a path from one of the two input trees if the path is
  59. * unchanged in the other relative to their common merge base tree. This is a
  60. * trivial 3-way merge (at the file path level only).
  61. * <p>
  62. * Modifications of the same file path (content and/or file mode) by both input
  63. * trees will cause a merge conflict, as this strategy does not attempt to merge
  64. * file contents.
  65. */
  66. public class StrategySimpleTwoWayInCore extends ThreeWayMergeStrategy {
  67. /** Create a new instance of the strategy. */
  68. protected StrategySimpleTwoWayInCore() {
  69. //
  70. }
  71. @Override
  72. public String getName() {
  73. return "simple-two-way-in-core"; //$NON-NLS-1$
  74. }
  75. @Override
  76. public ThreeWayMerger newMerger(final Repository db) {
  77. return new InCoreMerger(db);
  78. }
  79. @Override
  80. public ThreeWayMerger newMerger(Repository db, boolean inCore) {
  81. // This class is always inCore, so ignore the parameter
  82. return newMerger(db);
  83. }
  84. private static class InCoreMerger extends ThreeWayMerger {
  85. private static final int T_BASE = 0;
  86. private static final int T_OURS = 1;
  87. private static final int T_THEIRS = 2;
  88. private final NameConflictTreeWalk tw;
  89. private final DirCache cache;
  90. private DirCacheBuilder builder;
  91. private ObjectId resultTree;
  92. InCoreMerger(final Repository local) {
  93. super(local);
  94. tw = new NameConflictTreeWalk(reader);
  95. cache = DirCache.newInCore();
  96. }
  97. @Override
  98. protected boolean mergeImpl() throws IOException {
  99. tw.addTree(mergeBase());
  100. tw.addTree(sourceTrees[0]);
  101. tw.addTree(sourceTrees[1]);
  102. boolean hasConflict = false;
  103. builder = cache.builder();
  104. while (tw.next()) {
  105. final int modeO = tw.getRawMode(T_OURS);
  106. final int modeT = tw.getRawMode(T_THEIRS);
  107. if (modeO == modeT && tw.idEqual(T_OURS, T_THEIRS)) {
  108. add(T_OURS, DirCacheEntry.STAGE_0);
  109. continue;
  110. }
  111. final int modeB = tw.getRawMode(T_BASE);
  112. if (modeB == modeO && tw.idEqual(T_BASE, T_OURS))
  113. add(T_THEIRS, DirCacheEntry.STAGE_0);
  114. else if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))
  115. add(T_OURS, DirCacheEntry.STAGE_0);
  116. else {
  117. if (nonTree(modeB)) {
  118. add(T_BASE, DirCacheEntry.STAGE_1);
  119. hasConflict = true;
  120. }
  121. if (nonTree(modeO)) {
  122. add(T_OURS, DirCacheEntry.STAGE_2);
  123. hasConflict = true;
  124. }
  125. if (nonTree(modeT)) {
  126. add(T_THEIRS, DirCacheEntry.STAGE_3);
  127. hasConflict = true;
  128. }
  129. if (tw.isSubtree())
  130. tw.enterSubtree();
  131. }
  132. }
  133. builder.finish();
  134. builder = null;
  135. if (hasConflict)
  136. return false;
  137. try {
  138. ObjectInserter odi = getObjectInserter();
  139. resultTree = cache.writeTree(odi);
  140. odi.flush();
  141. return true;
  142. } catch (UnmergedPathException upe) {
  143. resultTree = null;
  144. return false;
  145. }
  146. }
  147. private static boolean nonTree(final int mode) {
  148. return mode != 0 && !FileMode.TREE.equals(mode);
  149. }
  150. private void add(final int tree, final int stage) throws IOException {
  151. final AbstractTreeIterator i = getTree(tree);
  152. if (i != null) {
  153. if (FileMode.TREE.equals(tw.getRawMode(tree))) {
  154. builder.addTree(tw.getRawPath(), stage, reader, tw
  155. .getObjectId(tree));
  156. } else {
  157. final DirCacheEntry e;
  158. e = new DirCacheEntry(tw.getRawPath(), stage);
  159. e.setObjectIdFromRaw(i.idBuffer(), i.idOffset());
  160. e.setFileMode(tw.getFileMode(tree));
  161. builder.add(e);
  162. }
  163. }
  164. }
  165. private AbstractTreeIterator getTree(final int tree) {
  166. return tw.getTree(tree, AbstractTreeIterator.class);
  167. }
  168. @Override
  169. public ObjectId getResultTreeId() {
  170. return resultTree;
  171. }
  172. }
  173. }