mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
implement new browse api for git
This commit is contained in:
@@ -35,6 +35,9 @@ package sonia.scm.repository.spi;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.collect.Lists;
|
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;
|
||||||
@@ -61,6 +64,7 @@ import sonia.scm.repository.SubRepository;
|
|||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -107,6 +111,7 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
logger.debug("try to create browse result for {}", request);
|
logger.debug("try to create browse result for {}", request);
|
||||||
|
|
||||||
BrowserResult result;
|
BrowserResult result;
|
||||||
|
|
||||||
org.eclipse.jgit.lib.Repository repo = open();
|
org.eclipse.jgit.lib.Repository repo = open();
|
||||||
ObjectId revId;
|
ObjectId revId;
|
||||||
|
|
||||||
@@ -121,7 +126,7 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
|
|
||||||
if (revId != null)
|
if (revId != null)
|
||||||
{
|
{
|
||||||
result = getResult(repo, request, revId);
|
result = new BrowserResult(revId.getName(), getEntry(repo, request, revId));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -134,8 +139,7 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
logger.warn("coul not find head of repository, empty?");
|
logger.warn("coul not find head of repository, empty?");
|
||||||
}
|
}
|
||||||
|
|
||||||
result = new BrowserResult(Constants.HEAD, null, null,
|
result = new BrowserResult(Constants.HEAD, createEmtpyRoot());
|
||||||
Collections.EMPTY_LIST);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -143,6 +147,14 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
private FileObject createEmtpyRoot() {
|
||||||
|
FileObject fileObject = new FileObject();
|
||||||
|
fileObject.setName("");
|
||||||
|
fileObject.setPath("");
|
||||||
|
fileObject.setDirectory(true);
|
||||||
|
return fileObject;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -193,7 +205,6 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
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)
|
||||||
@@ -265,22 +276,19 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
|
private FileObject getEntry(org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId) throws IOException, RevisionNotFoundException {
|
||||||
BrowseCommandRequest request, ObjectId revId)
|
|
||||||
throws IOException, RevisionNotFoundException {
|
|
||||||
BrowserResult result = null;
|
|
||||||
RevWalk revWalk = null;
|
RevWalk revWalk = null;
|
||||||
TreeWalk treeWalk = null;
|
TreeWalk treeWalk = null;
|
||||||
|
|
||||||
try
|
FileObject result;
|
||||||
{
|
|
||||||
if (logger.isDebugEnabled())
|
try {
|
||||||
{
|
|
||||||
logger.debug("load repository browser for revision {}", revId.name());
|
logger.debug("load repository browser for revision {}", revId.name());
|
||||||
}
|
|
||||||
|
|
||||||
treeWalk = new TreeWalk(repo);
|
treeWalk = new TreeWalk(repo);
|
||||||
treeWalk.setRecursive(request.isRecursive());
|
if (!Strings.isNullOrEmpty(request.getPath())) {
|
||||||
|
treeWalk.setFilter(PathFilter.create(request.getPath()));
|
||||||
|
}
|
||||||
revWalk = new RevWalk(repo);
|
revWalk = new RevWalk(repo);
|
||||||
|
|
||||||
RevTree tree = revWalk.parseTree(revId);
|
RevTree tree = revWalk.parseTree(revId);
|
||||||
@@ -291,65 +299,21 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// TODO throw exception
|
||||||
logger.error("could not find tree for {}", revId.name());
|
logger.error("could not find tree for {}", revId.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
result = new BrowserResult();
|
if (Strings.isNullOrEmpty(request.getPath())) {
|
||||||
|
result = createEmtpyRoot();
|
||||||
List<FileObject> files = Lists.newArrayList();
|
findChildren(result, repo, request, revId, treeWalk);
|
||||||
|
} else {
|
||||||
String path = request.getPath();
|
result = first(repo, request, revId, treeWalk);
|
||||||
|
if ( result.isDirectory() ) {
|
||||||
if (Util.isEmpty(path))
|
|
||||||
{
|
|
||||||
while (treeWalk.next())
|
|
||||||
{
|
|
||||||
FileObject fo = createFileObject(repo, request, revId, treeWalk);
|
|
||||||
|
|
||||||
if (fo != null)
|
|
||||||
{
|
|
||||||
files.add(fo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
String[] parts = path.split("/");
|
|
||||||
int current = 0;
|
|
||||||
int limit = parts.length;
|
|
||||||
|
|
||||||
while (treeWalk.next())
|
|
||||||
{
|
|
||||||
String name = treeWalk.getNameString();
|
|
||||||
|
|
||||||
if (current >= limit)
|
|
||||||
{
|
|
||||||
String p = treeWalk.getPathString();
|
|
||||||
|
|
||||||
if (p.split("/").length > limit)
|
|
||||||
{
|
|
||||||
FileObject fo = createFileObject(repo, request, revId, treeWalk);
|
|
||||||
|
|
||||||
if (fo != null)
|
|
||||||
{
|
|
||||||
files.add(fo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (name.equalsIgnoreCase(parts[current]))
|
|
||||||
{
|
|
||||||
current++;
|
|
||||||
|
|
||||||
if (!request.isRecursive())
|
|
||||||
{
|
|
||||||
treeWalk.enterSubtree();
|
treeWalk.enterSubtree();
|
||||||
}
|
findChildren(result, repo, request, revId, treeWalk);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.setFiles(files);
|
|
||||||
result.setRevision(revId.getName());
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -360,6 +324,45 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private FileObject findChildren(FileObject parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk) throws IOException, RevisionNotFoundException {
|
||||||
|
List<FileObject> files = Lists.newArrayList();
|
||||||
|
while (treeWalk.next())
|
||||||
|
{
|
||||||
|
|
||||||
|
FileObject fo = createFileObject(repo, request, revId, treeWalk);
|
||||||
|
if (!fo.getPath().startsWith(parent.getPath())) {
|
||||||
|
parent.setChildren(files);
|
||||||
|
return fo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (fo != null)
|
||||||
|
{
|
||||||
|
files.add(fo);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.isRecursive() && fo.isDirectory()) {
|
||||||
|
treeWalk.enterSubtree();
|
||||||
|
FileObject rc = findChildren(fo, repo, request, revId, treeWalk);
|
||||||
|
if (rc != null) {
|
||||||
|
files.add(rc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent.setChildren(files);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileObject first(org.eclipse.jgit.lib.Repository repo,
|
||||||
|
BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk) throws IOException, RevisionNotFoundException {
|
||||||
|
if (!treeWalk.next()) {
|
||||||
|
throw new IOException("tree seams to be empty");
|
||||||
|
}
|
||||||
|
return createFileObject(repo, request, revId, treeWalk);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Map<String,
|
private Map<String,
|
||||||
SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
|
SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
|
||||||
|
|||||||
@@ -59,18 +59,28 @@ import static org.junit.Assert.assertTrue;
|
|||||||
public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetFile() throws IOException, RevisionNotFoundException {
|
||||||
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
|
request.setPath("a.txt");
|
||||||
|
BrowserResult result = createCommand().getBrowserResult(request);
|
||||||
|
FileObject fileObject = result.getFile();
|
||||||
|
assertEquals("a.txt", fileObject.getName());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test browse command with default branch.
|
* Test browse command with default branch.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultBranch() throws IOException, RevisionNotFoundException {
|
public void testDefaultBranch() throws IOException, RevisionNotFoundException {
|
||||||
// without default branch, the repository head should be used
|
// without default branch, the repository head should be used
|
||||||
BrowserResult result = createCommand().getBrowserResult(new BrowseCommandRequest());
|
FileObject root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile();
|
||||||
assertNotNull(result);
|
assertNotNull(root);
|
||||||
|
|
||||||
List<FileObject> foList = result.getFiles();
|
List<FileObject> foList = root.getChildren();
|
||||||
assertNotNull(foList);
|
assertNotNull(foList);
|
||||||
assertFalse(foList.isEmpty());
|
assertFalse(foList.isEmpty());
|
||||||
|
|
||||||
assertEquals(4, foList.size());
|
assertEquals(4, foList.size());
|
||||||
|
|
||||||
assertEquals("a.txt", foList.get(0).getName());
|
assertEquals("a.txt", foList.get(0).getName());
|
||||||
@@ -80,10 +90,11 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
|||||||
|
|
||||||
// set default branch and fetch again
|
// set default branch and fetch again
|
||||||
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
|
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
|
||||||
result = createCommand().getBrowserResult(new BrowseCommandRequest());
|
|
||||||
assertNotNull(result);
|
|
||||||
|
|
||||||
foList = result.getFiles();
|
root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile();
|
||||||
|
assertNotNull(root);
|
||||||
|
|
||||||
|
foList = root.getChildren();
|
||||||
assertNotNull(foList);
|
assertNotNull(foList);
|
||||||
assertFalse(foList.isEmpty());
|
assertFalse(foList.isEmpty());
|
||||||
assertEquals(2, foList.size());
|
assertEquals(2, foList.size());
|
||||||
@@ -94,12 +105,10 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBrowse() throws IOException, RevisionNotFoundException {
|
public void testBrowse() throws IOException, RevisionNotFoundException {
|
||||||
BrowserResult result =
|
FileObject root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile();
|
||||||
createCommand().getBrowserResult(new BrowseCommandRequest());
|
assertNotNull(root);
|
||||||
|
|
||||||
assertNotNull(result);
|
List<FileObject> foList = root.getChildren();
|
||||||
|
|
||||||
List<FileObject> foList = result.getFiles();
|
|
||||||
|
|
||||||
assertNotNull(foList);
|
assertNotNull(foList);
|
||||||
assertFalse(foList.isEmpty());
|
assertFalse(foList.isEmpty());
|
||||||
@@ -139,11 +148,11 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
|||||||
|
|
||||||
request.setPath("c");
|
request.setPath("c");
|
||||||
|
|
||||||
BrowserResult result = createCommand().getBrowserResult(request);
|
FileObject root = createCommand().getBrowserResult(request).getFile();
|
||||||
|
assertNotNull(root);
|
||||||
|
|
||||||
assertNotNull(result);
|
|
||||||
|
|
||||||
List<FileObject> foList = result.getFiles();
|
List<FileObject> foList = root.getChildren();
|
||||||
|
|
||||||
assertNotNull(foList);
|
assertNotNull(foList);
|
||||||
assertFalse(foList.isEmpty());
|
assertFalse(foList.isEmpty());
|
||||||
@@ -181,20 +190,31 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRecusive() throws IOException, RevisionNotFoundException {
|
public void testRecursive() throws IOException, RevisionNotFoundException {
|
||||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
|
|
||||||
request.setRecursive(true);
|
request.setRecursive(true);
|
||||||
|
|
||||||
BrowserResult result = createCommand().getBrowserResult(request);
|
FileObject root = createCommand().getBrowserResult(request).getFile();
|
||||||
|
assertNotNull(root);
|
||||||
|
|
||||||
assertNotNull(result);
|
|
||||||
|
|
||||||
List<FileObject> foList = result.getFiles();
|
List<FileObject> foList = root.getChildren();
|
||||||
|
|
||||||
assertNotNull(foList);
|
assertNotNull(foList);
|
||||||
assertFalse(foList.isEmpty());
|
assertFalse(foList.isEmpty());
|
||||||
assertEquals(5, foList.size());
|
|
||||||
|
assertEquals(4, foList.size());
|
||||||
|
|
||||||
|
assertEquals("a.txt", foList.get(0).getName());
|
||||||
|
assertEquals("b.txt", foList.get(1).getName());
|
||||||
|
FileObject c = foList.get(2);
|
||||||
|
assertEquals("c", c.getName());
|
||||||
|
assertEquals("f.txt", foList.get(3).getName());
|
||||||
|
|
||||||
|
List<FileObject> cChilds = c.getChildren();
|
||||||
|
assertEquals("d.txt", cChilds.get(0).getName());
|
||||||
|
assertEquals("e.txt", cChilds.get(1).getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user