1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.tests.ccvs.core;
15 
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.team.internal.ccvs.core.CVSException;
25 import org.eclipse.team.internal.ccvs.core.CVSStatus;
26 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
27 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
28 import org.eclipse.team.internal.ccvs.core.client.Command;
29 import org.eclipse.team.internal.ccvs.core.client.Session;
30 import org.eclipse.team.internal.ccvs.core.client.Command.GlobalOption;
31 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
32 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
33 import org.junit.Assert;
34 
35 public class EclipseCVSClient implements ICVSClient {
36 	public static final ICVSClient INSTANCE = new EclipseCVSClient();
37 	private static final HashMap<String, Command> commandPool = new HashMap<>();
38 	static {
39 		commandPool.put("update", Command.UPDATE);
40 		commandPool.put("co", Command.CHECKOUT);
41 		commandPool.put("ci", Command.COMMIT);
42 		commandPool.put("import", Command.IMPORT);
43 		commandPool.put("add", Command.ADD);
44 		commandPool.put("remove", Command.REMOVE);
45 		commandPool.put("status", Command.STATUS);
46 		commandPool.put("log", Command.LOG);
47 		commandPool.put("tag", Command.TAG);
48 		commandPool.put("rtag", Command.RTAG);
49 		commandPool.put("admin", Command.ADMIN);
50 		commandPool.put("diff", Command.DIFF);
51 	}
52 
53 	@Override
executeCommand(ICVSRepositoryLocation repositoryLocation, IContainer localRoot, String command, String[] globalOptions, String[] localOptions, String[] arguments)54 	public void executeCommand(ICVSRepositoryLocation repositoryLocation,
55 		IContainer localRoot, String command, String[] globalOptions,
56 		String[] localOptions, String[] arguments) throws CVSException {
57 		execute(repositoryLocation, CVSWorkspaceRoot.getCVSFolderFor(localRoot), command,
58 			globalOptions, localOptions, arguments);
59 	}
60 
execute( ICVSRepositoryLocation cvsRepositoryLocation, ICVSFolder cvsLocalRoot, String command, String[] globalOptions, String[] localOptions, String[] arguments)61 	public static void execute(
62 		ICVSRepositoryLocation cvsRepositoryLocation, ICVSFolder cvsLocalRoot,
63 		String command, String[] globalOptions, String[] localOptions,
64 		String[] arguments) throws CVSException {
65 		// test arguments
66 		Assert.assertNotNull(cvsRepositoryLocation);
67 		Assert.assertNotNull(cvsLocalRoot);
68 		Assert.assertNotNull(command);
69 		Assert.assertNotNull(globalOptions);
70 		Assert.assertNotNull(localOptions);
71 		Assert.assertNotNull(arguments);
72 		Assert.assertTrue(cvsLocalRoot.exists());
73 
74 		// get command instance
75 		Command cvsCommand = commandPool.get(command);
76 
77 		// get global options
78 		List<CustomGlobalOption> globals = new ArrayList<>();
79 		for (String globalOption : globalOptions) {
80 			globals.add(new CustomGlobalOption(globalOption));
81 		}
82 		GlobalOption[] cvsGlobalOptions = globals.toArray(new GlobalOption[globals.size()]);
83 
84 		// get local options
85 		List<CustomLocalOption> locals = new ArrayList<>();
86 		for (int i = 0; i < localOptions.length; i++) {
87 			String option = localOptions[i];
88 			String argument = null;
89 			if ((i < localOptions.length - 1) && (localOptions[i + 1].charAt(0) != '-')) {
90 				argument = localOptions[++i];
91 			}
92 			locals.add(new CustomLocalOption(option, argument));
93 		}
94 		LocalOption[] cvsLocalOptions = locals.toArray(new LocalOption[locals.size()]);
95 
96 		// execute command
97 		IProgressMonitor monitor = new NullProgressMonitor();
98 		Session session = new Session(cvsRepositoryLocation, cvsLocalRoot);
99 		try {
100 			session.open(monitor, true /* open for modification */);
101 			IStatus status = cvsCommand.execute(session,
102 				cvsGlobalOptions, cvsLocalOptions, arguments, null, monitor);
103 			if (status.getCode() == CVSStatus.SERVER_ERROR) {
104 				throw new CVSClientException("Eclipse client returned non-ok status: " + status);
105 			}
106 		} finally {
107 			session.close();
108 			monitor.done();
109 		}
110 	}
111 
112 	private static class CustomGlobalOption extends GlobalOption {
CustomGlobalOption(String option)113 		public CustomGlobalOption(String option) {
114 			super(option);
115 		}
116 	}
117 
118 	private static class CustomLocalOption extends LocalOption {
CustomLocalOption(String option, String arg)119 		public CustomLocalOption(String option, String arg) {
120 			super(option, arg);
121 		}
122 	}
123 }
124