/
SubversionRepository.java
461 lines (411 loc) · 15.2 KB
/
SubversionRepository.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.history;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.opensolaris.opengrok.logger.LoggerFactory;
import org.opensolaris.opengrok.util.Executor;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;
/**
* Access to a Subversion repository.
*
* <b>TODO</b> The current implementation does <b>not</b> support nested
* repositories as described in http://svnbook.red-bean.com/en/1.0/ch07s03.html
*
* @author Trond Norbye
*/
public class SubversionRepository extends Repository {
private static final Logger LOGGER = LoggerFactory.getLogger(SubversionRepository.class);
private static final long serialVersionUID = 1L;
private static final String ENV_SVN_USERNAME = "OPENGROK_SUBVERSION_USERNAME";
private static final String ENV_SVN_PASSWORD = "OPENGROK_SUBVERSION_PASSWORD";
/**
* The property name used to obtain the client command for this repository.
*/
public static final String CMD_PROPERTY_KEY
= "org.opensolaris.opengrok.history.Subversion";
/**
* The command to use to access the repository if none was given explicitly
*/
public static final String CMD_FALLBACK = "svn";
protected String reposPath;
public SubversionRepository() {
type = "Subversion";
datePatterns = new String[]{
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.'Z'",
"yyyy-MM-dd'T'HH:mm:ss'Z'"
};
}
private String getValue(Node node) {
if (node == null) {
return null;
}
StringBuilder sb = new StringBuilder();
Node n = node.getFirstChild();
while (n != null) {
if (n.getNodeType() == Node.TEXT_NODE) {
sb.append(n.getNodeValue());
}
n = n.getNextSibling();
}
return sb.toString();
}
@Override
public void setDirectoryName(String directoryName) {
super.setDirectoryName(directoryName);
if (isWorking()) {
// set to true if we manage to find the root directory
Boolean rootFound = Boolean.FALSE;
List<String> cmd = new ArrayList<>();
cmd.add(RepoCommand);
cmd.add("info");
cmd.add("--xml");
File directory = new File(getDirectoryName());
Executor executor = new Executor(cmd, directory);
if (executor.exec() == 0) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(executor.getOutputStream());
String url
= getValue(document.getElementsByTagName("url").item(0));
if (url == null) {
LOGGER.log(Level.WARNING,
"svn info did not contain an URL for [{0}]. Assuming remote repository.",
directoryName);
setRemote(true);
} else {
if (!url.startsWith("file")) {
setRemote(true);
}
}
String root
= getValue(document.getElementsByTagName("root").item(0));
if (url != null && root != null) {
reposPath = url.substring(root.length());
rootFound = Boolean.TRUE;
}
} catch (SAXException saxe) {
LOGGER.log(Level.WARNING,
"Parser error parsing svn output", saxe);
} catch (ParserConfigurationException pce) {
LOGGER.log(Level.WARNING,
"Parser configuration error parsing svn output", pce);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING,
"IOException reading from svn process", ioe);
}
} else {
LOGGER.log(Level.WARNING,
"Failed to execute svn info for [{0}]. Repository disabled.",
directoryName);
}
setWorking(rootFound);
}
}
/**
* Get an executor to be used for retrieving the history log for the named
* file.
*
* @param file The file to retrieve history for
* @param sinceRevision the revision number immediately preceding the first
* revision we want, or {@code null} to fetch the entire
* history
* @return An Executor ready to be started
*/
Executor getHistoryLogExecutor(final File file, String sinceRevision) {
String abs;
try {
abs = file.getCanonicalPath();
} catch (IOException exp) {
LOGGER.log(Level.SEVERE,
"Failed to get canonical path: {0}", exp.getClass().toString());
return null;
}
String filename = "";
if (abs.length() > directoryName.length()) {
filename = abs.substring(directoryName.length() + 1);
}
List<String> cmd = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("log");
cmd.add("--non-interactive");
cmd.addAll(getAuthCommandLineParams());
cmd.add("--xml");
cmd.add("-v");
if (sinceRevision != null) {
cmd.add("-r");
// We would like to use sinceRevision+1 here, but if no new
// revisions have been added after sinceRevision, it would fail
// because there is no such revision as sinceRevision+1. Instead,
// fetch the unneeded revision and remove it later.
cmd.add("BASE:" + sinceRevision);
}
if (filename.length() > 0) {
cmd.add(escapeFileName(filename));
}
return new Executor(cmd, new File(directoryName), sinceRevision != null);
}
@Override
public InputStream getHistoryGet(String parent, String basename, String rev) {
InputStream ret = null;
File directory = new File(directoryName);
String filepath;
try {
filepath = (new File(parent, basename)).getCanonicalPath();
} catch (IOException exp) {
LOGGER.log(Level.SEVERE,
"Failed to get canonical path: {0}", exp.getClass().toString());
return null;
}
String filename = filepath.substring(directoryName.length() + 1);
List<String> cmd = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("cat");
cmd.add("-r");
cmd.add(rev);
cmd.add(escapeFileName(filename));
Executor executor = new Executor(cmd, directory);
if (executor.exec() == 0) {
ret = executor.getOutputStream();
}
return ret;
}
@Override
boolean hasHistoryForDirectories() {
return true;
}
@Override
History getHistory(File file) throws HistoryException {
return getHistory(file, null);
}
@Override
History getHistory(File file, String sinceRevision)
throws HistoryException {
return new SubversionHistoryParser().parse(file, this, sinceRevision);
}
private String escapeFileName(String name) {
if (name.length() == 0) {
return name;
}
return name + "@";
}
private static class AnnotateHandler extends DefaultHandler2 {
String rev;
String author;
final Annotation annotation;
final StringBuilder sb;
AnnotateHandler(String filename) {
annotation = new Annotation(filename);
sb = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String qname,
Attributes attr) {
sb.setLength(0);
if (null != qname) {
switch (qname) {
case "entry":
rev = null;
author = null;
break;
case "commit":
rev = attr.getValue("revision");
break;
}
}
}
@Override
public void endElement(String uri, String localName, String qname) {
if (null != qname) {
switch (qname) {
case "author":
author = sb.toString();
break;
case "entry":
annotation.addLine(rev, author, true);
break;
}
}
}
@Override
public void characters(char[] arg0, int arg1, int arg2) {
sb.append(arg0, arg1, arg2);
}
}
@Override
public Annotation annotate(File file, String revision) throws IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try {
saxParser = factory.newSAXParser();
} catch (ParserConfigurationException | SAXException ex) {
IOException err = new IOException("Failed to create SAX parser", ex);
throw err;
}
ArrayList<String> argv = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
argv.add(RepoCommand);
argv.add("annotate");
argv.addAll(getAuthCommandLineParams());
argv.add("--non-interactive");
argv.add("--xml");
if (revision != null) {
argv.add("-r");
argv.add(revision);
}
argv.add(escapeFileName(file.getName()));
ProcessBuilder pb = new ProcessBuilder(argv);
pb.directory(file.getParentFile());
Process process = null;
Annotation ret = null;
try {
process = pb.start();
AnnotateHandler handler = new AnnotateHandler(file.getName());
try (BufferedInputStream in
= new BufferedInputStream(process.getInputStream())) {
saxParser.parse(in, handler);
ret = handler.annotation;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"An error occurred while parsing the xml output", e);
}
} finally {
if (process != null) {
try {
process.exitValue();
} catch (IllegalThreadStateException e) {
// the process is still running??? just kill it..
process.destroy();
}
}
}
return ret;
}
@Override
public boolean fileHasAnnotation(File file) {
return true;
}
@Override
public boolean fileHasHistory(File file) {
// @TODO: Research how to cheaply test if a file in a given
// SVN repo has history. If there is a cheap test, then this
// code can be refined, boosting performance.
return true;
}
@Override
public void update() throws IOException {
File directory = new File(getDirectoryName());
List<String> cmd = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("update");
cmd.addAll(getAuthCommandLineParams());
cmd.add("--non-interactive");
Executor executor = new Executor(cmd, directory);
if (executor.exec() != 0) {
throw new IOException(executor.getErrorString());
}
}
@Override
boolean isRepositoryFor(File file) {
if (file.isDirectory()) {
File f = new File(file, ".svn");
return f.exists() && f.isDirectory();
}
return false;
}
@Override
public boolean isWorking() {
if (working == null) {
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
working = checkCmd(RepoCommand, "--help");
}
return working;
}
private List<String> getAuthCommandLineParams() {
List<String> result = new ArrayList<>();
String userName = System.getenv(ENV_SVN_USERNAME);
String password = System.getenv(ENV_SVN_PASSWORD);
if (userName != null && !userName.isEmpty() && password != null
&& !password.isEmpty()) {
result.add("--username");
result.add(userName);
result.add("--password");
result.add(password);
}
result.add("--trust-server-cert");
return result;
}
@Override
String determineParent() throws IOException {
String parent = null;
File directory = new File(directoryName);
List<String> cmd = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("info");
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(directory);
Process process;
process = pb.start();
try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith("URL:")) {
String parts[] = line.split("\\s+");
if (parts.length != 2) {
LOGGER.log(Level.WARNING,
"Failed to get parent for {0}", directoryName);
}
parent = parts[1];
break;
}
}
}
return parent;
}
@Override
String determineBranch() {
return null;
}
}