mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 03:25:56 +01:00
fix NullPointerException with empty git repositories, see issue #36
This commit is contained in:
@@ -110,11 +110,12 @@ public class GitChangesetViewer implements ChangesetViewer
|
||||
{
|
||||
gr = GitUtil.open(directory);
|
||||
|
||||
int counter = 0;
|
||||
List<Changeset> changesetList = new ArrayList<Changeset>();
|
||||
|
||||
if (!gr.getAllRefs().isEmpty())
|
||||
{
|
||||
Git git = new Git(gr);
|
||||
List<Changeset> changesetList = new ArrayList<Changeset>();
|
||||
int counter = 0;
|
||||
|
||||
treeWalk = new TreeWalk(gr);
|
||||
|
||||
@@ -129,9 +130,9 @@ public class GitChangesetViewer implements ChangesetViewer
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
changesets = new ChangesetPagingResult(counter, changesetList);
|
||||
}
|
||||
|
||||
changesets = new ChangesetPagingResult(counter, changesetList);
|
||||
}
|
||||
catch (NoHeadException ex)
|
||||
{
|
||||
|
||||
@@ -117,12 +117,25 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
|
||||
ObjectId revId = GitUtil.getRevisionId(repo, revision);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("load content for {} at {}", path, revId.name());
|
||||
}
|
||||
|
||||
revWalk = new RevWalk(repo);
|
||||
|
||||
RevCommit entry = revWalk.parseCommit(revId);
|
||||
RevTree revTree = entry.getTree();
|
||||
|
||||
treeWalk.addTree(revTree);
|
||||
if (revTree != null)
|
||||
{
|
||||
treeWalk.addTree(revTree);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("could not find tree for {}", revId.name());
|
||||
}
|
||||
|
||||
treeWalk.setFilter(PathFilter.create(path));
|
||||
|
||||
if (treeWalk.next())
|
||||
@@ -175,62 +188,33 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
BrowserResult result = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository repo = GitUtil.open(directory);
|
||||
RevWalk revWalk = null;
|
||||
TreeWalk treeWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
ObjectId revId = GitUtil.getRevisionId(repo, revision);
|
||||
|
||||
treeWalk = new TreeWalk(repo);
|
||||
revWalk = new RevWalk(repo);
|
||||
treeWalk.addTree(revWalk.parseTree(revId));
|
||||
result = new BrowserResult();
|
||||
|
||||
List<FileObject> files = new ArrayList<FileObject>();
|
||||
|
||||
if (Util.isEmpty(path))
|
||||
if (revId != null)
|
||||
{
|
||||
while (treeWalk.next())
|
||||
{
|
||||
files.add(createFileObject(repo, revId, treeWalk));
|
||||
}
|
||||
result = getResult(repo, revId, path);
|
||||
}
|
||||
else
|
||||
{
|
||||
String[] parts = path.split("/");
|
||||
int current = 0;
|
||||
int limit = parts.length;
|
||||
|
||||
while (treeWalk.next())
|
||||
if (Util.isNotEmpty(revision))
|
||||
{
|
||||
String name = treeWalk.getNameString();
|
||||
|
||||
if (current >= limit)
|
||||
{
|
||||
String p = treeWalk.getPathString();
|
||||
|
||||
if (p.split("/").length > limit)
|
||||
{
|
||||
files.add(createFileObject(repo, revId, treeWalk));
|
||||
}
|
||||
}
|
||||
else if (name.equalsIgnoreCase(parts[current]))
|
||||
{
|
||||
current++;
|
||||
treeWalk.enterSubtree();
|
||||
}
|
||||
logger.error("could not find revision {}", revision);
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("coul not find head of repository, empty?");
|
||||
}
|
||||
}
|
||||
|
||||
result.setFiles(files);
|
||||
result.setRevision(revId.getName());
|
||||
result = new BrowserResult(Constants.HEAD, null, null,
|
||||
new ArrayList<FileObject>());
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.close(repo);
|
||||
GitUtil.release(revWalk);
|
||||
GitUtil.release(treeWalk);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -328,6 +312,97 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revId
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
|
||||
ObjectId revId, String path)
|
||||
throws IOException
|
||||
{
|
||||
BrowserResult result = null;
|
||||
RevWalk revWalk = null;
|
||||
TreeWalk treeWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("load repository browser for revision {}", revId.name());
|
||||
}
|
||||
|
||||
treeWalk = new TreeWalk(repo);
|
||||
revWalk = new RevWalk(repo);
|
||||
|
||||
RevTree tree = revWalk.parseTree(revId);
|
||||
|
||||
if (tree != null)
|
||||
{
|
||||
treeWalk.addTree(tree);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("could not find tree for {}", revId.name());
|
||||
}
|
||||
|
||||
result = new BrowserResult();
|
||||
|
||||
List<FileObject> files = new ArrayList<FileObject>();
|
||||
|
||||
if (Util.isEmpty(path))
|
||||
{
|
||||
while (treeWalk.next())
|
||||
{
|
||||
files.add(createFileObject(repo, revId, treeWalk));
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
files.add(createFileObject(repo, revId, treeWalk));
|
||||
}
|
||||
}
|
||||
else if (name.equalsIgnoreCase(parts[current]))
|
||||
{
|
||||
current++;
|
||||
treeWalk.enterSubtree();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.setFiles(files);
|
||||
result.setRevision(revId.getName());
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.release(revWalk);
|
||||
GitUtil.release(treeWalk);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -35,20 +35,20 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -71,6 +71,23 @@ public class GitUtil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static org.eclipse.jgit.lib.Repository open(File directory)
|
||||
throws IOException
|
||||
{
|
||||
return RepositoryCache.open(RepositoryCache.FileKey.lenient(directory,
|
||||
FS.DETECTED), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -118,24 +135,6 @@ public class GitUtil
|
||||
return date;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static org.eclipse.jgit.lib.Repository open(File directory)
|
||||
throws IOException
|
||||
{
|
||||
return RepositoryCache.open(RepositoryCache.FileKey.lenient(directory,
|
||||
FS.DETECTED), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user