Skip to main content
summaryrefslogtreecommitdiffstats
blob: 02e4485766e62ea5c0e367c5fa333fd34899762e (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 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.core.client.listeners;

import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSStatus;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.client.CommandOutputListener;

/**
 * This class interprets the output of "cvs diff --brief ..." in order to get the revisions
 * of two compared versions of a project or folder.
 */
public class CompareDiffListener extends CommandOutputListener {
	
	private static ServerMessageLineMatcher LOCAL_FILE_MATCHER; 
	private static ServerMessageLineMatcher REMOTE_FILE_MATCHER;
	private static ServerMessageLineMatcher REVISION_LINE_MATCHER;
	
	static {
		try {
			LOCAL_FILE_MATCHER = new ServerMessageLineMatcher(
				"Index: (localFile:.*:localFile)", new String[] {"localFile"});
			REMOTE_FILE_MATCHER = new ServerMessageLineMatcher(
				"RCS file: (remoteFile:.*:remoteFile),v", new String[] {"remoteFile"});
			REVISION_LINE_MATCHER = new ServerMessageLineMatcher(
				"diff .* -r(leftRevision:.*:leftRevision) -r(rightRevision:.*:rightRevision)", new String[] {"leftRevision", "rightRevision"});
		} catch (CVSException e) {
			// This is serious as the listener will not function properly
			CVSProviderPlugin.log(e);
			LOCAL_FILE_MATCHER = null;
			REMOTE_FILE_MATCHER = null;
			REVISION_LINE_MATCHER = null;
		}
	}
	
	private String localFilePath, remoteFilePath, leftRevision, rightRevision;
	
	private IFileDiffListener listener;
	
	public interface IFileDiffListener {
		public void fileDiff(
				String localFilePath,
				String remoteFilePath,
				String leftRevision,
				String rightRevision);
	}
	
	public CompareDiffListener(IFileDiffListener listener) {
		this.listener = listener;
	}
	
	public IStatus messageLine(
			String line, 
			ICVSRepositoryLocation location, 
			ICVSFolder commandRoot,
			IProgressMonitor monitor) {
		// ignore any server messages	
		if (getServerMessage(line, location) != null) {
			return OK;
		}
		Map map = LOCAL_FILE_MATCHER.processServerMessage(line);
		if (map != null) {
			localFilePath = (String)map.get("localFile");
			return OK;
		}
		map = REMOTE_FILE_MATCHER.processServerMessage(line);
		if (map != null) {
			remoteFilePath = (String)map.get("remoteFile");
			return OK;
		}
		map = REVISION_LINE_MATCHER.processServerMessage(line);
		if (map != null) {
			leftRevision = (String)map.get("leftRevision");
			rightRevision = (String)map.get("rightRevision");
			if (localFilePath == null || remoteFilePath == null) {
				return new CVSStatus(IStatus.ERROR, "Unsupported message sequence received while comparing using CVS diff command");
			}
			listener.fileDiff(localFilePath, remoteFilePath, leftRevision, rightRevision);
			localFilePath = remoteFilePath = leftRevision = rightRevision  = null;
			return OK;
		}
		// Ignore all other lines
		return OK;
	}

	private IStatus handleUnknownDiffFormat(String line) {
		return new CVSStatus(IStatus.ERROR, "Unknown message format received while comparing using CVS diff command: {0}" + line);
	}

	public IStatus errorLine(
			String line, 
			ICVSRepositoryLocation location, 
			ICVSFolder commandRoot,
			IProgressMonitor monitor) {
		// ignore server messages for now - this is used only with the diff
		// request and the errors can be safely ignored.
		if (getServerMessage(line, location) != null) {
			return OK;
		}
		return super.errorLine(line, location, commandRoot, monitor);
	}

}

Back to the top