MergeStrategy.java

  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 and others
  5.  *
  6.  * This program and the accompanying materials are made available under the
  7.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  8.  * https://www.eclipse.org/org/documents/edl-v10.php.
  9.  *
  10.  * SPDX-License-Identifier: BSD-3-Clause
  11.  */

  12. package org.eclipse.jgit.merge;

  13. import java.text.MessageFormat;
  14. import java.util.HashMap;

  15. import org.eclipse.jgit.internal.JGitText;
  16. import org.eclipse.jgit.lib.Config;
  17. import org.eclipse.jgit.lib.ObjectInserter;
  18. import org.eclipse.jgit.lib.Repository;

  19. /**
  20.  * A method of combining two or more trees together to form an output tree.
  21.  * <p>
  22.  * Different strategies may employ different techniques for deciding which paths
  23.  * (and ObjectIds) to carry from the input trees into the final output tree.
  24.  */
  25. public abstract class MergeStrategy {
  26.     /** Simple strategy that sets the output tree to the first input tree. */
  27.     public static final MergeStrategy OURS = new StrategyOneSided("ours", 0); //$NON-NLS-1$

  28.     /** Simple strategy that sets the output tree to the second input tree. */
  29.     public static final MergeStrategy THEIRS = new StrategyOneSided("theirs", 1); //$NON-NLS-1$

  30.     /** Simple strategy to merge paths, without simultaneous edits. */
  31.     public static final ThreeWayMergeStrategy SIMPLE_TWO_WAY_IN_CORE = new StrategySimpleTwoWayInCore();

  32.     /**
  33.      * Simple strategy to merge paths. It tries to merge also contents. Multiple
  34.      * merge bases are not supported
  35.      */
  36.     public static final ThreeWayMergeStrategy RESOLVE = new StrategyResolve();

  37.     /**
  38.      * Recursive strategy to merge paths. It tries to merge also contents.
  39.      * Multiple merge bases are supported
  40.      * @since 3.0
  41.      */
  42.     public static final ThreeWayMergeStrategy RECURSIVE = new StrategyRecursive();

  43.     private static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<>();

  44.     static {
  45.         register(OURS);
  46.         register(THEIRS);
  47.         register(SIMPLE_TWO_WAY_IN_CORE);
  48.         register(RESOLVE);
  49.         register(RECURSIVE);
  50.     }

  51.     /**
  52.      * Register a merge strategy so it can later be obtained by name.
  53.      *
  54.      * @param imp
  55.      *            the strategy to register.
  56.      * @throws java.lang.IllegalArgumentException
  57.      *             a strategy by the same name has already been registered.
  58.      */
  59.     public static void register(MergeStrategy imp) {
  60.         register(imp.getName(), imp);
  61.     }

  62.     /**
  63.      * Register a merge strategy so it can later be obtained by name.
  64.      *
  65.      * @param name
  66.      *            name the strategy can be looked up under.
  67.      * @param imp
  68.      *            the strategy to register.
  69.      * @throws java.lang.IllegalArgumentException
  70.      *             a strategy by the same name has already been registered.
  71.      */
  72.     public static synchronized void register(final String name,
  73.             final MergeStrategy imp) {
  74.         if (STRATEGIES.containsKey(name))
  75.             throw new IllegalArgumentException(MessageFormat.format(
  76.                     JGitText.get().mergeStrategyAlreadyExistsAsDefault, name));
  77.         STRATEGIES.put(name, imp);
  78.     }

  79.     /**
  80.      * Locate a strategy by name.
  81.      *
  82.      * @param name
  83.      *            name of the strategy to locate.
  84.      * @return the strategy instance; null if no strategy matches the name.
  85.      */
  86.     public static synchronized MergeStrategy get(String name) {
  87.         return STRATEGIES.get(name);
  88.     }

  89.     /**
  90.      * Get all registered strategies.
  91.      *
  92.      * @return the registered strategy instances. No inherit order is returned;
  93.      *         the caller may modify (and/or sort) the returned array if
  94.      *         necessary to obtain a reasonable ordering.
  95.      */
  96.     public static synchronized MergeStrategy[] get() {
  97.         final MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()];
  98.         STRATEGIES.values().toArray(r);
  99.         return r;
  100.     }

  101.     /**
  102.      * Get default name of this strategy implementation.
  103.      *
  104.      * @return default name of this strategy implementation.
  105.      */
  106.     public abstract String getName();

  107.     /**
  108.      * Create a new merge instance.
  109.      *
  110.      * @param db
  111.      *            repository database the merger will read from, and eventually
  112.      *            write results back to.
  113.      * @return the new merge instance which implements this strategy.
  114.      */
  115.     public abstract Merger newMerger(Repository db);

  116.     /**
  117.      * Create a new merge instance.
  118.      *
  119.      * @param db
  120.      *            repository database the merger will read from, and eventually
  121.      *            write results back to.
  122.      * @param inCore
  123.      *            the merge will happen in memory, working folder will not be
  124.      *            modified, in case of a non-trivial merge that requires manual
  125.      *            resolution, the merger will fail.
  126.      * @return the new merge instance which implements this strategy.
  127.      */
  128.     public abstract Merger newMerger(Repository db, boolean inCore);

  129.     /**
  130.      * Create a new merge instance.
  131.      * <p>
  132.      * The merge will happen in memory, working folder will not be modified, in
  133.      * case of a non-trivial merge that requires manual resolution, the merger
  134.      * will fail.
  135.      *
  136.      * @param inserter
  137.      *            inserter to write results back to.
  138.      * @param config
  139.      *            repo config for reading diff algorithm settings.
  140.      * @return the new merge instance which implements this strategy.
  141.      * @since 4.8
  142.      */
  143.     public abstract Merger newMerger(ObjectInserter inserter, Config config);
  144. }