Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb9d353895f93ac662d8d936b7f3d40bd5f2d337 (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
/*******************************************************************************
 * Copyright (c) 2009, 2017 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.ui.synchronize.patch;

import java.util.*;

import org.eclipse.compare.internal.core.patch.*;
import org.eclipse.compare.internal.patch.PatchProjectDiffNode;
import org.eclipse.compare.internal.patch.WorkspacePatcher;
import org.eclipse.compare.patch.IHunk;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.core.subscribers.SubscriberChangeEvent;
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.mapping.LocalResourceVariant;

public class ApplyPatchSubscriber extends Subscriber {

	private class ApplyPatchSyncInfo extends SyncInfo {
		private ApplyPatchSyncInfo(IResource local, IResourceVariant base,
				IResourceVariant remote, IResourceVariantComparator comparator) {
			super(local, base, remote, comparator);
		}

		@Override
		protected int calculateKind() throws TeamException {
			// TODO: this works only for files, see bug 300214
			if (!getPatcher().isEnabled(PatchModelProvider.getPatchObject(getLocal(), patcher)))
				return IN_SYNC;

			// same story here, one merged hunk is enough to consider the file as merged
			if (getRemote() != null) {
				FilePatch2 filePatch2 = ((PatchedFileVariant)getRemote()).getDiff();
				IHunk[] hunks = filePatch2.getHunks();
				for (int i = 0; i < hunks.length; i++) {
					if (patcher.isManuallyMerged((Hunk) hunks[i]))
						return IN_SYNC;
				}
			} else {
				// deletions don't have the remote variant, but still can be manually merged
				Object patchObject = PatchModelProvider.getPatchObject(getLocal(), patcher);
				if (patchObject instanceof FilePatch2) {
					FilePatch2 filePatch2 = (FilePatch2) patchObject;
					IHunk[] hunks = filePatch2.getHunks();
					for (int i = 0; i < hunks.length; i++) {
						if (patcher.isManuallyMerged((Hunk) hunks[i]))
							return IN_SYNC;
					}
				}
			}
			int kind = super.calculateKind();
			// mark diffs with problems as conflicts
			if (getRemote() != null
					&& getPatcher().getDiffResult(((PatchedFileVariant)getRemote()).getDiff()).containsProblems())
				kind |= CONFLICTING;
			return kind;
		}
	}

	private WorkspacePatcher patcher;
	private IResourceVariantComparator comparator;

	public ApplyPatchSubscriber(WorkspacePatcher patcher) {
		this.patcher = patcher;
		this.comparator = new PatchedFileVariantComparator();
		getPatcher().refresh();
	}

	@Override
	public String getName() {
		return "Apply Patch Subscriber"; //$NON-NLS-1$
	}

	@Override
	public IResourceVariantComparator getResourceComparator() {
		return comparator;
	}

	@Override
	public SyncInfo getSyncInfo(IResource resource) throws TeamException {
		if (!isSupervised(resource)) return null;
		// TODO: called too many times, optimize
		refresh(new IResource[] { resource }, IResource.DEPTH_ZERO, null);
		try {
			FilePatch2 diff = (FilePatch2) PatchModelProvider.getPatchObject(resource, getPatcher());
			// use null as remote variant for deletions
			IResourceVariant remote = null;
			if (diff.getDiffType(patcher.isReversed()) != FilePatch2.DELETION)
				remote =  new PatchedFileVariant(getPatcher(), diff);
			IResourceVariant base = null;
			if (diff.getDiffType(patcher.isReversed()) != FilePatch2.ADDITION)
				base = new LocalResourceVariant(resource);
			SyncInfo info = new ApplyPatchSyncInfo(resource, base, remote, getResourceComparator());
			info.init();
			return info;
		} catch (CoreException e) {
			throw TeamException.asTeamException(e);
		}
	}

	@Override
	public boolean isSupervised(IResource resource) throws TeamException {
		return resource.getType() == IResource.FILE
				&& PatchModelProvider.getPatchObject(resource, getPatcher()) != null;
	}

	@Override
	public IResource[] members(IResource resource) throws TeamException {
		//XXX: what if there is an addition in the patch that needs to add 3 subfolders?
		try {
			if(resource.getType() == IResource.FILE)
				// file has no IResource members
				return new IResource[0];
			IContainer container = (IContainer) resource;

			// workspace container members
			List<IResource> existingChildren = new ArrayList<>();

			if (container.isAccessible())
				existingChildren.addAll(Arrays.asList(container.members()));

			// patch members, subscriber location
			FilePatch2[] diffs = getPatcher().getDiffs();
			for (int i = 0; i < diffs.length; i++) {
				IResource file = PatchModelProvider.getFile(diffs[i], getPatcher());
				if (container.getFullPath().isPrefixOf(file.getFullPath())) {
					// XXX: check segments
					if (!container.exists(file.getProjectRelativePath())) {
						existingChildren.add(file);
					}
				}
			}
			return existingChildren.toArray(new IResource[existingChildren.size()]);
		} catch (CoreException e) {
			throw TeamException.asTeamException(e);
		}
	}

	@Override
	public void refresh(IResource[] resources, int depth,
			IProgressMonitor monitor) throws TeamException {
		Set<FilePatch2> diffs = new HashSet<>();
		for (int i = 0; i < resources.length; i++) {
			Object object = PatchModelProvider.getPatchObject(resources[i],
					getPatcher());
			if (object instanceof FilePatch2) {
				FilePatch2 filePatch = (FilePatch2) object;
				diffs.add(filePatch);
			}
		}
		getPatcher().refresh(diffs.toArray(new FilePatch2[0]));
	}

	@Override
	public IResource[] roots() {
		Set<IResource> roots = new HashSet<>();
		if (getPatcher().isWorkspacePatch()) {
			IDiffElement[] children = PatchModelProvider.getPatchWorkspace(this).getChildren();
			for (int i = 0; i < children.length; i++) {
				// return array of projects from the patch
				DiffProject diffProject = ((PatchProjectDiffNode)children[i]).getDiffProject();
				IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(diffProject.getName());
				if (project.isAccessible())
					roots.add(project);
			}
		} else {
			roots.add(getPatcher().getTarget());
		}
		return roots.toArray(new IResource[0]);
	}

	public WorkspacePatcher getPatcher() {
		return patcher;
	}

	public void merged(IResource[] resources) {
		fireTeamResourceChange(SubscriberChangeEvent.asSyncChangedDeltas(this, resources));
	}
}

Back to the top