implement recursive option for git browse command

This commit is contained in:
Sebastian Sdorra
2013-01-19 13:09:58 +01:00
parent ab85fb9bf6
commit cc4604b60f
2 changed files with 46 additions and 10 deletions

View File

@@ -35,6 +35,7 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.MissingObjectException;
@@ -67,7 +68,6 @@ import sonia.scm.util.Util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -143,7 +143,7 @@ public class GitBrowseCommand extends AbstractGitCommand
if (revId != null) if (revId != null)
{ {
result = getResult(repo, request, revId, request.getPath()); result = getResult(repo, request, revId);
} }
else else
{ {
@@ -157,7 +157,7 @@ public class GitBrowseCommand extends AbstractGitCommand
} }
result = new BrowserResult(Constants.HEAD, null, null, result = new BrowserResult(Constants.HEAD, null, null,
new ArrayList<FileObject>()); Collections.EMPTY_LIST);
} }
return result; return result;
@@ -233,6 +233,7 @@ public class GitBrowseCommand extends AbstractGitCommand
* *
* *
* @param repo * @param repo
* @param request
* @param revId * @param revId
* @param treeWalk * @param treeWalk
* *
@@ -240,8 +241,8 @@ public class GitBrowseCommand extends AbstractGitCommand
* *
* @throws IOException * @throws IOException
*/ */
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
ObjectId revId, TreeWalk treeWalk) BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
throws IOException throws IOException
{ {
FileObject file = null; FileObject file = null;
@@ -261,9 +262,10 @@ public class GitBrowseCommand extends AbstractGitCommand
file.setLength(loader.getSize()); file.setLength(loader.getSize());
// don't show message and date for directories to improve performance // 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()); logger.trace("fetch last commit for {} at {}", path, revId.getName());
RevCommit commit = getLatestCommit(repo, revId, path); RevCommit commit = getLatestCommit(repo, revId, path);
if (commit != null) if (commit != null)
@@ -338,6 +340,7 @@ public class GitBrowseCommand extends AbstractGitCommand
* *
* *
* @param repo * @param repo
* @param request
* @param revId * @param revId
* @param path * @param path
* *
@@ -347,7 +350,7 @@ public class GitBrowseCommand extends AbstractGitCommand
* @throws RepositoryException * @throws RepositoryException
*/ */
private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo, private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
BrowseCommandRequest request, ObjectId revId, String path) BrowseCommandRequest request, ObjectId revId)
throws IOException, RepositoryException throws IOException, RepositoryException
{ {
BrowserResult result = null; BrowserResult result = null;
@@ -362,6 +365,7 @@ public class GitBrowseCommand extends AbstractGitCommand
} }
treeWalk = new TreeWalk(repo); treeWalk = new TreeWalk(repo);
treeWalk.setRecursive(request.isRecursive());
revWalk = new RevWalk(repo); revWalk = new RevWalk(repo);
RevTree tree = revWalk.parseTree(revId); RevTree tree = revWalk.parseTree(revId);
@@ -377,7 +381,9 @@ public class GitBrowseCommand extends AbstractGitCommand
result = new BrowserResult(); result = new BrowserResult();
List<FileObject> files = new ArrayList<FileObject>(); List<FileObject> files = Lists.newArrayList();
String path = request.getPath();
appendSubModules(files, repo, revId, path); appendSubModules(files, repo, revId, path);
@@ -420,7 +426,11 @@ public class GitBrowseCommand extends AbstractGitCommand
else if (name.equalsIgnoreCase(parts[current])) else if (name.equalsIgnoreCase(parts[current]))
{ {
current++; current++;
treeWalk.enterSubtree();
if (!request.isRecursive())
{
treeWalk.enterSubtree();
}
} }
} }
} }

View File

@@ -30,6 +30,7 @@
*/ */
package sonia.scm.repository.spi; package sonia.scm.repository.spi;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
@@ -159,6 +160,31 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
checkDate(e.getLastModified()); checkDate(e.getLastModified());
} }
/**
* 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 * Method description
* *