From 0a343159a9e04c33281d569b554da0faa85e3278 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Tue, 21 Jun 2011 17:41:05 +0200 Subject: [PATCH] fetch latest revcommit --- .../scm/repository/GitChangesetViewer.java | 4 +- .../scm/repository/GitRepositoryBrowser.java | 74 +++++++++++++++++-- .../java/sonia/scm/repository/GitUtil.java | 63 ++++++++++++++++ 3 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java diff --git a/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java b/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java index f87683c8f4..951c3d9967 100644 --- a/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java +++ b/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java @@ -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(), diff --git a/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryBrowser.java b/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryBrowser.java index 9a41c2c47e..680c15d4e3 100644 --- a/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryBrowser.java +++ b/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryBrowser.java @@ -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 iterable = git.log().add(revId).addPath(path).call(); + + if (iterable != null) + { + Iterator 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 * diff --git a/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java b/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java new file mode 100644 index 0000000000..d1eaaba6dc --- /dev/null +++ b/plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java @@ -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; + } +}