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.3KB

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