Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 697a93115aa353617e0a0c82c1df360ce8414538 (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
/*******************************************************************************
 * 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.internal.ui.mapping;

import org.eclipse.compare.*;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.diff.IThreeWayDiff;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.mapping.IResourceDiff;
import org.eclipse.team.core.mapping.ISynchronizationContext;
import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.history.FileRevisionTypedElement;
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
import org.eclipse.team.ui.mapping.ISynchronizationCompareInput;
import org.eclipse.team.ui.mapping.SaveableComparison;

/**
 * A resource-based compare input that gets it's contributors from an {@link IDiff}.
 */
public class ResourceDiffCompareInput extends AbstractCompareInput implements ISynchronizationCompareInput, IAdaptable, IResourceProvider {

	private IDiff node;
	private final ISynchronizationContext context;

	public static int getCompareKind(IDiff node) {
		int compareKind = 0;
		if (node != null) {
			switch (node.getKind()) {
			case IDiff.ADD:
				compareKind = Differencer.ADDITION;
				break;
			case IDiff.REMOVE:
				compareKind = Differencer.DELETION;
				break;
			case IDiff.CHANGE:
				compareKind = Differencer.CHANGE;
				break;
			}
			if (node instanceof IThreeWayDiff) {
				IThreeWayDiff twd = (IThreeWayDiff) node;
				switch (twd.getDirection()) {
				case IThreeWayDiff.OUTGOING :
					compareKind |= Differencer.RIGHT;
					break;
				case IThreeWayDiff.INCOMING :
					compareKind |= Differencer.LEFT;
					break;
				case IThreeWayDiff.CONFLICTING :
					compareKind |= Differencer.LEFT;
					compareKind |= Differencer.RIGHT;
					break;
				}
			}
		}
		return compareKind;
	}

	private static FileRevisionTypedElement getRightContributor(IDiff node) {
		// For a resource diff, use the after state
		if (node instanceof IResourceDiff) {
			IResourceDiff rd = (IResourceDiff) node;
			return asTypedElement(rd.getAfterState(), getLocalEncoding(node));
		}
		if (node instanceof IThreeWayDiff) {
			IThreeWayDiff twd = (IThreeWayDiff) node;
			IResourceDiff diff = (IResourceDiff)twd.getRemoteChange();
			// If there is a remote change, use the after state
			if (diff != null)
				return getRightContributor(diff);
			// There's no remote change so use the before state of the local
			diff = (IResourceDiff)twd.getLocalChange();
			return asTypedElement(diff.getBeforeState(), getLocalEncoding(node));

		}
		return null;
	}

	private static LocalResourceTypedElement getLeftContributor(final IDiff node) {
		// The left contributor is always the local resource
		return new LocalResourceTypedElement(ResourceDiffTree.getResourceFor(node));
	}

	private static FileRevisionTypedElement getAncestor(IDiff node) {
		if (node instanceof IThreeWayDiff) {
			IThreeWayDiff twd = (IThreeWayDiff) node;
			IResourceDiff diff = (IResourceDiff)twd.getLocalChange();
			if (diff == null)
				diff = (IResourceDiff)twd.getRemoteChange();
			return asTypedElement(diff.getBeforeState(), getLocalEncoding(node));

		}
		return null;
	}

	private static String getLocalEncoding(IDiff node) {
		IResource resource = ResourceDiffTree.getResourceFor(node);
		if (resource instanceof IEncodedStorage) {
			IEncodedStorage es = (IEncodedStorage) resource;
			try {
				return es.getCharset();
			} catch (CoreException e) {
				TeamUIPlugin.log(e);
			}
		}
		return null;
	}

	private static FileRevisionTypedElement asTypedElement(IFileRevision state, String localEncoding) {
		if (state == null)
			return null;
		return new FileRevisionTypedElement(state, localEncoding);
	}

	public static void ensureContentsCached(IDiff diff, IProgressMonitor monitor) throws CoreException {
		if (diff != null) {
			ensureContentsCached(getAncestor(diff), getRightContributor(diff), monitor);
		}
	}

	private static void ensureContentsCached(Object ancestor, Object right,
			IProgressMonitor monitor) throws CoreException {
		SubMonitor sm = SubMonitor.convert(monitor, 100);
		if (ancestor instanceof FileRevisionTypedElement) {
			FileRevisionTypedElement fste = (FileRevisionTypedElement) ancestor;
			fste.cacheContents(sm.newChild(50));
		} else {
			sm.setWorkRemaining(50);
		}
		if (right instanceof FileRevisionTypedElement) {
			FileRevisionTypedElement fste = (FileRevisionTypedElement) right;
			fste.cacheContents(sm.newChild(50));
		}
		if (monitor != null)
			monitor.done();
	}

	/**
	 * Create the compare input on the given diff.
	 * @param diff the diff
	 * @param context the synchronization context
	 */
	public ResourceDiffCompareInput(IDiff diff, ISynchronizationContext context) {
		super(getCompareKind(diff), getAncestor(diff), getLeftContributor(diff), getRightContributor(diff));
		this.node = diff;
		this.context = context;
	}

	/**
	 * Fire a compare input change event.
	 * This method is public so that the change can be fired
	 * by the containing editor input on a save.
	 */
	@Override
	public void fireChange() {
		super.fireChange();
	}

	@Override
	public void prepareInput(CompareConfiguration configuration, IProgressMonitor monitor) throws CoreException {
		configuration.setLabelProvider(this, ((ResourceCompareInputChangeNotifier)getChangeNotifier()).getLabelProvider());
		ensureContentsCached(getAncestor(), getRight(), monitor);
	}

	@Override
	public SaveableComparison getSaveable() {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (adapter == IFile.class || adapter == IResource.class) {
			return (T) ResourceDiffTree.getResourceFor(node);
		}
		if (adapter == ResourceMapping.class) {
			IResource resource = ResourceDiffTree.getResourceFor(node);
			return resource.getAdapter(adapter);
		}
		return null;
	}

	@Override
	public String getFullPath() {
		final IResource resource = ResourceDiffTree.getResourceFor(node);
		if (resource != null)
			return resource.getFullPath().toString();
		return getName();
	}

	@Override
	public boolean isCompareInputFor(Object object) {
		final IResource resource = ResourceDiffTree.getResourceFor(node);
		IResource other = Utils.getResource(object);
		if (resource != null && other != null)
			return resource.equals(other);
		return false;
	}

	@Override
	public IResource getResource() {
		return ResourceDiffTree.getResourceFor(node);
	}

	/**
	 * Return a compare input change notifier that will detect changes in the synchronization context and
	 * translate them into compare input change events by calling {@link #update()}.
	 * @return a compare input change notifier
	 */
	@Override
	public CompareInputChangeNotifier getChangeNotifier() {
		return ResourceCompareInputChangeNotifier.getChangeNotifier(context);
	}

	@Override
	public boolean equals(Object other) {
		if (other == this)
			return true;
		if (other instanceof ResourceDiffCompareInput) {
			ResourceDiffCompareInput otherInput = (ResourceDiffCompareInput) other;
			return (isEqual(getLeft(), otherInput.getLeft())
					&& isEqual(getRight(), otherInput.getRight())
					&& isEqual(getAncestor(), otherInput.getAncestor()));
		}
		return false;
	}

	private boolean isEqual(ITypedElement e1, ITypedElement e2) {
		if (e1 == null) {
			return e2 == null;
		}
		if (e2 == null)
			return false;
		return e1.equals(e2);
	}

	@Override
	public int hashCode() {
		return getResource().hashCode();
	}

	/**
	 * Re-obtain the diff for this compare input and update the kind and 3
	 * contributor appropriately.
	 */
	@Override
	public void update() {
		IDiff newNode = context.getDiffTree().getDiff(getResource());
		if (newNode == null) {
			// The resource is in-sync. Just leave the ancestor and right the same and set the kind
			setKind(Differencer.NO_CHANGE);
			fireChange();
		} else {
			LocalResourceTypedElement left = (LocalResourceTypedElement)getLeft();
			if (!this.node.equals(newNode) || !left.isSynchronized()) {
				this.node = newNode;
				setKind(getCompareKind(node));
				left.update();
				FileRevisionTypedElement newRight = getRightContributor(node);
				propogateAuthorIfSameRevision((FileRevisionTypedElement)getRight(), newRight);
				setRight(newRight);
				FileRevisionTypedElement newAncestor = getAncestor(node);
				propogateAuthorIfSameRevision((FileRevisionTypedElement)getAncestor(), newAncestor);
				setAncestor(newAncestor);
				propogateAuthorIfSameRevision((FileRevisionTypedElement)getAncestor(), (FileRevisionTypedElement)getRight());
			}
			fireChange();
		}
	}

	private boolean propogateAuthorIfSameRevision(FileRevisionTypedElement oldContributor, FileRevisionTypedElement newContributor) {
		if (oldContributor == null || newContributor == null)
			return false;
		String author= oldContributor.getAuthor();
		if (newContributor.getAuthor() == null && author != null && oldContributor.getContentIdentifier().equals(newContributor.getContentIdentifier())) {
			newContributor.setAuthor(author);
			return true;
		}
		return false;
	}

	private boolean propogateAuthorIfSameRevision(FileRevisionTypedElement oldContributor, LocalResourceTypedElement newContributor) {
		if (oldContributor == null || newContributor == null)
			return false;
		String author= oldContributor.getAuthor();
		if (newContributor.getAuthor() == null && author != null && oldContributor.getContentIdentifier().equals(getLocalContentId())) {
			newContributor.setAuthor(author);
			return true;
		}
		return false;
	}

	/**
	 * Return whether the diff associated with this input has changed.
	 * @return whether the diff associated with this input has changed
	 */
	@Override
	public boolean needsUpdate() {
		IDiff newNode= context.getDiffTree().getDiff(getResource());
		return newNode == null || !newNode.equals(node);
	}

	/**
	 * Return the local content id for this compare input.
	 * @return the local content id for this compare input
	 */
	public String getLocalContentId() {
		return Utils.getLocalContentId(node);
	}

	public boolean updateAuthorInfo(IProgressMonitor monitor) throws CoreException {
		boolean fireEvent= false;
		FileRevisionTypedElement ancestor= (FileRevisionTypedElement)getAncestor();
		FileRevisionTypedElement right= (FileRevisionTypedElement)getRight();
		LocalResourceTypedElement left= (LocalResourceTypedElement)getLeft();

		if (ancestor != null && ancestor.getAuthor() == null) {
			ancestor.fetchAuthor(monitor);
			fireEvent|= ancestor.getAuthor() != null;
		}

		fireEvent|= propogateAuthorIfSameRevision(ancestor, right);
		fireEvent|= propogateAuthorIfSameRevision(ancestor, left);

		if (right != null && right.getAuthor() == null) {
			right.fetchAuthor(monitor);
			fireEvent|= right.getAuthor() != null;
		}

		fireEvent|= propogateAuthorIfSameRevision(right, left);

		if (left != null && left.getAuthor() == null) {
			left.fetchAuthor(monitor);
			fireEvent|= fireEvent= left.getAuthor() != null;
		}

		return fireEvent;
	}

}

Back to the top