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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.Repository;
  50. /**
  51. * A method of combining two or more trees together to form an output tree.
  52. * <p>
  53. * Different strategies may employ different techniques for deciding which paths
  54. * (and ObjectIds) to carry from the input trees into the final output tree.
  55. */
  56. public abstract class MergeStrategy {
  57. /** Simple strategy that sets the output tree to the first input tree. */
  58. public static final MergeStrategy OURS = new StrategyOneSided("ours", 0); //$NON-NLS-1$
  59. /** Simple strategy that sets the output tree to the second input tree. */
  60. public static final MergeStrategy THEIRS = new StrategyOneSided("theirs", 1); //$NON-NLS-1$
  61. /** Simple strategy to merge paths, without simultaneous edits. */
  62. public static final ThreeWayMergeStrategy SIMPLE_TWO_WAY_IN_CORE = new StrategySimpleTwoWayInCore();
  63. /**
  64. * Simple strategy to merge paths. It tries to merge also contents. Multiple
  65. * merge bases are not supported
  66. */
  67. public static final ThreeWayMergeStrategy RESOLVE = new StrategyResolve();
  68. /**
  69. * Recursive strategy to merge paths. It tries to merge also contents.
  70. * Multiple merge bases are supported
  71. */
  72. public static final ThreeWayMergeStrategy RECURSIVE = new StrategyRecursive();
  73. private static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<String, MergeStrategy>();
  74. static {
  75. register(OURS);
  76. register(THEIRS);
  77. register(SIMPLE_TWO_WAY_IN_CORE);
  78. register(RESOLVE);
  79. register(RECURSIVE);
  80. }
  81. /**
  82. * Register a merge strategy so it can later be obtained by name.
  83. *
  84. * @param imp
  85. * the strategy to register.
  86. * @throws IllegalArgumentException
  87. * a strategy by the same name has already been registered.
  88. */
  89. public static void register(final MergeStrategy imp) {
  90. register(imp.getName(), imp);
  91. }
  92. /**
  93. * Register a merge strategy so it can later be obtained by name.
  94. *
  95. * @param name
  96. * name the strategy can be looked up under.
  97. * @param imp
  98. * the strategy to register.
  99. * @throws IllegalArgumentException
  100. * a strategy by the same name has already been registered.
  101. */
  102. public static synchronized void register(final String name,
  103. final MergeStrategy imp) {
  104. if (STRATEGIES.containsKey(name))
  105. throw new IllegalArgumentException(MessageFormat.format(
  106. JGitText.get().mergeStrategyAlreadyExistsAsDefault, name));
  107. STRATEGIES.put(name, imp);
  108. }
  109. /**
  110. * Locate a strategy by name.
  111. *
  112. * @param name
  113. * name of the strategy to locate.
  114. * @return the strategy instance; null if no strategy matches the name.
  115. */
  116. public static synchronized MergeStrategy get(final String name) {
  117. return STRATEGIES.get(name);
  118. }
  119. /**
  120. * Get all registered strategies.
  121. *
  122. * @return the registered strategy instances. No inherit order is returned;
  123. * the caller may modify (and/or sort) the returned array if
  124. * necessary to obtain a reasonable ordering.
  125. */
  126. public static synchronized MergeStrategy[] get() {
  127. final MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()];
  128. STRATEGIES.values().toArray(r);
  129. return r;
  130. }
  131. /** @return default name of this strategy implementation. */
  132. public abstract String getName();
  133. /**
  134. * Create a new merge instance.
  135. *
  136. * @param db
  137. * repository database the merger will read from, and eventually
  138. * write results back to.
  139. * @return the new merge instance which implements this strategy.
  140. */
  141. public abstract Merger newMerger(Repository db);
  142. /**
  143. * Create a new merge instance.
  144. *
  145. * @param db
  146. * repository database the merger will read from, and eventually
  147. * write results back to.
  148. * @param inCore
  149. * the merge will happen in memory, working folder will not be
  150. * modified, in case of a non-trivial merge that requires manual
  151. * resolution, the merger will fail.
  152. * @return the new merge instance which implements this strategy.
  153. */
  154. public abstract Merger newMerger(Repository db, boolean inCore);
  155. }