Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc4d6fbfd1a50c6960d099e7ad7a83677ded8bc9 (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 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.tests.ccvs.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.team.internal.ccvs.core.CVSException;
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.Command;
import org.eclipse.team.internal.ccvs.core.client.Session;
import org.eclipse.team.internal.ccvs.core.client.Command.GlobalOption;
import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.junit.Assert;

public class EclipseCVSClient implements ICVSClient {
	public static final ICVSClient INSTANCE = new EclipseCVSClient();
	private static final HashMap<String, Command> commandPool = new HashMap<>();
	static {
		commandPool.put("update", Command.UPDATE);
		commandPool.put("co", Command.CHECKOUT);
		commandPool.put("ci", Command.COMMIT);
		commandPool.put("import", Command.IMPORT);
		commandPool.put("add", Command.ADD);
		commandPool.put("remove", Command.REMOVE);
		commandPool.put("status", Command.STATUS);
		commandPool.put("log", Command.LOG);
		commandPool.put("tag", Command.TAG);
		commandPool.put("rtag", Command.RTAG);
		commandPool.put("admin", Command.ADMIN);
		commandPool.put("diff", Command.DIFF);
	}
	
	@Override
	public void executeCommand(ICVSRepositoryLocation repositoryLocation,
		IContainer localRoot, String command, String[] globalOptions,
		String[] localOptions, String[] arguments) throws CVSException {
		execute(repositoryLocation, CVSWorkspaceRoot.getCVSFolderFor(localRoot), command,
			globalOptions, localOptions, arguments);
	}
	
	public static void execute(
		ICVSRepositoryLocation cvsRepositoryLocation, ICVSFolder cvsLocalRoot,
		String command, String[] globalOptions, String[] localOptions,
		String[] arguments) throws CVSException {
		// test arguments
		Assert.assertNotNull(cvsRepositoryLocation);
		Assert.assertNotNull(cvsLocalRoot);
		Assert.assertNotNull(command);
		Assert.assertNotNull(globalOptions);
		Assert.assertNotNull(localOptions);
		Assert.assertNotNull(arguments);
		Assert.assertTrue(cvsLocalRoot.exists());

		// get command instance
		Command cvsCommand = commandPool.get(command);
			
		// get global options
		List<CustomGlobalOption> globals = new ArrayList<>();
		for (int i = 0; i < globalOptions.length; i++) {
			globals.add(new CustomGlobalOption(globalOptions[i]));
		}
		GlobalOption[] cvsGlobalOptions = globals.toArray(new GlobalOption[globals.size()]);
		
		// get local options
		List<CustomLocalOption> locals = new ArrayList<CustomLocalOption>();
		for (int i = 0; i < localOptions.length; i++) {
			String option = localOptions[i];
			String argument = null;
			if ((i < localOptions.length - 1) && (localOptions[i + 1].charAt(0) != '-')) {
				argument = localOptions[++i];
			}
			locals.add(new CustomLocalOption(option, argument));
		}
		LocalOption[] cvsLocalOptions = locals.toArray(new LocalOption[locals.size()]);
		
		// execute command
		IProgressMonitor monitor = new NullProgressMonitor();
		Session session = new Session(cvsRepositoryLocation, cvsLocalRoot);
		try {
			session.open(monitor, true /* open for modification */);
			IStatus status = cvsCommand.execute(session,
				cvsGlobalOptions, cvsLocalOptions, arguments, null, monitor);
			if (status.getCode() == CVSStatus.SERVER_ERROR) {
				throw new CVSClientException("Eclipse client returned non-ok status: " + status);
			}
		} finally {
			session.close();
			monitor.done();
		}
	}

	private static class CustomGlobalOption extends GlobalOption {
		public CustomGlobalOption(String option) {
			super(option);
		}
	}

	private static class CustomLocalOption extends LocalOption {
		public CustomLocalOption(String option, String arg) {
			super(option, arg);
		}
	}
}

Back to the top