fetch latest revcommit

This commit is contained in:
Sebastian Sdorra
2011-06-21 17:41:05 +02:00
parent 3c367d225b
commit 0a343159a9
3 changed files with 131 additions and 10 deletions

View File

@@ -202,9 +202,7 @@ public class GitChangesetViewer implements ChangesetViewer
throws IOException
{
String id = commit.getId().abbreviate(ID_LENGTH).name();
long date = commit.getCommitTime();
date = date * 1000;
long date = GitUtil.getCommitTime(commit);
PersonIdent authorIndent = commit.getCommitterIdent();
Person author = new Person(authorIndent.getName(),

View File

@@ -35,6 +35,7 @@ package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.lib.Constants;
@@ -48,6 +49,9 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.util.FS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
@@ -57,6 +61,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
@@ -66,6 +71,12 @@ import java.util.List;
public class GitRepositoryBrowser implements RepositoryBrowser
{
/** the logger for GitRepositoryBrowser */
private static final Logger logger =
LoggerFactory.getLogger(GitRepositoryBrowser.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
@@ -163,6 +174,7 @@ public class GitRepositoryBrowser implements RepositoryBrowser
BrowserResult result = null;
File directory = handler.getDirectory(repository);
org.eclipse.jgit.lib.Repository repo = open(directory);
Git git = new Git(repo);
TreeWalk treeWalk = null;
try
@@ -179,7 +191,7 @@ public class GitRepositoryBrowser implements RepositoryBrowser
while (treeWalk.next())
{
files.add(createFileObject(repo, treeWalk));
files.add(createFileObject(git, revId, treeWalk));
}
result.setFiles(files);
@@ -214,29 +226,41 @@ public class GitRepositoryBrowser implements RepositoryBrowser
* Method description
*
*
* @param repo
*
* @param git
* @param revId
* @param treeWalk
*
* @return
*
* @throws IOException
*/
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
private FileObject createFileObject(Git git, ObjectId revId,
TreeWalk treeWalk)
throws IOException
{
FileObject file = new FileObject();
String path = treeWalk.getPathString();
file.setName(treeWalk.getNameString());
file.setPath(treeWalk.getPathString());
file.setPath(path);
ObjectLoader loader = repo.open(treeWalk.getObjectId(0));
ObjectLoader loader = git.getRepository().open(treeWalk.getObjectId(0));
file.setDirectory(loader.getType() == Constants.OBJ_TREE);
file.setLength(loader.getSize());
// TODO
file.setLastModified(System.currentTimeMillis());
RevCommit commit = getLatestCommit(git, revId, path);
if (commit != null)
{
file.setLastModified(GitUtil.getCommitTime(commit));
file.setDescription(commit.getShortMessage());
}
else if (logger.isWarnEnabled())
{
logger.warn("could not find latest commit for {} on {}", path, revId);
}
return file;
}
@@ -274,6 +298,42 @@ public class GitRepositoryBrowser implements RepositoryBrowser
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param git
* @param revId
* @param path
*
* @return
*/
private RevCommit getLatestCommit(Git git, ObjectId revId, String path)
{
RevCommit commit = null;
try
{
Iterable<RevCommit> iterable = git.log().add(revId).addPath(path).call();
if (iterable != null)
{
Iterator<RevCommit> it = iterable.iterator();
if ((it != null) && it.hasNext())
{
commit = it.next();
}
}
}
catch (Exception ex)
{
logger.error("could not fetch latest commit", ex);
}
return commit;
}
/**
* Method description
*

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import org.eclipse.jgit.revwalk.RevCommit;
/**
*
* @author Sebastian Sdorra
*/
public class GitUtil
{
/**
* Method description
*
*
* @param commit
*
* @return
*/
public static long getCommitTime(RevCommit commit)
{
long date = commit.getCommitTime();
date = date * 1000;
return date;
}
}