summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2011-11-16 22:39:28 -0500
committerJames Moger <james.moger@gitblit.com>2011-11-16 22:39:28 -0500
commit2f1c776719cf3817a118ab930547e46301fad738 (patch)
tree40f745c33d20be6dee59d7712434a06fee26a2a2
parente19d3da3cdb4e3d0524e3a3ffc6a2cfa8876e927 (diff)
downloadgitblit-2f1c776719cf3817a118ab930547e46301fad738.tar.gz
gitblit-2f1c776719cf3817a118ab930547e46301fad738.zip
New JGit utility function to retrieve commits since a specific date
-rw-r--r--src/com/gitblit/utils/JGitUtils.java40
-rw-r--r--tests/com/gitblit/tests/JGitUtilsTest.java13
2 files changed, 49 insertions, 4 deletions
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;
@@ -824,6 +825,45 @@ public class JGitUtils {
}
/**
+ * 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<RevCommit> getRevLog(Repository repository, String objectId, Date minimumDate) {
+ List<RevCommit> list = new ArrayList<RevCommit>();
+ 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<RevCommit> 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.
*
* @param repository
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<RevCommit> 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();
}