Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4bb5b9b37d2583ae8f1104136ef133ca808d73e9 (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
387
388
389
390
391
392
393
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.operations;

import java.util.*;

import org.eclipse.compare.CompareUI;
import org.eclipse.core.runtime.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.client.*;
import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
import org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener;
import org.eclipse.team.internal.ccvs.core.resources.*;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.core.util.Assert;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Compare the two versions of given remote folders obtained from the two tags specified.
 */
public class RemoteCompareOperation extends RemoteOperation {
    
    private CompareTreeBuilder builder;
    private CVSTag left, right;
	
    /**
     * Helper class for builder and comparing the resource trees
     */
	public static class CompareTreeBuilder implements RDiffSummaryListener.IFileDiffListener {
	    private ICVSRepositoryLocation location;
		private RemoteFolderTree leftTree, rightTree;
		private CVSTag left, right;

        public CompareTreeBuilder(ICVSRepositoryLocation location, CVSTag left, CVSTag right) {
            this.left = left;
            this.right = right;
            this.location = location;
            reset();
        }
		
        public RemoteFolderTree getLeftTree() {
            return leftTree;
        }
        public RemoteFolderTree getRightTree() {
            return rightTree;
        }
        
        /**
         * Reset the builder to prepare for a new build
         */
        public void reset() {
            leftTree = new RemoteFolderTree(null, location, ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, left);
    		leftTree.setChildren(new ICVSRemoteResource[0]);
    		rightTree = new RemoteFolderTree(null, location, ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, right);
    		rightTree.setChildren(new ICVSRemoteResource[0]);
        }
        
        /**
         * Cache the contents for the files that are about to be compares
         * @throws CVSException
         */
        public void cacheContents(IProgressMonitor monitor) throws CVSException {
			String[] overlappingFilePaths = getOverlappingFilePaths();
			if (overlappingFilePaths.length > 0) {
			    monitor.beginTask(null, 100);
				fetchFileContents(leftTree, overlappingFilePaths, Policy.subMonitorFor(monitor, 50));
				fetchFileContents(rightTree, overlappingFilePaths, Policy.subMonitorFor(monitor, 50));
				monitor.done();
			}
        }
        
	    /**
         * Open the comparison in a compare editor
         */
        public void openCompareEditor(final IWorkbenchPage page, final String title, final String toolTip) {
			if (leftTree == null || rightTree == null) return;
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					CompareUI.openCompareEditorOnPage(
						new CVSCompareEditorInput(title, toolTip, new ResourceEditionNode(leftTree), new ResourceEditionNode(rightTree)), page);
				}
			});
        }
		
        /**
         * Add the predecessor to the left tree and the remote to the right tree.
         * @param predecessor
         * @param remote
         */
        public void addToTrees(ICVSRemoteFile predecessor, ICVSRemoteFile remote) {
            if (remote != null) {
				try {
					Path filePath = new Path(null, remote.getRepositoryRelativePath());
                    addFile(rightTree, right, filePath, remote.getRevision());
					getFolder(leftTree, left, filePath.removeLastSegments(1), Path.EMPTY);
				} catch (TeamException e) {
					CVSUIPlugin.log(e);
				}
            }
            if (predecessor != null) {
				try {
					Path filePath = new Path(null, predecessor.getRepositoryRelativePath());
                    addFile(leftTree, left, filePath, predecessor.getRevision());
					getFolder(rightTree, right, filePath.removeLastSegments(1), Path.EMPTY);
				} catch (TeamException e) {
					CVSUIPlugin.log(e);
				}
            }
        }
        
		private void addFile(RemoteFolderTree tree, CVSTag tag, Path filePath, String revision) throws CVSException {
			RemoteFolderTree parent = (RemoteFolderTree)getFolder(tree, tag, filePath.removeLastSegments(1), Path.EMPTY);
			String name = filePath.lastSegment();
			ICVSRemoteFile file = new RemoteFile(parent, 0, name, revision, null, getTag(revision, tag));
			addChild(parent, file);
		}
		
        private CVSTag getTag(String revision, CVSTag tag) {
            if (tag == null) {
                tag = new CVSTag(revision, CVSTag.VERSION);
            }
            return tag;
        }

        /* 
		 * Get the folder at the given path in the given tree, creating any missing folders as needed.
		 */
		private ICVSRemoteFolder getFolder(RemoteFolderTree tree, CVSTag tag, IPath remoteFolderPath, IPath parentPath) throws CVSException {
			if (remoteFolderPath.segmentCount() == 0) return tree;
			String name = remoteFolderPath.segment(0);
			ICVSResource child;
			IPath childPath = parentPath.append(name);
			if (tree.childExists(name)) {
				child = tree.getChild(name);
			}  else {
				child = new RemoteFolderTree(tree, tree.getRepository(), childPath.toString(), tag);
				((RemoteFolderTree)child).setChildren(new ICVSRemoteResource[0]);
				addChild(tree, (ICVSRemoteResource)child);
			}
			return getFolder((RemoteFolderTree)child, tag, remoteFolderPath.removeFirstSegments(1), childPath);
		}

		private void addChild(RemoteFolderTree tree, ICVSRemoteResource resource) {
			ICVSRemoteResource[] children = tree.getChildren();
			ICVSRemoteResource[] newChildren;
			if (children == null) {
				newChildren = new ICVSRemoteResource[] { resource };
			} else {
				newChildren = new ICVSRemoteResource[children.length + 1];
				System.arraycopy(children, 0, newChildren, 0, children.length);
				newChildren[children.length] = resource;
			}
			tree.setChildren(newChildren);
		}
        
		/* (non-Javadoc)
		 * @see org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener.IFileDiffListener#fileDiff(java.lang.String, java.lang.String, java.lang.String)
		 */
		public void fileDiff(String remoteFilePath, String leftRevision, String rightRevision) {
			try {
				addFile(rightTree, right, new Path(null, remoteFilePath), rightRevision);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
			try {
				addFile(leftTree, left, new Path(null, remoteFilePath), leftRevision);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener.IFileDiffListener#newFile(java.lang.String, java.lang.String)
		 */
		public void newFile(String remoteFilePath, String rightRevision) {
			try {
				addFile(rightTree, right, new Path(null, remoteFilePath), rightRevision);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener.IFileDiffListener#deletedFile(java.lang.String)
		 */
		public void deletedFile(String remoteFilePath, String leftRevision) {
			// The leftRevision may be null in which case the tag is used
			try {
				addFile(leftTree, left, new Path(null, remoteFilePath), leftRevision);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener.IFileDiffListener#directory(java.lang.String)
		 */
		public void directory(String remoteFolderPath) {
			try {
				getFolder(leftTree, left, new Path(null, remoteFolderPath), Path.EMPTY);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
			try {
				getFolder(rightTree, right, new Path(null, remoteFolderPath), Path.EMPTY);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
		}
		
		private String[] getOverlappingFilePaths() {
			String[] leftFiles = getFilePaths(leftTree);
			String[] rightFiles = getFilePaths(rightTree);
			Set set = new HashSet();
			for (int i = 0; i < rightFiles.length; i++) {
				String rightFile = rightFiles[i];
				for (int j = 0; j < leftFiles.length; j++) {
					String leftFile = leftFiles[j];
					if (leftFile.equals(rightFile)) {
						set.add(leftFile);
					}
				}
			}
			return (String[]) set.toArray(new String[set.size()]);
		}

		private void fetchFileContents(RemoteFolderTree tree, String[] overlappingFilePaths, IProgressMonitor monitor) throws CVSException {
			FileContentCachingService.fetchFileContents(tree, overlappingFilePaths, monitor);
		}

		private String[] getFilePaths(RemoteFolderTree tree) {
			ICVSRemoteResource[] children = tree.getChildren();
			List result = new ArrayList();
			for (int i = 0; i < children.length; i++) {
				ICVSRemoteResource resource = children[i];
				if (resource.isContainer()) {
					result.addAll(Arrays.asList(getFilePaths((RemoteFolderTree)resource)));
				} else {
					result.add(resource.getRepositoryRelativePath());
				}
			}
			return (String[]) result.toArray(new String[result.size()]);
		}
	}
	
	public static CVSTag getTag(ICVSRemoteResource resource) throws CVSException {
		CVSTag tag = null;
		try {
			if (resource.isContainer()) {
				tag = ((ICVSRemoteFolder)resource).getTag();
			} else {
				ICVSRemoteFile file = (ICVSRemoteFile)resource;
				String revision = file.getRevision();
				if (revision.equals(ResourceSyncInfo.ADDED_REVISION)) {
					ResourceSyncInfo info =file.getSyncInfo();
					if (info != null) tag = info.getTag();
				} else {
					tag = new CVSTag(revision, CVSTag.VERSION);
				}
			}
		} catch (TeamException e) {
			throw CVSException.wrapException(e);
		}
		if (tag == null) tag = CVSTag.DEFAULT;
		return tag;
	}
	
	public static RemoteCompareOperation create(IWorkbenchPart part, ICVSRemoteResource remoteResource, CVSTag tag) throws CVSException {
		CVSTag tag0 = getTag(remoteResource);
		CVSTag tag1 = tag;
		if (tag0.getType() == CVSTag.DATE && tag1.getType() == CVSTag.DATE) {
			if (tag0.asDate().after(tag1.asDate())) {
				tag = tag0;
				remoteResource = remoteResource.forTag(tag1);
			}
		}
		return new RemoteCompareOperation(part, remoteResource, tag);
	}
	
	/**
	 * Compare two versions of the given remote resource.
	 * @param shell
	 * @param remoteResource the resource whose tags are being compared
	 * @param left the earlier tag (not null)
	 * @param right the later tag (not null)
	 */
	protected RemoteCompareOperation(IWorkbenchPart part, ICVSRemoteResource remoteResource, CVSTag tag) {
		super(part, new ICVSRemoteResource[] {remoteResource});
		Assert.isNotNull(tag);
		this.right = tag;
		try {
			this.left = getTag(remoteResource);
		} catch (CVSException e) {
			// This shouldn't happen but log it just in case
			CVSProviderPlugin.log(e);
		}
		if (this.left == null) {
			this.left = CVSTag.DEFAULT;
		}
		builder = new CompareTreeBuilder(remoteResource.getRepository(), left, right);
	}

	/*
	 * This command only supports the use of a single resource
	 */
	private ICVSRemoteResource getRemoteResource() {
		return getRemoteResources()[0];
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected void execute(IProgressMonitor monitor) throws CVSException {
		boolean fetchContents = CVSUIPlugin.getPlugin().getPluginPreferences().getBoolean(ICVSUIConstants.PREF_CONSIDER_CONTENTS);
		monitor.beginTask(getTaskName(), 50 + (fetchContents ? 100 : 0));
		try {
			ICVSRemoteResource resource = getRemoteResource();
			IStatus status = buildTrees(resource, Policy.subMonitorFor(monitor, 50));
			if (status.isOK() && fetchContents) {
			    builder.cacheContents(Policy.subMonitorFor(monitor, 100));
			}
			collectStatus(status);
			openCompareEditor(builder);
		} finally {
			monitor.done();
		}
	}

	/**
     * This method is here to allow subclasses to override
     */
    protected void openCompareEditor(CompareTreeBuilder builder) {
        builder.openCompareEditor(getTargetPage(), null, null);
    }

    /*
	 * Build the two trees uses the reponses from "cvs rdiff -s ...".
	 */
	private IStatus buildTrees(ICVSRemoteResource resource, IProgressMonitor monitor) throws CVSException {
		// Initialize the resulting trees
	    builder.reset();
		Command.QuietOption oldOption= CVSProviderPlugin.getPlugin().getQuietness();
		Session session = new Session(resource.getRepository(), builder.getLeftTree(), false);
		try {
			monitor.beginTask(getTaskName(), 100);
			CVSProviderPlugin.getPlugin().setQuietness(Command.VERBOSE);
			session.open(Policy.subMonitorFor(monitor, 10));
			IStatus status = Command.RDIFF.execute(session,
					Command.NO_GLOBAL_OPTIONS,
					getLocalOptions(),
					new ICVSResource[] { resource },
					new RDiffSummaryListener(builder),
					Policy.subMonitorFor(monitor, 90));
			return status;
		} finally {
			try {
				session.close();
			} finally {
				CVSProviderPlugin.getPlugin().setQuietness(oldOption);
			}
			monitor.done();
		}
	}

    private LocalOption[] getLocalOptions() {
		return new LocalOption[] {RDiff.SUMMARY, RDiff.makeTagOption(left), RDiff.makeTagOption(right)};
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
	 */
	protected String getTaskName() {
		return Policy.bind("RemoteCompareOperation.0", new Object[] {left.getName(), right.getName(), getRemoteResource().getRepositoryRelativePath()}); //$NON-NLS-1$
	}
	
	protected IWorkbenchPage getTargetPage() {
		return TeamUIPlugin.getActivePage();
	}
}

Back to the top