Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3268bf4d698e50b7a572f11b19d533962fe41d5 (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
194
195
196
197
198
199
200
201
202
203
package org.eclipse.team.tests.ccvs.core.compatible;
/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.ccvs.core.*;
import org.eclipse.team.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.client.Session;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.util.SyncFileUtil;

/**
 * This class is responsible for executing cvs commands using
 * a reference CVS command line client.
 */
public class ReferenceClient {
	
	public static final String cvsLocation = System.getProperty("eclipse.cvs.command");
	/**
	 * Puts opetions into one String seperated by
	 * space.
	 * starts and ends with a space.
	 */
	private static String flatenOptions(String[] options) {
		
		StringBuffer result = new StringBuffer(" ");
		String quote;
		
		for (int i=0; i<options.length; i++) {
			
			if (options[i].indexOf(" ")==-1) {
				quote = "";
			} else {
				quote = "\"";
			}
			result.append(quote);
			result.append(options[i]);
			result.append(quote);
			result.append(' ');
		}
			
		return result.toString();
	}
	
	public static void execute(String request, 
						String[] globalOptions, 
						String[] localOptions, 
						String[] arguments,
						File ioRoot,
						IProgressMonitor monitor, 
						PrintStream messageOut) 
						throws CVSException {

		Runtime runtime;
		Process process;
		BufferedReader stdIn;
		BufferedReader errIn;
		ICVSFolder mRoot;
		
		String global;
		String local;
		String arg;
		String commandLine;

		globalOptions = (String[]) globalOptions.clone();
		mRoot = CVSWorkspaceRoot.getCVSFolderFor(ioRoot);
		
		runtime = Runtime.getRuntime();
		global = flatenOptions(globalOptions);
		local = flatenOptions(localOptions);
		arg = flatenOptions(arguments);
		
		commandLine = cvsLocation + " ";
		commandLine = commandLine + global;
		commandLine = commandLine + request + " ";
		commandLine = commandLine + local;
		commandLine = commandLine + arg;
		
		// System.out.println(ioRoot.getPath() + "> " + commandLine);
		
		try {
			process = runtime.exec(commandLine, null, ioRoot);
		} catch (IOException e) {
			throw new CVSException("IOException while executing ReferenceClient",e);
		}
		
		stdIn = new BufferedReader(new InputStreamReader(process.getInputStream()));
		new ContiniousPipe(stdIn, messageOut, "M ");

		errIn = new BufferedReader(new InputStreamReader(process.getErrorStream()));
		new ContiniousPipe(errIn, messageOut, "E ");

		try {
			process.waitFor();
		} catch (InterruptedException e) {
			throw new CVSException("InterruptedException while executing ReferenceClient",e);
		}
		
		if (process.exitValue() != 0) {
			throw new ReferenceException("Return Code of CVS reference client: " + 
									process.exitValue() + "\nwhile executing: " + 
									commandLine);
		}	
		
		SyncFileUtil.mergeEntriesLogFiles(ioRoot);
		
	}
	
	/**
	 * 
	 * returns ":pserver:username@host:/cvs/root"
	 *         when you insert ":pserver:username:password@host:/cvs/root"
	 */
	public static String removePassword(String repoName) {
		
		int atPlace = -1;
		int colonPlace = -1;
		int colonCount = 0;
		String currentChar; 
		
		for (int i=0; i<repoName.length(); i++) {
			
			currentChar = repoName.substring(i,i+1);
			
			if (currentChar.equals(":")) {
				colonCount++;
				
				if (colonCount == 3) {
					colonPlace = i;
				}
			}
			
			if (currentChar.equals("@")) {
				if (colonPlace == -1) {
					
					// If the @ comes before the third colon, then 
					// we do not have a password and return with the
					// same string
					return repoName;
				} else {
					atPlace = i;
				}
				
			}
		}
		
		if (atPlace == -1) {
			return repoName;
		}
		
		return repoName.substring(0,colonPlace) + repoName.substring(atPlace);
	}
}

/**
 * This class does continiously pipe from a bufferdReader
 * to a printStream. It does stop as soon, as the bufferdReader is
 * closed an therefore an IOException is thrown or the pipe returns null.
 * 
 * It does close the BufferedReader on it's own (to be sure that it got
 * everything)
 */
class ContiniousPipe implements Runnable {
	
	BufferedReader in;
	PrintStream out;
	String prefix;
	
	ContiniousPipe(BufferedReader in, PrintStream out, String prefix) {
		this.in = in;
		this.out = out;
		this.prefix = prefix;
		(new Thread(this)).start();
	}
	
	public void run() {
		
		String line;
		
		try {
			while ((line=in.readLine()) != null) {
				out.println(prefix + line);
			}
		} catch (IOException e) {
			// Should not happen, as the PrintStream does not throw IOExceptions
			// at all an in is a stream from a process
		} finally {
			
			try {
				in.close();
			} catch (IOException e) {}
			
		}
	}
}

Back to the top