Commits

Sebastian Sdorra committed cdcab7a

implement recursive option for subversion browse command

Comments (0)

Files changed (2)

scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnBrowseCommand.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.collect.Lists;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 
 import java.io.IOException;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
       Collection<SVNDirEntry> entries =
         svnRepository.getDir(Util.nonNull(path), revisionNumber, null,
           (Collection) null);
-      List<FileObject> children = new ArrayList<FileObject>();
-      String basePath = Util.EMPTY_STRING;
+      List<FileObject> children = Lists.newArrayList();
+      String basePath = createBasePath(path);
 
-      if (Util.isNotEmpty(path))
+      if (request.isRecursive())
       {
-        basePath = path;
+        browseRecursive(svnRepository, revisionNumber, request, children,
+          entries, basePath);
+      }
+      else
+      {
+        for (SVNDirEntry entry : entries)
+        {
+          children.add(createFileObject(request, svnRepository, revisionNumber,
+            entry, basePath));
 
-        if (!basePath.endsWith("/"))
-        {
-          basePath = basePath.concat("/");
         }
       }
 
-      for (SVNDirEntry entry : entries)
-      {
-        children.add(createFileObject(request, svnRepository, revisionNumber,
-          entry, basePath));
-      }
-
       result = new BrowserResult();
       result.setRevision(String.valueOf(revisionNumber));
       result.setFiles(children);
    * Method description
    *
    *
+   * @param svnRepository
+   * @param revisionNumber
+   * @param request
+   * @param children
+   * @param entries
+   * @param basePath
+   *
+   * @throws SVNException
+   */
+  private void browseRecursive(SVNRepository svnRepository,
+    long revisionNumber, BrowseCommandRequest request,
+    List<FileObject> children, Collection<SVNDirEntry> entries, String basePath)
+    throws SVNException
+  {
+    for (SVNDirEntry entry : entries)
+    {
+      FileObject fo = createFileObject(request, svnRepository, revisionNumber,
+                        entry, basePath);
+
+      children.add(fo);
+
+      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()));
+      }
+    }
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param path
+   *
+   * @return
+   */
+  private String createBasePath(String path)
+  {
+    String basePath = Util.EMPTY_STRING;
+
+    if (Util.isNotEmpty(path))
+    {
+      basePath = path;
+
+      if (!basePath.endsWith("/"))
+      {
+        basePath = basePath.concat("/");
+      }
+    }
+
+    return basePath;
+  }
+
+  /**
+   * Method description
+   *
+   *
    *
    *
    * @param request

scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java

     assertNull(a.getDescription());
     assertNull(a.getLastModified());
   }
+  
+  @Test
+  public void testRecursive() throws IOException, RepositoryException
+  {
+    BrowseCommandRequest request = new BrowseCommandRequest();
+    request.setRecursive(true);
+    BrowserResult result = createCommand().getBrowserResult(request);
+
+    assertNotNull(result);
+
+    List<FileObject> foList = result.getFiles();
+
+    assertNotNull(foList);
+    assertFalse(foList.isEmpty());
+    assertEquals(4, foList.size());
+    
+    for ( FileObject fo : foList ){
+      System.out.println(fo);
+    }
+  }
 
   /**
    * Method description