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

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