34
34
import java .util .ArrayList ;
35
35
import java .util .HashSet ;
36
36
import java .util .List ;
37
+ import java .util .Objects ;
37
38
import java .util .Set ;
38
39
import java .util .function .Supplier ;
39
40
import java .util .logging .Level ;
44
45
import javax .xml .parsers .DocumentBuilderFactory ;
45
46
import javax .xml .parsers .ParserConfigurationException ;
46
47
48
+ import org .jetbrains .annotations .Nullable ;
47
49
import org .jetbrains .annotations .VisibleForTesting ;
48
50
import org .opengrok .indexer .configuration .CommandTimeoutType ;
49
51
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
57
59
58
60
/**
59
61
* Access to a Subversion repository.
60
- *
62
+ * <p>
61
63
* <b>TODO</b> The current implementation does <b>not</b> support nested
62
64
* repositories as described in http://svnbook.red-bean.com/en/1.0/ch07s03.html
63
- *
65
+ * </p>
64
66
* @author Trond Norbye
65
67
*/
66
68
public class SubversionRepository extends Repository {
@@ -120,11 +122,10 @@ private String getValue(Node node) {
120
122
}
121
123
122
124
/**
123
- * Get {@code Document} corresponding to the parsed XML output from
124
- * {@code svn info} command.
125
- * @return document with data from {@code info} or null if the {@code svn}
126
- * command failed
125
+ * Get {@code Document} corresponding to the parsed XML output from {@code svn info} command.
126
+ * @return document with data from {@code info} or null if the {@code svn} command failed
127
127
*/
128
+ @ Nullable
128
129
private Document getInfoDocument () {
129
130
Document document = null ;
130
131
List <String > cmd = new ArrayList <>();
@@ -146,26 +147,21 @@ private Document getInfoDocument() {
146
147
DocumentBuilder builder = factory .newDocumentBuilder ();
147
148
document = builder .parse (executor .getOutputStream ());
148
149
} catch (SAXException saxe ) {
149
- LOGGER .log (Level .WARNING ,
150
- "Parser error parsing svn output" , saxe );
150
+ LOGGER .log (Level .WARNING , "Parser error parsing svn output" , saxe );
151
151
} catch (ParserConfigurationException pce ) {
152
- LOGGER .log (Level .WARNING ,
153
- "Parser configuration error parsing svn output" , pce );
152
+ LOGGER .log (Level .WARNING , "Parser configuration error parsing svn output" , pce );
154
153
} catch (IOException ioe ) {
155
- LOGGER .log (Level .WARNING ,
156
- "IOException reading from svn process" , ioe );
154
+ LOGGER .log (Level .WARNING , "IOException reading from svn process" , ioe );
157
155
}
158
156
} else {
159
- LOGGER .log (Level .WARNING ,
160
- "Failed to execute svn info for [{0}]. Repository disabled." ,
161
- getDirectoryName ());
157
+ LOGGER .log (Level .WARNING , "Failed to execute svn info for ''{0}''" , getDirectoryName ());
162
158
}
163
159
164
160
return document ;
165
161
}
166
162
167
163
/**
168
- * Get value of given tag in ' svn info' document.
164
+ * Get value of given tag in @{code svn info} document.
169
165
* @param document document object containing {@code info} contents
170
166
* @param tagName name of the tag to return value for
171
167
* @return value string
@@ -187,7 +183,7 @@ public void setDirectoryName(File directory) {
187
183
String url = getInfoPart (document , URL_ATTR );
188
184
if (url == null ) {
189
185
LOGGER .log (Level .WARNING ,
190
- "svn info did not contain an URL for [ {0}] . Assuming remote repository." ,
186
+ "svn info did not contain an URL for '' {0}'' . Assuming remote repository." ,
191
187
getDirectoryName ());
192
188
setRemote (true );
193
189
} else {
@@ -196,8 +192,7 @@ public void setDirectoryName(File directory) {
196
192
}
197
193
}
198
194
199
- String root
200
- = getValue (document .getElementsByTagName ("root" ).item (0 ));
195
+ String root = getValue (document .getElementsByTagName ("root" ).item (0 ));
201
196
if (url != null && root != null ) {
202
197
reposPath = url .substring (root .length ());
203
198
rootFound = Boolean .TRUE ;
@@ -243,7 +238,7 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision,
243
238
// fetch the unneeded revision and remove it later.
244
239
cmd .add ("BASE:" + sinceRevision );
245
240
}
246
- if (filename .length () > 0 ) {
241
+ if (! filename .isEmpty () ) {
247
242
cmd .add (escapeFileName (filename ));
248
243
}
249
244
@@ -260,8 +255,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
260
255
try {
261
256
filepath = new File (parent , basename ).getCanonicalPath ();
262
257
} catch (IOException exp ) {
263
- LOGGER .log (Level .SEVERE ,
264
- "Failed to get canonical path: {0}" , exp .getClass ());
258
+ LOGGER .log (Level .SEVERE , "Failed to get canonical path: {0}" , exp .getClass ());
265
259
return false ;
266
260
}
267
261
String filename = filepath .substring (getDirectoryName ().length () + 1 );
@@ -280,11 +274,13 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
280
274
RuntimeEnvironment .getInstance ().getInteractiveCommandTimeout ());
281
275
if (executor .exec () == 0 ) {
282
276
try {
283
- copyBytes (out ::write , executor .getOutputStream ());
277
+ InputStream stream = executor .getOutputStream ();
278
+ if (!Objects .isNull (stream )) {
279
+ copyBytes (out ::write , stream );
280
+ }
284
281
return true ;
285
282
} catch (IOException e ) {
286
- LOGGER .log (Level .SEVERE , "Failed to get content for {0}" ,
287
- basename );
283
+ LOGGER .log (Level .SEVERE , "Failed to get content for ''{0}''" , basename );
288
284
}
289
285
}
290
286
@@ -318,35 +314,28 @@ private Executor getDirectoryListExecutor(final String dir, String atRevision, C
318
314
* Provides a list of files that were in a directory at a given revision.
319
315
* This is useful for finding files that will need special renamed file history
320
316
* handling because they were in a directory when it was renamed.
321
- *
317
+ * <p>
322
318
* Note that this doesn't throw an exception even if the command was not completed
323
319
* because we will still be able to get the file history up to this point.
324
- *
320
+ * </p>
325
321
* @param directory the directory to check
326
322
* @param revision the revision to check at
327
323
* @param cmdType the timeout setting.
328
324
* @return the files that were in the directory at that revision
329
325
*/
330
- Set <String > getFilesInDirectoryAtRevision (String directory , String revision ,
331
- CommandTimeoutType cmdType ) {
326
+ Set <String > getFilesInDirectoryAtRevision (String directory , String revision , CommandTimeoutType cmdType ) {
332
327
333
328
Executor executor = getDirectoryListExecutor (
334
329
RuntimeEnvironment .getInstance ().getSourceRootPath () + File .separator + directory ,
335
330
revision , cmdType );
336
331
337
332
Set <String > files = new HashSet <>();
338
333
339
- StreamHandler directoryLogStreamHandler = new StreamHandler () {
340
-
341
- @ Override
342
- public void processStream (InputStream in ) throws IOException {
343
- new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ))
344
- .lines ()
345
- .filter (s -> !s .isBlank ())
346
- .map (s -> directory + File .separator + s )
347
- .forEach (files ::add );
348
- }
349
- };
334
+ StreamHandler directoryLogStreamHandler = in -> new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ))
335
+ .lines ()
336
+ .filter (s -> !s .isBlank ())
337
+ .map (s -> directory + File .separator + s )
338
+ .forEach (files ::add );
350
339
351
340
int status = executor .exec (true , directoryLogStreamHandler );
352
341
if (status != 0 ) {
@@ -373,15 +362,14 @@ History getHistory(File file, String sinceRevision) throws HistoryException {
373
362
return getHistory (file , sinceRevision , 0 , CommandTimeoutType .INDEXER );
374
363
}
375
364
376
- private History getHistory (File file , String sinceRevision , int numEntries ,
377
- CommandTimeoutType cmdType )
365
+ private History getHistory (File file , String sinceRevision , int numEntries , CommandTimeoutType cmdType )
378
366
throws HistoryException {
379
367
return new SubversionHistoryParser ().parse (file , this , sinceRevision ,
380
368
numEntries , cmdType );
381
369
}
382
370
383
371
private String escapeFileName (String name ) {
384
- if (name .length () == 0 ) {
372
+ if (name .isEmpty () ) {
385
373
return name ;
386
374
}
387
375
return name + "@" ;
@@ -423,7 +411,7 @@ public boolean fileHasAnnotation(File file) {
423
411
424
412
@ Override
425
413
public boolean fileHasHistory (File file ) {
426
- // @ TODO: Research how to cheaply test if a file in a given
414
+ // TODO: Research how to cheaply test if a file in a given
427
415
// SVN repo has history. If there is a cheap test, then this
428
416
// code can be refined, boosting performance.
429
417
return true ;
@@ -517,7 +505,7 @@ public String determineCurrentVersion(CommandTimeoutType cmdType) throws IOExcep
517
505
}
518
506
}
519
507
} catch (HistoryException ex ) {
520
- LOGGER .log (Level .WARNING , "cannot get current version info for {0}" ,
508
+ LOGGER .log (Level .WARNING , "cannot get current version info for '' {0}'' " ,
521
509
getDirectoryName ());
522
510
}
523
511
0 commit comments