KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > ReplaceWithBaseVisitor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.util;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.team.internal.ccvs.core.*;
17 import org.eclipse.team.internal.ccvs.core.client.*;
18 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
19 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
20 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
21 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
22
23 public class ReplaceWithBaseVisitor implements ICVSResourceVisitor {
24
25     private IProgressMonitor monitor;
26     private int depth;
27     private Session session;
28     
29     /**
30      * @see ICVSResourceVisitor#visitFile(ICVSFile)
31      */

32     public void visitFile(final ICVSFile file) throws CVSException {
33         byte[] syncBytes = file.getSyncBytes();
34         if (syncBytes == null) {
35             // Delete unmanaged files if the user wants them deleted
36
if (CVSProviderPlugin.getPlugin().isReplaceUnmanaged()) {
37                 file.delete();
38             }
39         } else if (ResourceSyncInfo.isAddition(syncBytes)) {
40             file.delete();
41             file.unmanage(null);
42         } else {
43             byte[] tagBytes = ResourceSyncInfo.getTagBytes(syncBytes);
44             boolean isModified = file.isModified(null);
45             if (ResourceSyncInfo.isDeletion(syncBytes)) {
46                 // If deleted, null the sync info so the file will be refetched
47
syncBytes = ResourceSyncInfo.convertFromDeletion(syncBytes);
48                 file.setSyncBytes(syncBytes, ICVSFile.UNKNOWN);
49                 isModified = true;
50             }
51             // Fetch the file from the server
52
if (isModified) {
53                 ICVSFolder parent = file.getParent();
54                 FolderSyncInfo folderInfo = parent.getFolderSyncInfo();
55                 // Use the session opened in tghe replaceWithBase method to make the connection.
56
Command.UPDATE.execute(this.session, Command.NO_GLOBAL_OPTIONS,
57                     new LocalOption[] {Update.makeTagOption(CVSTag.BASE), Update.IGNORE_LOCAL_CHANGES},
58                     new ICVSResource[] { file }, null, Policy.subMonitorFor(monitor, 1));
59     
60                 // Set the tag to be the original tag
61
syncBytes = file.getSyncBytes();
62                 syncBytes = ResourceSyncInfo.setTag(syncBytes, tagBytes);
63                 file.setSyncBytes(syncBytes, ICVSFile.UNKNOWN);
64             }
65         }
66         monitor.worked(1);
67     }
68
69     /**
70      * @see ICVSResourceVisitor#visitFolder(ICVSFolder)
71      */

72     public void visitFolder(ICVSFolder folder) throws CVSException {
73         // Visit the children of the folder as appropriate
74
if (depth == IResource.DEPTH_INFINITE) {
75             folder.acceptChildren(this);
76         } else if (depth == IResource.DEPTH_ONE) {
77             ICVSResource[] files = folder.members(ICVSFolder.FILE_MEMBERS);
78             for (int i = 0; i < files.length; i++) {
79                 files[i].accept(this);
80             }
81         }
82         // Also delete ignored child files that start with .#
83
ICVSResource[] ignoredFiles = folder.members(ICVSFolder.FILE_MEMBERS | ICVSFolder.IGNORED_MEMBERS);
84         for (int i = 0; i < ignoredFiles.length; i++) {
85             ICVSResource cvsResource = ignoredFiles[i];
86             if (cvsResource.getName().startsWith(".#")) { //$NON-NLS-1$
87
cvsResource.delete();
88             }
89         }
90         monitor.worked(1);
91     }
92     
93     /*
94      * This method will replace any changed resources in the local workspace with the
95      * base resource. Although CVS allows this operation using "cvs update -r BASE" the
96      * results in the workspace are "sticky". This operation does not leave the local workspace "sticky".
97      *
98      * NOTE: This operation issues multiple commands over a single connection. It may fail
99      * with some servers that are configured to run scripts during an update (see bug 40145).
100      */

101     public void replaceWithBase(IProject project, final IResource[] resources, int depth, IProgressMonitor pm) throws CVSException {
102         this.depth = depth;
103         final ICVSFolder root = CVSWorkspaceRoot.getCVSFolderFor(project);
104         FolderSyncInfo folderInfo = root.getFolderSyncInfo();
105         IProgressMonitor monitor = Policy.monitorFor(pm);
106         monitor.beginTask(null, 100);
107         this.session = new Session(KnownRepositories.getInstance().getRepository(folderInfo.getRoot()), root, true /* creat e backups */);
108         this.session.open(Policy.subMonitorFor(monitor, 10), false /* read-only */);
109         try {
110             this.monitor = Policy.infiniteSubMonitorFor(monitor, 90);
111             this.monitor.beginTask(null, 512);
112             for (int i = 0; i < resources.length; i++) {
113                 this.monitor.subTask(Policy.bind("ReplaceWithBaseVisitor.replacing", resources[i].getFullPath().toString())); //$NON-NLS-1$
114
CVSWorkspaceRoot.getCVSResourceFor(resources[i]).accept(this);
115             }
116         } finally {
117             this.session.close();
118             monitor.done();
119         }
120     }
121 }
122
Popular Tags