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 13 package org.eclipse.jgit.merge; 14 15 import java.text.MessageFormat; 16 import java.util.HashMap; 17 18 import org.eclipse.jgit.internal.JGitText; 19 import org.eclipse.jgit.lib.Config; 20 import org.eclipse.jgit.lib.ObjectInserter; 21 import org.eclipse.jgit.lib.Repository; 22 23 /** 24 * A method of combining two or more trees together to form an output tree. 25 * <p> 26 * Different strategies may employ different techniques for deciding which paths 27 * (and ObjectIds) to carry from the input trees into the final output tree. 28 */ 29 public abstract class MergeStrategy { 30 /** Simple strategy that sets the output tree to the first input tree. */ 31 public static final MergeStrategy OURS = new StrategyOneSided("ours", 0); //$NON-NLS-1$ 32 33 /** Simple strategy that sets the output tree to the second input tree. */ 34 public static final MergeStrategy THEIRS = new StrategyOneSided("theirs", 1); //$NON-NLS-1$ 35 36 /** Simple strategy to merge paths, without simultaneous edits. */ 37 public static final ThreeWayMergeStrategy SIMPLE_TWO_WAY_IN_CORE = new StrategySimpleTwoWayInCore(); 38 39 /** 40 * Simple strategy to merge paths. It tries to merge also contents. Multiple 41 * merge bases are not supported 42 */ 43 public static final ThreeWayMergeStrategy RESOLVE = new StrategyResolve(); 44 45 /** 46 * Recursive strategy to merge paths. It tries to merge also contents. 47 * Multiple merge bases are supported 48 * @since 3.0 49 */ 50 public static final ThreeWayMergeStrategy RECURSIVE = new StrategyRecursive(); 51 52 private static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<>(); 53 54 static { 55 register(OURS); 56 register(THEIRS); 57 register(SIMPLE_TWO_WAY_IN_CORE); 58 register(RESOLVE); 59 register(RECURSIVE); 60 } 61 62 /** 63 * Register a merge strategy so it can later be obtained by name. 64 * 65 * @param imp 66 * the strategy to register. 67 * @throws java.lang.IllegalArgumentException 68 * a strategy by the same name has already been registered. 69 */ 70 public static void register(MergeStrategy imp) { 71 register(imp.getName(), imp); 72 } 73 74 /** 75 * Register a merge strategy so it can later be obtained by name. 76 * 77 * @param name 78 * name the strategy can be looked up under. 79 * @param imp 80 * the strategy to register. 81 * @throws java.lang.IllegalArgumentException 82 * a strategy by the same name has already been registered. 83 */ 84 public static synchronized void register(final String name, 85 final MergeStrategy imp) { 86 if (STRATEGIES.containsKey(name)) 87 throw new IllegalArgumentException(MessageFormat.format( 88 JGitText.get().mergeStrategyAlreadyExistsAsDefault, name)); 89 STRATEGIES.put(name, imp); 90 } 91 92 /** 93 * Locate a strategy by name. 94 * 95 * @param name 96 * name of the strategy to locate. 97 * @return the strategy instance; null if no strategy matches the name. 98 */ 99 public static synchronized MergeStrategy get(String name) { 100 return STRATEGIES.get(name); 101 } 102 103 /** 104 * Get all registered strategies. 105 * 106 * @return the registered strategy instances. No inherit order is returned; 107 * the caller may modify (and/or sort) the returned array if 108 * necessary to obtain a reasonable ordering. 109 */ 110 public static synchronized MergeStrategy[] get() { 111 final MergeStrategytegy.html#MergeStrategy">MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()]; 112 STRATEGIES.values().toArray(r); 113 return r; 114 } 115 116 /** 117 * Get default name of this strategy implementation. 118 * 119 * @return default name of this strategy implementation. 120 */ 121 public abstract String getName(); 122 123 /** 124 * Create a new merge instance. 125 * 126 * @param db 127 * repository database the merger will read from, and eventually 128 * write results back to. 129 * @return the new merge instance which implements this strategy. 130 */ 131 public abstract Merger newMerger(Repository db); 132 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 * @param inCore 140 * the merge will happen in memory, working folder will not be 141 * modified, in case of a non-trivial merge that requires manual 142 * resolution, the merger will fail. 143 * @return the new merge instance which implements this strategy. 144 */ 145 public abstract Merger newMerger(Repository db, boolean inCore); 146 147 /** 148 * Create a new merge instance. 149 * <p> 150 * The merge will happen in memory, working folder will not be modified, in 151 * case of a non-trivial merge that requires manual resolution, the merger 152 * will fail. 153 * 154 * @param inserter 155 * inserter to write results back to. 156 * @param config 157 * repo config for reading diff algorithm settings. 158 * @return the new merge instance which implements this strategy. 159 * @since 4.8 160 */ 161 public abstract Merger newMerger(ObjectInserter inserter, Config config); 162 }