Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c3d941cf3f266a4aaa480ab73c2f7e11df7e360 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*******************************************************************************
 * Copyright (c) 2006, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.ui.synchronize;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.mapping.ModelProvider;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.diff.FastDiffFilter;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.diff.IDiffTree;
import org.eclipse.team.core.diff.IThreeWayDiff;
import org.eclipse.team.core.mapping.IMergeContext;
import org.eclipse.team.core.mapping.IMergeStatus;
import org.eclipse.team.core.mapping.IResourceMappingMerger;
import org.eclipse.team.core.mapping.ISynchronizationContext;
import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.TeamUIMessages;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.internal.ui.dialogs.NoChangesDialog;
import org.eclipse.ui.IWorkbenchPart;

/**
 * A model operation that executes a merge according to the merge lifecycle
 * associated with an {@link IMergeContext} and {@link IResourceMappingMerger}
 * instances obtained from the model providers involved.
 *
 * @since 3.2
 */
public abstract class ModelMergeOperation extends ModelOperation {

	/**
	 * Validate the merge context with the model providers that have mappings in
	 * the scope of the context. The {@link IResourceMappingMerger} for each
	 * model provider will be consulted and any non-OK status will be
	 * accumulated and returned,
	 *
	 * @param context
	 *            the merge context being validated
	 * @param monitor
	 *            a progress monitor
	 * @return a status or multi-status that identify any conditions that should
	 *         force a preview of the merge
	 */
	public static IStatus validateMerge(IMergeContext context, IProgressMonitor monitor) {
		try {
			ModelProvider[] providers = context.getScope().getModelProviders();
			monitor.beginTask(null, 100 * providers.length);
			List<IStatus> notOK = new ArrayList<>();
			for (ModelProvider provider : providers) {
				IStatus status = validateMerge(provider, context, Policy.subMonitorFor(monitor, 100));
				if (!status.isOK())
					notOK.add(status);
			}
			if (notOK.isEmpty())
				return Status.OK_STATUS;
			if (notOK.size() == 1)
				return notOK.get(0);
			return new MultiStatus(TeamUIPlugin.ID, 0, notOK.toArray(new IStatus[notOK.size()]), TeamUIMessages.ResourceMappingMergeOperation_3, null);
		} finally {
			monitor.done();
		}
	}

	/*
	 * Validate the merge by obtaining the {@link IResourceMappingMerger} for the
	 * given provider.
	 * @param provider the model provider
	 * @param context the merge context
	 * @param monitor a progress monitor
	 * @return the status obtained from the merger for the provider
	 */
	private static IStatus validateMerge(ModelProvider provider, IMergeContext context, IProgressMonitor monitor) {
		IResourceMappingMerger merger = getMerger(provider);
		if (merger == null)
			return Status.OK_STATUS;
		return merger.validateMerge(context, monitor);
	}

	/*
	 * Return the auto-merger associated with the given model provider using the
	 * adaptable mechanism. If the model provider does not have a merger
	 * associated with it, a default merger that performs the merge at the file
	 * level is returned.
	 *
	 * @param provider
	 *            the model provider of the elements to be merged (must not be
	 *            <code>null</code>)
	 * @return a merger
	 */
	private static IResourceMappingMerger getMerger(ModelProvider provider) {
		Assert.isNotNull(provider);
		return Adapters.adapt(provider, IResourceMappingMerger.class);
	}

	/**
	 * Create a model merge operation.
	 * @param part the workbench part from which the operation was requested or <code>null</code>
	 * @param manager the scope manager
	 */
	protected ModelMergeOperation(IWorkbenchPart part, ISynchronizationScopeManager manager) {
		super(part, manager);
	}

	/**
	 * Perform a merge. First {@link #initializeContext(IProgressMonitor)} is
	 * called to determine the set of resource changes. Then the
	 * {@link #executeMerge(IProgressMonitor)} method is invoked.
	 *
	 * @param monitor a progress monitor
	 */
	@Override
	protected void execute(IProgressMonitor monitor)
			throws InvocationTargetException, InterruptedException {
		try {
			monitor.beginTask(null, 100);
			initializeContext(Policy.subMonitorFor(monitor, 50));
			executeMerge(Policy.subMonitorFor(monitor, 50));
		} catch (CoreException e) {
			throw new InvocationTargetException(e);
		} finally {
			monitor.done();
		}
	}

	/**
	 * Perform a merge. This method is invoked from
	 * {@link #execute(IProgressMonitor)} after the context has been
	 * initialized. If there are changes in the context, they will be validating
	 * by calling {@link #validateMerge(IMergeContext, IProgressMonitor)}. If
	 * there are no validation problems, {@link #performMerge(IProgressMonitor)}
	 * will then be called to perform the merge. If there are problems encountered
	 * or if a preview was requested, {@link #handlePreviewRequest()} is called.
	 *
	 * @param monitor a progress monitor
	 */
	protected void executeMerge(IProgressMonitor monitor) throws CoreException {
		monitor.beginTask(null, 100);
		if (!hasChangesOfInterest()) {
			handleNoChanges();
		} else if (isPreviewRequested()) {
			handlePreviewRequest();
		} else {
			IStatus status = ModelMergeOperation.validateMerge(getMergeContext(), Policy.subMonitorFor(monitor, 10));
			if (!status.isOK()) {
				handleValidationFailure(status);
			} else {
				status = performMerge(Policy.subMonitorFor(monitor, 90));
				if (!status.isOK()) {
					handleMergeFailure(status);
				}
			}
		}
		monitor.done();
	}

	/**
	 * A preview of the merge has been requested. By default, this method does
	 * nothing. Subclasses that wish to support previewing must override this
	 * method to preview the merge and the {@link #getPreviewRequestMessage()}
	 * to have the option presented to the user if the scope changes.
	 */
	protected void handlePreviewRequest() {
		// Do nothing
	}

	/**
	 * Initialize the merge context for this merge operation.
	 * After this method is invoked, the {@link #getContext()}
	 * method must return an instance of {@link IMergeContext}
	 * that is fully initialized.
	 * @param monitor a progress monitor
	 * @throws CoreException
	 */
	protected abstract void initializeContext(IProgressMonitor monitor) throws CoreException;

	/**
	 * Method invoked when the context contains changes that failed validation
	 * by at least one {@link IResourceMappingMerger}.
	 * By default, the user is prompted to inform them that unmergeable changes were found
	 * and the {@link #handlePreviewRequest()} method is invoked.
	 * Subclasses may override.
	 * @param status the status returned from the mergers that reported the validation failures
	 */
	protected void handleValidationFailure(final IStatus status) {
		final boolean[] result = new boolean[] { false };
		Runnable runnable = () -> {
			ErrorDialog dialog = new ErrorDialog(getShell(), TeamUIMessages.ModelMergeOperation_0, TeamUIMessages.ModelMergeOperation_1, status, IStatus.ERROR | IStatus.WARNING | IStatus.INFO) {
				@Override
				protected void createButtonsForButtonBar(Composite parent) {
					createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL,
							false);
					createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL,
							true);
					createDetailsButton(parent);
				}

				@Override
				protected void buttonPressed(int id) {
					if (id == IDialogConstants.YES_ID)
						super.buttonPressed(IDialogConstants.OK_ID);
					else if (id == IDialogConstants.NO_ID)
						super.buttonPressed(IDialogConstants.CANCEL_ID);
					super.buttonPressed(id);
				}
			};
			int code = dialog.open();
			result[0] = code == 0;
		};
		getShell().getDisplay().syncExec(runnable);
		if (result[0])
			handlePreviewRequest();
	}

	/**
	 * Method invoked when the context contains unmergable changes.
	 * By default, the user is prompted to inform them that unmergeable changes were found.
	 * Subclasses may override.
	 * @param status the status returned from the merger that reported the conflict
	 */
	protected void handleMergeFailure(final IStatus status) {
		Display.getDefault().syncExec(() -> MessageDialog.openInformation(getShell(), TeamUIMessages.MergeIncomingChangesAction_0, status.getMessage()));
		handlePreviewRequest();
	}

	/**
	 * Method invoked when the context contains no changes.
	 * By default, the user is prompted to inform them that no changes were found.
	 * Subclasses may override.
	 */
	protected void handleNoChanges() {
		Display.getDefault().syncExec(() -> NoChangesDialog.open(getShell(), TeamUIMessages.ResourceMappingMergeOperation_0, TeamUIMessages.ResourceMappingMergeOperation_1, TeamUIMessages.ModelMergeOperation_3, getScope().asInputScope()));
	}

	/**
	 * Attempt a headless merge of the elements in the context of this
	 * operation. The merge is performed by obtaining the
	 * {@link IResourceMappingMerger} for the model providers in the context's
	 * scope. The merger of the model providers are invoked in the order
	 * determined by the {@link ModelOperation#sortByExtension(ModelProvider[])}
	 * method. The method will stop on the first conflict encountered.
	 * This method will throw a runtime exception
	 * if the operation does not have a merge context.
	 *
	 * @param monitor
	 *            a progress monitor
	 * @return a status that indicates whether the merge succeeded.
	 * @throws CoreException
	 *             if an error occurred
	 */
	protected IStatus performMerge(IProgressMonitor monitor) throws CoreException {
		ISynchronizationContext sc = getContext();
		if (sc instanceof IMergeContext) {
			IMergeContext context = (IMergeContext) sc;
			final ModelProvider[] providers = sortByExtension(context.getScope().getModelProviders());
			final IStatus[] result = new IStatus[] { Status.OK_STATUS };
			context.run(monitor1 -> {
				try {
					int ticks = 100;
					monitor1.beginTask(null, ticks + ((providers.length - 1) * 10));
					for (ModelProvider provider : providers) {
						IStatus status = performMerge(provider, Policy.subMonitorFor(monitor1, ticks));
						ticks = 10;
						if (!status.isOK()) {
							// Stop at the first failure
							result[0] = status;
							return;
						}
						try {
							Job.getJobManager().join(getContext(), monitor1);
						} catch (InterruptedException e) {
							// Ignore
						}
					}
				} finally {
					monitor1.done();
				}
			}, null /* scheduling rule */, IResource.NONE, monitor);
			return result[0];
		}
		return noMergeContextAvailable();
	}

	/**
	 * Attempt to merge all the mappings that come from the given provider.
	 * Return a status which indicates whether the merge succeeded or if
	 * unmergeable conflicts were found. By default, this method invokes
	 * the {@link IResourceMappingMerger#merge(IMergeContext, IProgressMonitor)}
	 * method but does not wait for the context to update (see {@link ISynchronizationContext}.
	 * Callers that are invoking the merge on multiple models should wait until the
	 * context has updated before invoking merge on another merger. The following
	 * line of code will wait for the context to update:
	 * <pre>
	 * Job.getJobManager().join(getContext(), monitor);
	 * </pre>
	 * <p>
	 * This method will throw a runtime exception
	 * if the operation does not have a merge context.
	 * @param provider the model provider whose mappings are to be merged
	 * @param monitor a progress monitor
	 * @return a non-OK status if there were unmergable conflicts
	 * @throws CoreException if an error occurred
	 */
	protected IStatus performMerge(ModelProvider provider, IProgressMonitor monitor) throws CoreException {
		ISynchronizationContext sc = getContext();
		if (sc instanceof IMergeContext) {
			IMergeContext context = (IMergeContext) sc;
			IResourceMappingMerger merger = getMerger(provider);
			if (merger != null) {
				IStatus status = merger.merge(context, monitor);
				if (status.isOK() || status.getCode() == IMergeStatus.CONFLICTS) {
					return status;
				}
				throw new TeamException(status);
			}
			return Status.OK_STATUS;
		}
		return noMergeContextAvailable();
	}

	private IStatus noMergeContextAvailable() {
		throw new IllegalStateException(TeamUIMessages.ModelMergeOperation_2);
	}

	/**
	 * Return whether the context of this operation has changes that are
	 * of interest to the operation. Subclasses may override.
	 * @return whether the context of this operation has changes that are
	 * of interest to the operation
	 */
	protected boolean hasChangesOfInterest() {
		return !getContext().getDiffTree().isEmpty() && hasIncomingChanges(getContext().getDiffTree());
	}

	private boolean hasIncomingChanges(IDiffTree tree) {
		return tree.hasMatchingDiffs(ResourcesPlugin.getWorkspace().getRoot().getFullPath(), new FastDiffFilter() {
			@Override
			public boolean select(IDiff node) {
				if (node instanceof IThreeWayDiff) {
					IThreeWayDiff twd = (IThreeWayDiff) node;
					int direction = twd.getDirection();
					if (direction == IThreeWayDiff.INCOMING || direction == IThreeWayDiff.CONFLICTING) {
						return true;
					}
				} else {
					// Return true for any two-way change
					return true;
				}
				return false;
			}
		});
	}

	private IMergeContext getMergeContext() {
		return (IMergeContext)getContext();
	}
}

Back to the top