Skip to content

Commit c75a81d

Browse files
committed
getFileContent() speed improved: branch existing is checked only if
requested file is not found getBranchFirstCommit() speed improved: from head to first and take last -> from first to head, limit 1 and take first
1 parent 2962f77 commit c75a81d

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/main/java/org/scm4j/vcs/svn/SVNVCS.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,18 @@ public void setProxy(String host, int port, String proxyUser, String proxyPasswo
247247
public String getFileContent(String branchName, String filePath, String revision) {
248248
ByteArrayOutputStream baos = new ByteArrayOutputStream();
249249
try {
250-
if (repository.checkPath(getBranchName(branchName), -1L) == SVNNodeKind.NONE) {
251-
throw new EVCSBranchNotFound(getRepoUrl(), getBranchName(branchName));
252-
}
253250
repository.getFile(new File(getBranchName(branchName), filePath).getPath().replace("\\", "/"),
254251
(revision == null || revision.isEmpty()) ? -1 : Long.parseLong(revision), new SVNProperties(), baos);
255252
return baos.toString(StandardCharsets.UTF_8.name());
256-
} catch (EVCSBranchNotFound e) {
257-
throw e;
258253
} catch (SVNException e) {
259254
if (e.getErrorMessage().getErrorCode().getCode() == SVN_FILE_NOT_FOUND_ERROR_CODE) {
255+
try {
256+
if (repository.checkPath(getBranchName(branchName), -1L) == SVNNodeKind.NONE) {
257+
throw new EVCSBranchNotFound(getRepoUrl(), getBranchName(branchName));
258+
}
259+
} catch (SVNException e1) {
260+
throw new EVCSException(e1);
261+
}
260262
throw new EVCSFileNotFound(getRepoUrl(), getBranchName(branchName), filePath, revision);
261263
}
262264
throw new EVCSException(e);
@@ -348,15 +350,15 @@ private List<VCSDiffEntry> fillUnifiedDiffs(final String srcBranchName, final St
348350
}
349351

350352
protected SVNLogEntry getBranchFirstCommit(final String branchPath) throws Exception {
351-
final List<SVNLogEntry> logEntries = new ArrayList<>();
352-
repository.log(new String[] { getBranchName(branchPath) }, -1 /* start from head descending */,
353-
0, true, true, -1, new ISVNLogEntryHandler() {
353+
final SVNLogEntry[] firstEntryHolder = new SVNLogEntry[1];
354+
repository.log(new String[] { getBranchName(branchPath) }, 0 /* start from first commit */,
355+
-1 /* to the head commit */, true, true, 1 /* limit */, new ISVNLogEntryHandler() {
354356
@Override
355357
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
356-
logEntries.add(logEntry);
358+
firstEntryHolder[0] = logEntry;
357359
}
358360
});
359-
return logEntries.get(logEntries.size() - 1);
361+
return firstEntryHolder[0];
360362
}
361363

362364

src/test/java/org/scm4j/vcs/svn/SVNVCSTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ public void testGetFileContentExceptions() throws Exception {
285285
} catch (EVCSException e) {
286286
checkEVCSException(e);
287287
}
288+
doCallRealMethod().when(mockedRepo).getFile(anyString(), anyLong(), any(SVNProperties.class), any(OutputStream.class));
289+
doThrow(testSVNException).when(mockedRepo).checkPath(anyString(), anyLong());
290+
try {
291+
vcs.getFileContent("wrong-branch", "", "");
292+
fail();
293+
} catch (EVCSException e) {
294+
checkEVCSException(e);
295+
}
288296
}
289297

290298
@Test

0 commit comments

Comments
 (0)