mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-30 03:09:13 +01:00
improve GitUtil
This commit is contained in:
@@ -41,7 +41,6 @@ import org.eclipse.jgit.dircache.DirCacheIterator;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevTree;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
@@ -52,8 +51,6 @@ import org.eclipse.jgit.util.FS;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
@@ -110,15 +107,18 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository repo = open(directory);
|
||||
org.eclipse.jgit.lib.Repository repo = GitUtil.open(directory);
|
||||
TreeWalk treeWalk = null;
|
||||
RevWalk revWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
treeWalk = new TreeWalk(repo);
|
||||
|
||||
ObjectId revId = getRevisionId(repo, revision);
|
||||
RevWalk revWalk = new RevWalk(repo);
|
||||
ObjectId revId = GitUtil.getRevisionId(repo, revision);
|
||||
|
||||
revWalk = new RevWalk(repo);
|
||||
|
||||
RevCommit entry = revWalk.parseCommit(revId);
|
||||
RevTree revTree = entry.getTree();
|
||||
|
||||
@@ -150,8 +150,9 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
}
|
||||
finally
|
||||
{
|
||||
close(repo);
|
||||
release(treeWalk);
|
||||
GitUtil.release(revWalk);
|
||||
GitUtil.release(treeWalk);
|
||||
GitUtil.close(repo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,13 +174,13 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
{
|
||||
BrowserResult result = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository repo = open(directory);
|
||||
org.eclipse.jgit.lib.Repository repo = GitUtil.open(directory);
|
||||
Git git = new Git(repo);
|
||||
TreeWalk treeWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
ObjectId revId = getRevisionId(repo, revision);
|
||||
ObjectId revId = GitUtil.getRevisionId(repo, revision);
|
||||
DirCache cache = new DirCache(directory, FS.DETECTED);
|
||||
|
||||
treeWalk = new TreeWalk(repo);
|
||||
@@ -199,8 +200,8 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
}
|
||||
finally
|
||||
{
|
||||
close(repo);
|
||||
release(treeWalk);
|
||||
GitUtil.close(repo);
|
||||
GitUtil.release(treeWalk);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -208,20 +209,6 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
*/
|
||||
private void close(org.eclipse.jgit.lib.Repository repo)
|
||||
{
|
||||
if (repo != null)
|
||||
{
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -265,37 +252,6 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private org.eclipse.jgit.lib.Repository open(File directory)
|
||||
throws IOException
|
||||
{
|
||||
return RepositoryCache.open(RepositoryCache.FileKey.lenient(directory,
|
||||
FS.DETECTED), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param walk
|
||||
*/
|
||||
private void release(TreeWalk walk)
|
||||
{
|
||||
if (walk != null)
|
||||
{
|
||||
walk.release();
|
||||
}
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -334,35 +290,6 @@ public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
return commit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revision
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private ObjectId getRevisionId(org.eclipse.jgit.lib.Repository repo,
|
||||
String revision)
|
||||
throws IOException
|
||||
{
|
||||
ObjectId revId = null;
|
||||
|
||||
if (Util.isNotEmpty(revision))
|
||||
{
|
||||
revId = repo.resolve(revision);
|
||||
}
|
||||
else
|
||||
{
|
||||
revId = repo.resolve(Constants.HEAD);
|
||||
}
|
||||
|
||||
return revId;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -35,7 +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.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -44,6 +57,50 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
||||
public class GitUtil
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
*/
|
||||
public static void close(org.eclipse.jgit.lib.Repository repo)
|
||||
{
|
||||
if (repo != null)
|
||||
{
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param walk
|
||||
*/
|
||||
public static void release(TreeWalk walk)
|
||||
{
|
||||
if (walk != null)
|
||||
{
|
||||
walk.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param walk
|
||||
*/
|
||||
public static void release(RevWalk walk)
|
||||
{
|
||||
if (walk != null)
|
||||
{
|
||||
walk.release();
|
||||
}
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -60,4 +117,51 @@ 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
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revision
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static ObjectId getRevisionId(org.eclipse.jgit.lib.Repository repo,
|
||||
String revision)
|
||||
throws IOException
|
||||
{
|
||||
ObjectId revId = null;
|
||||
|
||||
if (Util.isNotEmpty(revision))
|
||||
{
|
||||
revId = repo.resolve(revision);
|
||||
}
|
||||
else
|
||||
{
|
||||
revId = repo.resolve(Constants.HEAD);
|
||||
}
|
||||
|
||||
return revId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user