aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeStrategy.java
blob: ba9c5b7dccb573020a14b8306bf50b1c9fd4361e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2008-2009, Google Inc.
 * Copyright (C) 2009, Matthias Sohn <matthias.sohn@sap.com>
 * Copyright (C) 2012, Research In Motion Limited and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.merge;

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

import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.Repository;

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

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

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

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

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

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

	static {
		register(OURS);
		register(THEIRS);
		register(SIMPLE_TWO_WAY_IN_CORE);
		register(RESOLVE);
		register(RECURSIVE);
	}

	/**
	 * Register a merge strategy so it can later be obtained by name.
	 *
	 * @param imp
	 *            the strategy to register.
	 * @throws java.lang.IllegalArgumentException
	 *             a strategy by the same name has already been registered.
	 */
	public static void register(MergeStrategy imp) {
		register(imp.getName(), imp);
	}

	/**
	 * Register a merge strategy so it can later be obtained by name.
	 *
	 * @param name
	 *            name the strategy can be looked up under.
	 * @param imp
	 *            the strategy to register.
	 * @throws java.lang.IllegalArgumentException
	 *             a strategy by the same name has already been registered.
	 */
	public static synchronized void register(final String name,
			final MergeStrategy imp) {
		if (STRATEGIES.containsKey(name))
			throw new IllegalArgumentException(MessageFormat.format(
					JGitText.get().mergeStrategyAlreadyExistsAsDefault, name));
		STRATEGIES.put(name, imp);
	}

	/**
	 * Locate a strategy by name.
	 *
	 * @param name
	 *            name of the strategy to locate.
	 * @return the strategy instance; null if no strategy matches the name.
	 */
	public static synchronized MergeStrategy get(String name) {
		return STRATEGIES.get(name);
	}

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

	/**
	 * Get default name of this strategy implementation.
	 *
	 * @return default name of this strategy implementation.
	 */
	public abstract String getName();

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

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

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