Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b8f2a6f98821c5203a6678ed2e7f90069f30db3 (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
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core.mapping;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.diff.*;
import org.eclipse.team.core.diff.provider.Diff;
import org.eclipse.team.core.diff.provider.ThreeWayDiff;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.mapping.IResourceDiff;
import org.eclipse.team.core.mapping.provider.ResourceDiff;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.core.variants.IResourceVariantComparator;
import org.eclipse.team.internal.core.Messages;
import org.eclipse.team.internal.core.history.LocalFileRevision;

/**
 * Covert a SyncInfo into a IDiff
 */
public class SyncInfoToDiffConverter {

	private static class PrecalculatedSyncInfo extends SyncInfo {
		public int kind;
		public PrecalculatedSyncInfo(int kind, IResource local, IResourceVariant base, IResourceVariant remote, IResourceVariantComparator comparator) {
			super(local, base, remote, comparator);
			this.kind = kind;
		}

		protected int calculateKind() throws TeamException {
			return kind;
		}
	}

	private static SyncInfoToDiffConverter instance;


	public static String diffKindToString(int kind) {
		String label = ""; //$NON-NLS-1$
		if(kind==IDiff.NO_CHANGE) {
			label = Messages.RemoteSyncElement_insync;
		} else {
			switch(kind) {
				case IDiff.CHANGE: label = Messages.RemoteSyncElement_change ; break;
				case IDiff.ADD: label = Messages.RemoteSyncElement_addition; break;
				case IDiff.REMOVE: label = Messages.RemoteSyncElement_deletion; break;
			}
		}
		return label;
	}

	public static String diffDirectionToString(int direction) {
		switch(direction) {
			case IThreeWayDiff.CONFLICTING: return Messages.RemoteSyncElement_conflicting;
			case IThreeWayDiff.OUTGOING: return Messages.RemoteSyncElement_outgoing;
			case IThreeWayDiff.INCOMING: return Messages.RemoteSyncElement_incoming;
		}
		return ""; //$NON-NLS-1$
	}

	public static String diffStatusToString(int status) {
		int kind = status & Diff.KIND_MASK;
		String label = diffKindToString(kind);
		int direction = status & ThreeWayDiff.DIRECTION_MASK;
		if (direction != 0)
			label = NLS.bind(Messages.concatStrings, new String[] { diffDirectionToString(direction), label });
		return label;
	}

	public static int asDiffFlags(int syncInfoFlags) {
		if (syncInfoFlags == SyncInfo.IN_SYNC)
			return IDiff.NO_CHANGE;
		int kind = SyncInfo.getChange(syncInfoFlags);
		int diffFlags = 0;
		switch (kind) {
		case SyncInfo.ADDITION:
			diffFlags = IDiff.ADD;
			break;
		case SyncInfo.DELETION:
			diffFlags = IDiff.REMOVE;
			break;
		case SyncInfo.CHANGE:
			diffFlags = IDiff.CHANGE;
			break;
		}
		int direction = SyncInfo.getDirection(syncInfoFlags);
		switch (direction) {
		case SyncInfo.INCOMING:
			diffFlags |= IThreeWayDiff.INCOMING;
			break;
		case SyncInfo.OUTGOING:
			diffFlags |= IThreeWayDiff.OUTGOING;
			break;
		case SyncInfo.CONFLICTING:
			diffFlags |= IThreeWayDiff.CONFLICTING;
			break;
		}
		return diffFlags;
	}

	private static int asSyncInfoKind(IThreeWayDiff diff) {
		int kind = diff.getKind();
		if (diff.getKind() == IDiff.NO_CHANGE)
			return SyncInfo.IN_SYNC;
		int syncKind = 0;
		switch (kind) {
		case IDiff.ADD:
			syncKind = SyncInfo.ADDITION;
			break;
		case IDiff.REMOVE:
			syncKind = SyncInfo.DELETION;
			break;
		case IDiff.CHANGE:
			syncKind = SyncInfo.CHANGE;
			break;
		}
		int direction = diff.getDirection();
		switch (direction) {
		case IThreeWayDiff.INCOMING:
			syncKind |= SyncInfo.INCOMING;
			break;
		case IThreeWayDiff.OUTGOING:
			syncKind |= SyncInfo.OUTGOING;
			break;
		case IThreeWayDiff.CONFLICTING:
			syncKind |= SyncInfo.CONFLICTING;
			break;
		}
		return syncKind;
	}

	public IDiff getDeltaFor(SyncInfo info) {
		if (info.getComparator().isThreeWay()) {
			ITwoWayDiff local = getLocalDelta(info);
			ITwoWayDiff remote = getRemoteDelta(info);
			return new ThreeWayDiff(local, remote);
		} else {
			if (info.getKind() != SyncInfo.IN_SYNC) {
				IResourceVariant remote = info.getRemote();
				IResource local = info.getLocal();
				int kind;
				if (remote == null) {
					kind = IDiff.REMOVE;
				} else if (!local.exists()) {
					kind = IDiff.ADD;
				} else {
					kind = IDiff.CHANGE;
				}
				if (local.getType() == IResource.FILE) {
					IFileRevision after = asFileState(remote);
					IFileRevision before = getFileRevisionFor((IFile)local);
					return new ResourceDiff(info.getLocal(), kind, 0, before, after);
				}
				// For folders, we don't need file states
				return new ResourceDiff(info.getLocal(), kind);
			}
			return null;
		}
	}

	private ITwoWayDiff getRemoteDelta(SyncInfo info) {
		int direction = SyncInfo.getDirection(info.getKind());
		if (direction == SyncInfo.INCOMING || direction == SyncInfo.CONFLICTING) {
			IResourceVariant ancestor = info.getBase();
			IResourceVariant remote = info.getRemote();
			int kind;
			if (ancestor == null) {
				kind = IDiff.ADD;
			} else if (remote == null) {
				kind = IDiff.REMOVE;
			} else {
				kind = IDiff.CHANGE;
			}
			// For folders, we don't need file states
			if (info.getLocal().getType() == IResource.FILE) {
				IFileRevision before = asFileState(ancestor);
				IFileRevision after = asFileState(remote);
				return new ResourceDiff(info.getLocal(), kind, 0, before, after);
			}

			return new ResourceDiff(info.getLocal(), kind);
		}
		return null;
	}

	private IFileRevision asFileState(final IResourceVariant variant) {
		if (variant == null)
			return null;
		return asFileRevision(variant);
	}

	private IFileRevision getFileRevisionFor(final IFile file) {
		return new LocalFileRevision(file);
	}

	protected ResourceVariantFileRevision asFileRevision(final IResourceVariant variant) {
		return new ResourceVariantFileRevision(variant);
	}

	private ITwoWayDiff getLocalDelta(SyncInfo info) {
		int direction = SyncInfo.getDirection(info.getKind());
		if (direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING) {
			IResourceVariant ancestor = info.getBase();
			IResource local = info.getLocal();
			int kind;
			if (ancestor == null) {
				kind = IDiff.ADD;
			} else if (!local.exists()) {
				kind = IDiff.REMOVE;
			} else {
				kind = IDiff.CHANGE;
			}
			if (local.getType() == IResource.FILE) {
				IFileRevision before = asFileState(ancestor);
				IFileRevision after = getFileRevisionFor((IFile)local);
				return new ResourceDiff(info.getLocal(), kind, 0, before, after);
			}
			// For folders, we don't need file states
			return new ResourceDiff(info.getLocal(), kind);

		}
		return null;
	}

	public static IResourceVariant getRemoteVariant(IThreeWayDiff twd) {
		IFileRevision revision = getRemote(twd);
		if (revision != null)
			return asResourceVariant(revision);
		return null;
	}

	public static IResourceVariant getBaseVariant(IThreeWayDiff twd) {
		IResourceDiff diff = (IResourceDiff)twd.getRemoteChange();
		if (diff != null)
			return asResourceVariant(diff.getBeforeState());
		diff = (IResourceDiff)twd.getLocalChange();
		if (diff != null)
			return asResourceVariant(diff.getBeforeState());
		return null;
	}

	public SyncInfo asSyncInfo(IDiff diff, IResourceVariantComparator comparator) {
		if (diff instanceof ResourceDiff) {
			ResourceDiff rd = (ResourceDiff) diff;
			IResource local = rd.getResource();
			IFileRevision afterState = rd.getAfterState();
			IResourceVariant remote = asResourceVariant(afterState);
			int kind;
			if (remote == null) {
				kind = SyncInfo.DELETION;
			} else if (!local.exists()) {
				kind = SyncInfo.ADDITION;
			} else {
				kind = SyncInfo.CHANGE;
			}
			SyncInfo info = createSyncInfo(comparator, kind, local, null, remote);
			return info;
		} else if (diff instanceof IThreeWayDiff) {
			IThreeWayDiff twd = (IThreeWayDiff) diff;
			IResource local = getLocal(twd);
			if (local != null) {
				IResourceVariant remote = getRemoteVariant(twd);
				IResourceVariant base = getBaseVariant(twd);
				int kind = asSyncInfoKind(twd);
				SyncInfo info = createSyncInfo(comparator, kind, local, base, remote);
				return info;
			}
		}
		return null;
	}

	protected SyncInfo createSyncInfo(IResourceVariantComparator comparator, int kind, IResource local, IResourceVariant base, IResourceVariant remote) {
		PrecalculatedSyncInfo info = new PrecalculatedSyncInfo(kind, local, base, remote, comparator);
		try {
			info.init();
		} catch (TeamException e) {
			// Ignore
		}
		return info;
	}

	private static IResource getLocal(IThreeWayDiff twd) {
		IResourceDiff diff = (IResourceDiff)twd.getRemoteChange();
		if (diff != null)
			return diff.getResource();
		diff = (IResourceDiff)twd.getLocalChange();
		if (diff != null)
			return diff.getResource();
		return null;
	}

	public static IResourceVariant asResourceVariant(IFileRevision revision) {
		if (revision == null)
			return null;
		if (revision instanceof ResourceVariantFileRevision) {
			ResourceVariantFileRevision rvfr = (ResourceVariantFileRevision) revision;
			return rvfr.getVariant();
		}
		if (revision instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable) revision;
			Object o = adaptable.getAdapter(IResourceVariant.class);
			if (o instanceof IResourceVariant) {
				return (IResourceVariant) o;
			}
		}
		return null;
	}

	public static IFileRevision getRemote(IDiff diff) {
		if (diff instanceof IResourceDiff) {
			IResourceDiff rd = (IResourceDiff) diff;
			return rd.getAfterState();
		}
		if (diff instanceof IThreeWayDiff) {
			IThreeWayDiff twd = (IThreeWayDiff) diff;
			return getRemote(twd);
		}
		return null;
	}

	public static IFileRevision getRemote(IThreeWayDiff twd) {
		IResourceDiff rd = (IResourceDiff)twd.getRemoteChange();
		if (rd != null)
			return rd.getAfterState();
		rd = (IResourceDiff)twd.getLocalChange();
		if (rd != null)
			return rd.getBeforeState();
		return null;
	}

	public static SyncInfoToDiffConverter getDefault() {
		if (instance == null)
			instance = new SyncInfoToDiffConverter();
		return instance;
	}
}

Back to the top