mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-01 11:05:56 +01:00
implemented new browse api for svn
This commit is contained in:
@@ -35,6 +35,7 @@ package sonia.scm.repository.spi;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -79,11 +80,11 @@ public class SvnBrowseCommand extends AbstractSvnCommand
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public BrowserResult getBrowserResult(BrowseCommandRequest request) throws RevisionNotFoundException {
|
||||
String path = request.getPath();
|
||||
String path = Strings.nullToEmpty(request.getPath());
|
||||
long revisionNumber = SvnUtil.getRevisionNumber(request.getRevision());
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("browser repository {} in path {} at revision {}", repository.getName(), path, revisionNumber);
|
||||
logger.debug("browser repository {} in path \"{}\" at revision {}", repository.getName(), path, revisionNumber);
|
||||
}
|
||||
|
||||
BrowserResult result = null;
|
||||
@@ -91,34 +92,21 @@ public class SvnBrowseCommand extends AbstractSvnCommand
|
||||
try
|
||||
{
|
||||
SVNRepository svnRepository = open();
|
||||
Collection<SVNDirEntry> entries =
|
||||
svnRepository.getDir(Util.nonNull(path), revisionNumber, null,
|
||||
(Collection) null);
|
||||
List<FileObject> children = Lists.newArrayList();
|
||||
String basePath = createBasePath(path);
|
||||
|
||||
if (request.isRecursive())
|
||||
{
|
||||
browseRecursive(svnRepository, revisionNumber, request, children,
|
||||
entries, basePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (SVNDirEntry entry : entries)
|
||||
{
|
||||
children.add(createFileObject(request, svnRepository, revisionNumber,
|
||||
entry, basePath));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (revisionNumber == -1) {
|
||||
revisionNumber = svnRepository.getLatestRevision();
|
||||
}
|
||||
|
||||
result = new BrowserResult();
|
||||
result.setRevision(String.valueOf(revisionNumber));
|
||||
result.setFiles(children);
|
||||
SVNDirEntry rootEntry = svnRepository.info(path, revisionNumber);
|
||||
FileObject root = createFileObject(request, svnRepository, revisionNumber, rootEntry, path);
|
||||
root.setPath(path);
|
||||
|
||||
if (root.isDirectory()) {
|
||||
traverse(svnRepository, revisionNumber, request, root, createBasePath(path));
|
||||
}
|
||||
|
||||
|
||||
result = new BrowserResult(String.valueOf(revisionNumber), root);
|
||||
}
|
||||
catch (SVNException ex)
|
||||
{
|
||||
@@ -130,52 +118,24 @@ public class SvnBrowseCommand extends AbstractSvnCommand
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param svnRepository
|
||||
* @param revisionNumber
|
||||
* @param request
|
||||
* @param children
|
||||
* @param entries
|
||||
* @param basePath
|
||||
*
|
||||
* @throws SVNException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void browseRecursive(SVNRepository svnRepository,
|
||||
long revisionNumber, BrowseCommandRequest request,
|
||||
List<FileObject> children, Collection<SVNDirEntry> entries, String basePath)
|
||||
private void traverse(SVNRepository svnRepository, long revisionNumber, BrowseCommandRequest request,
|
||||
FileObject parent, String basePath)
|
||||
throws SVNException
|
||||
{
|
||||
Collection<SVNDirEntry> entries = svnRepository.getDir(parent.getPath(), revisionNumber, null, (Collection) null);
|
||||
for (SVNDirEntry entry : entries)
|
||||
{
|
||||
FileObject fo = createFileObject(request, svnRepository, revisionNumber,
|
||||
entry, basePath);
|
||||
FileObject child = createFileObject(request, svnRepository, revisionNumber, entry, basePath);
|
||||
|
||||
children.add(fo);
|
||||
parent.addChild(child);
|
||||
|
||||
if (fo.isDirectory())
|
||||
{
|
||||
Collection<SVNDirEntry> subEntries =
|
||||
svnRepository.getDir(Util.nonNull(fo.getPath()), revisionNumber,
|
||||
null, (Collection) null);
|
||||
|
||||
browseRecursive(svnRepository, revisionNumber, request, children,
|
||||
subEntries, createBasePath(fo.getPath()));
|
||||
if (child.isDirectory() && request.isRecursive()) {
|
||||
traverse(svnRepository, revisionNumber, request, child, createBasePath(child.getPath()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private String createBasePath(String path)
|
||||
{
|
||||
String basePath = Util.EMPTY_STRING;
|
||||
@@ -193,20 +153,6 @@ public class SvnBrowseCommand extends AbstractSvnCommand
|
||||
return basePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param repository
|
||||
* @param revision
|
||||
* @param entry
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private FileObject createFileObject(BrowseCommandRequest request,
|
||||
SVNRepository repository, long revision, SVNDirEntry entry, String path)
|
||||
{
|
||||
@@ -237,15 +183,6 @@ public class SvnBrowseCommand extends AbstractSvnCommand
|
||||
return fileObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param revision
|
||||
* @param entry
|
||||
* @param fileObject
|
||||
*/
|
||||
private void fetchExternalsProperty(SVNRepository repository, long revision,
|
||||
SVNDirEntry entry, FileObject fileObject)
|
||||
{
|
||||
|
||||
@@ -58,6 +58,16 @@ import static org.junit.Assert.assertTrue;
|
||||
public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
|
||||
{
|
||||
|
||||
@Test
|
||||
public void testBrowseWithFilePath() throws RevisionNotFoundException {
|
||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||
request.setPath("a.txt");
|
||||
FileObject file = createCommand().getBrowserResult(request).getFile();
|
||||
assertEquals("a.txt", file.getName());
|
||||
assertFalse(file.isDirectory());
|
||||
assertTrue(file.getChildren().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBrowse() throws RevisionNotFoundException {
|
||||
List<FileObject> foList = getRootFromTip(new BrowseCommandRequest());
|
||||
@@ -92,7 +102,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
|
||||
|
||||
assertNotNull(result);
|
||||
|
||||
List<FileObject> foList = result.getFiles();
|
||||
List<FileObject> foList = result.getFile().getChildren();
|
||||
|
||||
assertNotNull(foList);
|
||||
assertFalse(foList.isEmpty());
|
||||
@@ -151,15 +161,16 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
|
||||
|
||||
assertNotNull(result);
|
||||
|
||||
List<FileObject> foList = result.getFiles();
|
||||
List<FileObject> foList = result.getFile().getChildren();
|
||||
|
||||
assertNotNull(foList);
|
||||
assertFalse(foList.isEmpty());
|
||||
assertEquals(4, foList.size());
|
||||
|
||||
for ( FileObject fo : foList ){
|
||||
System.out.println(fo);
|
||||
}
|
||||
assertEquals(2, foList.size());
|
||||
|
||||
FileObject c = getFileObject(foList, "c");
|
||||
assertEquals("c", c.getName());
|
||||
assertTrue(c.isDirectory());
|
||||
assertEquals(2, c.getChildren().size());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,7 +219,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
|
||||
|
||||
assertNotNull(result);
|
||||
|
||||
List<FileObject> foList = result.getFiles();
|
||||
List<FileObject> foList = result.getFile().getChildren();
|
||||
|
||||
assertNotNull(foList);
|
||||
assertFalse(foList.isEmpty());
|
||||
|
||||
Reference in New Issue
Block a user