Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8fb53734ab8273fcb141417385bdd7f89e030439 (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
/*******************************************************************************
 * Copyright (c) 2000, 2010 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
 *     Matt McCutchen <hashproduct+eclipse@gmail.com> - Bug 179174 CVS client sets timestamps back when replacing
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.operations;

import java.util.*;

import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
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.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.core.util.PrepareForReplaceVisitor;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ccvs.ui.tags.TagSource;
import org.eclipse.team.internal.ccvs.ui.tags.TagSourceWorkbenchAdapter;
import org.eclipse.ui.IWorkbenchPart;

/**
 * This operation replaces the local resources with their remote contents
 */
public class ReplaceOperation extends UpdateOperation {

	public ReplaceOperation(IWorkbenchPart part, IResource[] resources, CVSTag tag, boolean recurse) {
		super(part, asResourceMappers(resources, recurse ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE), new LocalOption[] { Update.IGNORE_LOCAL_CHANGES }, tag);
	}

	public ReplaceOperation(IWorkbenchPart part, ResourceMapping[] mappings, CVSTag tag) {
        super(part, mappings, new LocalOption[] { Update.IGNORE_LOCAL_CHANGES }, tag);
    }

	@Override
	protected String getTaskName() {
		return CVSUIMessages.ReplaceOperation_taskName; 
	}

	@Override
	protected IStatus executeCommand(
		final Session session,
		final CVSTeamProvider provider,
		final ICVSResource[] resources,
		final boolean recurse, IProgressMonitor monitor)
		throws CVSException, InterruptedException {
		
        final IStatus[] status = new IStatus[] { Status.OK_STATUS };
        try {
			ResourcesPlugin.getWorkspace().run((IWorkspaceRunnable) monitor1 -> {
				try {
					status[0] = internalExecuteCommand(session, provider, resources, recurse, monitor1);
				} catch (InterruptedException e) {
					throw new OperationCanceledException();
				}
			}, null, IWorkspace.AVOID_UPDATE, monitor);
        } catch (CoreException e) {
            throw CVSException.wrapException(e);
        }
		return status[0];
	}

	// Files deleted by the PrepareForReplaceVisitor.
	private Set/*<ICVSFile>*/ prepDeletedFiles;

	/**
	 * Tells whether resources for which the given tag does not exists, are ignored by the replace
	 * operation.
	 */
	private boolean ignoreResourcesIfTagDoesNotExist;


    private IStatus internalExecuteCommand(Session session, CVSTeamProvider provider, ICVSResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException {
        monitor.beginTask(null, 100);
        ICVSResource[] managedResources = getResourcesToUpdate(resources, Policy.subMonitorFor(monitor, 5));
		if (ignoreResourcesIfTagDoesNotExist && managedResources.length == 0)
			return OK;

        try {
        	// Purge any unmanaged or added files
        	PrepareForReplaceVisitor pfrv = new PrepareForReplaceVisitor(session, getTag());
        	pfrv.visitResources(
        		provider.getProject(), 
        		resources, 
        		CVSUIMessages.ReplaceOperation_1, 
        		recurse ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE, 
        		Policy.subMonitorFor(monitor, 25));
        	prepDeletedFiles = pfrv.getDeletedFiles();
        	
        	// Only perform the remote command if some of the resources being replaced were managed
        	IStatus status = OK;
        	if (managedResources.length > 0) {
        		// Perform an update, ignoring any local file modifications
        		status = super.executeCommand(session, provider, managedResources, recurse, Policy.subMonitorFor(monitor, 70));
        	}
        	
        	// Prune any empty folders left after the resources were purged.
        	// This is done to prune any empty folders that contained only unmanaged resources
        	if (status.isOK() && CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()) {
        		new PruneFolderVisitor().visit(session, resources);
        	}
        	
        	return status;
        } finally {
        	monitor.done();
        }
    }

	/**
	 * Return the resources that need to be updated from the server.
	 * By default, this is all resources that are managed.
	 * @param resources all resources being replaced
	 * @return resources that are to be updated from the server
	 * @throws CVSException
	 */
	protected ICVSResource[] getResourcesToUpdate(ICVSResource[] resources, IProgressMonitor monitor) throws CVSException {
		// Accumulate the managed resources from the list of provided resources
		List<ICVSResource> managedResources = new ArrayList<>();
		monitor.beginTask(null, resources.length * 100);
		for (int i = 0; i < resources.length; i++) {
			ICVSResource resource = resources[i];
			if ((resource.isFolder() && ((ICVSFolder)resource).isCVSFolder())) {
				addResourceIfTagExists(managedResources, resource, Policy.subMonitorFor(monitor, 100));
			} else if (!resource.isFolder()){
				byte[] syncBytes = ((ICVSFile)resource).getSyncBytes();
				if (syncBytes != null && !ResourceSyncInfo.isAddition(syncBytes)) {
					addResourceIfTagExists(managedResources, resource, Policy.subMonitorFor(monitor, 100));
				}
			}
		}
		monitor.done();
		return managedResources.toArray(new ICVSResource[managedResources.size()]);
	}

	private void addResourceIfTagExists(List<ICVSResource> managedResources, ICVSResource resource,
			IProgressMonitor monitor) {
		CVSTag tag = getTag();
		if (tag == null || tag.getType() == CVSTag.DATE || tag.isHeadTag() || tag.isBaseTag()) {
			// No need to check for date, head or base tags
			managedResources.add(resource);
		} else {
			TagSource tagSource= TagSource.create(new ICVSResource[] { resource });
			if (isTagPresent(tagSource, tag)) {
				managedResources.add(resource);
			} else {
				// If the tag usn't present, lets refresh just to make sure
				try {
					tagSource.refresh(false, monitor);
					if (isTagPresent(tagSource, tag)) {
						managedResources.add(resource);
					} else {
						tagSource.refresh(true, monitor);
						if (isTagPresent(tagSource, tag)) {
							managedResources.add(resource);
						}
					}
				} catch (TeamException e) {
					CVSUIPlugin.log(e);
				}
			}
		}
	}
	
	private boolean isTagPresent(TagSource tagSource, CVSTag tag) {
		CVSTag[] tags= tagSource.getTags(TagSource.convertIncludeFlaqsToTagTypes(TagSourceWorkbenchAdapter.INCLUDE_ALL_TAGS));
		return Arrays.asList(tags).contains(tag);
	}
	
	@Override
	protected Update getUpdateCommand() {
		// Use a special replace command that doesn't set back the timestamps
		// of files in the passed set if it recreates them.
		return new Replace(prepDeletedFiles);
	}

	@Override
	protected String getTaskName(CVSTeamProvider provider) {
		return NLS.bind(CVSUIMessages.ReplaceOperation_0, new String[] { provider.getProject().getName() }); 
	}

	/**
	 * Tells to ignore resources for which the given tag does not exists.
	 */
	public void ignoreResourcesWithMissingTag() {
		ignoreResourcesIfTagDoesNotExist= true;
	}
}

Back to the top