Sebastian Sdorra avatar Sebastian Sdorra committed 6be946b

implement recursive option for git browse command

Comments (0)

Files changed (2)

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBrowseCommand.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 import org.eclipse.jgit.errors.MissingObjectException;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
     if (revId != null)
     {
-      result = getResult(repo, request, revId, request.getPath());
+      result = getResult(repo, request, revId);
     }
     else
     {
       }
 
       result = new BrowserResult(Constants.HEAD, null, null,
-        new ArrayList<FileObject>());
+        Collections.EMPTY_LIST);
     }
 
     return result;
    *
    *
    * @param repo
+   * @param request
    * @param revId
    * @param treeWalk
    *
    *
    * @throws IOException
    */
-  private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request,
-    ObjectId revId, TreeWalk treeWalk)
+  private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
+    BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
     throws IOException
   {
     FileObject file = null;
       file.setLength(loader.getSize());
 
       // don't show message and date for directories to improve performance
-      if (!file.isDirectory() && ! request.isDisableLastCommit())
+      if (!file.isDirectory() &&!request.isDisableLastCommit())
       {
         logger.trace("fetch last commit for {} at {}", path, revId.getName());
+
         RevCommit commit = getLatestCommit(repo, revId, path);
 
         if (commit != null)
    *
    *
    * @param repo
+   * @param request
    * @param revId
    * @param path
    *
    * @throws IOException
    * @throws RepositoryException
    */
-  private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo, 
-    BrowseCommandRequest request, ObjectId revId, String path)
+  private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
+    BrowseCommandRequest request, ObjectId revId)
     throws IOException, RepositoryException
   {
     BrowserResult result = null;
       }
 
       treeWalk = new TreeWalk(repo);
+      treeWalk.setRecursive(request.isRecursive());
       revWalk = new RevWalk(repo);
 
       RevTree tree = revWalk.parseTree(revId);
 
       result = new BrowserResult();
 
-      List<FileObject> files = new ArrayList<FileObject>();
+      List<FileObject> files = Lists.newArrayList();
+
+      String path = request.getPath();
 
       appendSubModules(files, repo, revId, path);
 
           else if (name.equalsIgnoreCase(parts[current]))
           {
             current++;
-            treeWalk.enterSubtree();
+
+            if (!request.isRecursive())
+            {
+              treeWalk.enterSubtree();
+            }
           }
         }
       }

scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java

  */
 
 
+
 package sonia.scm.repository.spi;
 
 //~--- non-JDK imports --------------------------------------------------------
    * Method description
    *
    *
+   * @throws IOException
+   * @throws RepositoryException
+   */
+  @Test
+  public void testRecusive() 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(5, foList.size());
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @return
    */
   private GitBrowseCommand createCommand()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.