From: James Moger Date: Thu, 17 Nov 2011 03:39:28 +0000 (-0500) Subject: New JGit utility function to retrieve commits since a specific date X-Git-Tag: v0.8.0~94 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2f1c776719cf3817a118ab930547e46301fad738;p=gitblit.git New JGit utility function to retrieve commits since a specific date --- diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index c61b3d9f..99c2d0a8 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -62,6 +62,7 @@ import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevSort; import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter; import org.eclipse.jgit.revwalk.filter.RevFilter; import org.eclipse.jgit.storage.file.FileRepository; import org.eclipse.jgit.transport.CredentialsProvider; @@ -823,6 +824,45 @@ public class JGitUtils { return "missing"; } + /** + * Returns a list of commits since the minimum date starting from the + * specified object id. + * + * @param repository + * @param objectId + * if unspecified, HEAD is assumed. + * @param minimumDate + * @return list of commits + */ + public static List getRevLog(Repository repository, String objectId, Date minimumDate) { + List list = new ArrayList(); + if (!hasCommits(repository)) { + return list; + } + try { + // resolve branch + ObjectId branchObject; + if (StringUtils.isEmpty(objectId)) { + branchObject = getDefaultBranch(repository); + } else { + branchObject = repository.resolve(objectId); + } + + RevWalk rw = new RevWalk(repository); + rw.markStart(rw.parseCommit(branchObject)); + rw.setRevFilter(CommitTimeRevFilter.after(minimumDate)); + Iterable revlog = rw; + for (RevCommit rev : revlog) { + list.add(rev); + } + rw.dispose(); + } catch (Throwable t) { + error(t, repository, "{0} failed to get {1} revlog for minimum date {2}", objectId, + minimumDate); + } + return list; + } + /** * Returns a list of commits starting from HEAD and working backwards. * diff --git a/tests/com/gitblit/tests/JGitUtilsTest.java b/tests/com/gitblit/tests/JGitUtilsTest.java index 594d090d..7cd79adc 100644 --- a/tests/com/gitblit/tests/JGitUtilsTest.java +++ b/tests/com/gitblit/tests/JGitUtilsTest.java @@ -17,6 +17,7 @@ package com.gitblit.tests; import java.io.File; import java.io.FileOutputStream; +import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -287,20 +288,24 @@ public class JGitUtilsTest extends TestCase { public void testRevlog() throws Exception { assertTrue(JGitUtils.getRevLog(null, 0).size() == 0); List commits = JGitUtils.getRevLog(null, 10); - assertTrue(commits.size() == 0); + assertEquals(0, commits.size()); Repository repository = GitBlitSuite.getHelloworldRepository(); // get most recent 10 commits commits = JGitUtils.getRevLog(repository, 10); - assertTrue(commits.size() == 10); + assertEquals(10, commits.size()); // test paging and offset by getting the 10th most recent commit RevCommit lastCommit = JGitUtils.getRevLog(repository, null, 9, 1).get(0); - assertTrue(commits.get(9).equals(lastCommit)); + assertEquals(lastCommit, commits.get(9)); // grab the two most recent commits to java.java commits = JGitUtils.getRevLog(repository, null, "java.java", 0, 2); - assertTrue(commits.size() == 2); + assertEquals(2, commits.size()); + + // grab the commits since 2008-07-15 + commits = JGitUtils.getRevLog(repository, null, new SimpleDateFormat("yyyy-MM-dd").parse("2008-07-15")); + assertEquals(12, commits.size()); repository.close(); }