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.

MergeStrategy.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2009, Matthias Sohn <matthias.sohn@sap.com>
  4. * Copyright (C) 2012, Research In Motion Limited
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.merge;
  46. import java.text.MessageFormat;
  47. import java.util.HashMap;
  48. import org.eclipse.jgit.internal.JGitText;
  49. import org.eclipse.jgit.lib.Config;
  50. import org.eclipse.jgit.lib.ObjectInserter;
  51. import org.eclipse.jgit.lib.Repository;
  52. /**
  53. * A method of combining two or more trees together to form an output tree.
  54. * <p>
  55. * Different strategies may employ different techniques for deciding which paths
  56. * (and ObjectIds) to carry from the input trees into the final output tree.
  57. */
  58. public abstract class MergeStrategy {
  59. /** Simple strategy that sets the output tree to the first input tree. */
  60. public static final MergeStrategy OURS = new StrategyOneSided("ours", 0); //$NON-NLS-1$
  61. /** Simple strategy that sets the output tree to the second input tree. */
  62. public static final MergeStrategy THEIRS = new StrategyOneSided("theirs", 1); //$NON-NLS-1$
  63. /** Simple strategy to merge paths, without simultaneous edits. */
  64. public static final ThreeWayMergeStrategy SIMPLE_TWO_WAY_IN_CORE = new StrategySimpleTwoWayInCore();
  65. /**
  66. * Simple strategy to merge paths. It tries to merge also contents. Multiple
  67. * merge bases are not supported
  68. */
  69. public static final ThreeWayMergeStrategy RESOLVE = new StrategyResolve();
  70. /**
  71. * Recursive strategy to merge paths. It tries to merge also contents.
  72. * Multiple merge bases are supported
  73. * @since 3.0
  74. */
  75. public static final ThreeWayMergeStrategy RECURSIVE = new StrategyRecursive();
  76. private static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<>();
  77. static {
  78. register(OURS);
  79. register(THEIRS);
  80. register(SIMPLE_TWO_WAY_IN_CORE);
  81. register(RESOLVE);
  82. register(RECURSIVE);
  83. }
  84. /**
  85. * Register a merge strategy so it can later be obtained by name.
  86. *
  87. * @param imp
  88. * the strategy to register.
  89. * @throws IllegalArgumentException
  90. * a strategy by the same name has already been registered.
  91. */
  92. public static void register(final MergeStrategy imp) {
  93. register(imp.getName(), imp);
  94. }
  95. /**
  96. * Register a merge strategy so it can later be obtained by name.
  97. *
  98. * @param name
  99. * name the strategy can be looked up under.
  100. * @param imp
  101. * the strategy to register.
  102. * @throws IllegalArgumentException
  103. * a strategy by the same name has already been registered.
  104. */
  105. public static synchronized void register(final String name,
  106. final MergeStrategy imp) {
  107. if (STRATEGIES.containsKey(name))
  108. throw new IllegalArgumentException(MessageFormat.format(
  109. JGitText.get().mergeStrategyAlreadyExistsAsDefault, name));
  110. STRATEGIES.put(name, imp);
  111. }
  112. /**
  113. * Locate a strategy by name.
  114. *
  115. * @param name
  116. * name of the strategy to locate.
  117. * @return the strategy instance; null if no strategy matches the name.
  118. */
  119. public static synchronized MergeStrategy get(final String name) {
  120. return STRATEGIES.get(name);
  121. }
  122. /**
  123. * Get all registered strategies.
  124. *
  125. * @return the registered strategy instances. No inherit order is returned;
  126. * the caller may modify (and/or sort) the returned array if
  127. * necessary to obtain a reasonable ordering.
  128. */
  129. public static synchronized MergeStrategy[] get() {
  130. final MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()];
  131. STRATEGIES.values().toArray(r);
  132. return r;
  133. }
  134. /** @return default name of this strategy implementation. */
  135. public abstract String getName();
  136. /**
  137. * Create a new merge instance.
  138. *
  139. * @param db
  140. * repository database the merger will read from, and eventually
  141. * write results back to.
  142. * @return the new merge instance which implements this strategy.
  143. */
  144. public abstract Merger newMerger(Repository db);
  145. /**
  146. * Create a new merge instance.
  147. *
  148. * @param db
  149. * repository database the merger will read from, and eventually
  150. * write results back to.
  151. * @param inCore
  152. * the merge will happen in memory, working folder will not be
  153. * modified, in case of a non-trivial merge that requires manual
  154. * resolution, the merger will fail.
  155. * @return the new merge instance which implements this strategy.
  156. */
  157. public abstract Merger newMerger(Repository db, boolean inCore);
  158. /**
  159. * Create a new merge instance.
  160. * <p>
  161. * The merge will happen in memory, working folder will not be modified, in
  162. * case of a non-trivial merge that requires manual resolution, the merger
  163. * will fail.
  164. *
  165. * @param inserter
  166. * inserter to write results back to.
  167. * @param config
  168. * repo config for reading diff algorithm settings.
  169. * @return the new merge instance which implements this strategy.
  170. * @since 4.8
  171. */
  172. public abstract Merger newMerger(ObjectInserter inserter, Config config);
  173. }