Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7bd3c76b9e5d5ae2e7f45105414c2f0555490a6b (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 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 org.eclipse.compare.internal.core.patch.FilePatch2;
import org.eclipse.compare.internal.core.patch.HunkResult;
import org.eclipse.compare.internal.patch.WorkspacePatcher;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.diff.IThreeWayDiff;
import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.core.subscribers.SubscriberMergeContext;

public class ApplyPatchSubscriberMergeContext extends SubscriberMergeContext {

	protected ApplyPatchSubscriberMergeContext(Subscriber subscriber,
			ISynchronizationScopeManager manager) {
		super(subscriber, manager);
	}

	public static ApplyPatchSubscriberMergeContext createContext(
			Subscriber subscriber, ISynchronizationScopeManager manager) {
		ApplyPatchSubscriberMergeContext mergeContext = new ApplyPatchSubscriberMergeContext(
				subscriber, manager);
		// Initialize using the ApplyPatchSubscriber to populate the diff tree.
		mergeContext.initialize();
		return mergeContext;
	}

	@Override
	protected void makeInSync(IDiff diff, IProgressMonitor monitor)
			throws CoreException {
		markAsMerged(diff, true, monitor);
	}

	@Override
	public void markAsMerged(IDiff node, boolean inSyncHint,
			IProgressMonitor monitor) throws CoreException {
		IResource resource = getDiffTree().getResource(node);
		WorkspacePatcher patcher = ((ApplyPatchSubscriber) getSubscriber()).getPatcher();
		Object object = PatchModelProvider.getPatchObject(resource, patcher);
		if (object instanceof FilePatch2) {
			HunkResult[] hunkResults = patcher.getDiffResult((FilePatch2) object).getHunkResults();
			for (int i = 0; i < hunkResults.length; i++) {
				if (inSyncHint) {
					// clean Merge > disable hunks that have merged
					if (hunkResults[i].isOK())
						patcher.setEnabled(hunkResults[i].getHunk(), false);
				} else {
					// Mark as Merged > mark *all* hunks from the file as manually merged
					patcher.setManuallyMerged(hunkResults[i].getHunk(), true);
				}
			}
		} else {
			patcher.setEnabled(object, false);
			// TODO: mark as merged
		}
		// fire a team resource change event
		((ApplyPatchSubscriber)getSubscriber()).merged(new IResource[] { resource});
		// don't need to worry about the node no more... it is in sync now
		// see ApplyPatchSubscriber.ApplyPatchSyncInfo.calculateKind()
	}

	@Override
	protected IStatus performThreeWayMerge(IThreeWayDiff diff,
			IProgressMonitor monitor) throws CoreException {
		IStatus status = super.performThreeWayMerge(diff, monitor);
		if (status.isOK()) {
			// Merge with conflicts > all hunks from the diff have been marked
			// as manually merged...
			IResource resource = getDiffTree().getResource(diff);
			WorkspacePatcher patcher = ((ApplyPatchSubscriber) getSubscriber()).getPatcher();
			Object object = PatchModelProvider.getPatchObject(resource, patcher);
			if (object instanceof FilePatch2) {
				HunkResult[] hunkResults = patcher.getDiffResult((FilePatch2) object).getHunkResults();
				for (int i = 0; i < hunkResults.length; i++) {
					// ... unmark them and exclude those properly merged
					if (patcher.isManuallyMerged(hunkResults[i].getHunk())) {
						patcher.setManuallyMerged(hunkResults[i].getHunk(), false);
						if (hunkResults[i].isOK()) {
							patcher.setEnabled(hunkResults[i].getHunk(), false);
						}
					}
				}
			}
		}
		return status;
	}

	@Override
	public void reject(IDiff diff, IProgressMonitor monitor)
			throws CoreException {
		// do nothing
	}
}

Back to the top