]> source.dussan.org Git - gitblit.git/commitdiff
New JGit utility function to retrieve commits since a specific date
authorJames Moger <james.moger@gitblit.com>
Thu, 17 Nov 2011 03:39:28 +0000 (22:39 -0500)
committerJames Moger <james.moger@gitblit.com>
Thu, 17 Nov 2011 03:39:28 +0000 (22:39 -0500)
src/com/gitblit/utils/JGitUtils.java
tests/com/gitblit/tests/JGitUtilsTest.java

index c61b3d9f757981d5072a205409c40602bbf40b31..99c2d0a86f408e9bf7b35ff88483148e7cf8058a 100644 (file)
@@ -62,6 +62,7 @@ import org.eclipse.jgit.revwalk.RevObject;
 import org.eclipse.jgit.revwalk.RevSort;\r
 import org.eclipse.jgit.revwalk.RevTree;\r
 import org.eclipse.jgit.revwalk.RevWalk;\r
+import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;\r
 import org.eclipse.jgit.revwalk.filter.RevFilter;\r
 import org.eclipse.jgit.storage.file.FileRepository;\r
 import org.eclipse.jgit.transport.CredentialsProvider;\r
@@ -823,6 +824,45 @@ public class JGitUtils {
                return "missing";\r
        }\r
 \r
+       /**\r
+        * Returns a list of commits since the minimum date starting from the\r
+        * specified object id.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        *            if unspecified, HEAD is assumed.\r
+        * @param minimumDate\r
+        * @return list of commits\r
+        */\r
+       public static List<RevCommit> getRevLog(Repository repository, String objectId, Date minimumDate) {\r
+               List<RevCommit> list = new ArrayList<RevCommit>();\r
+               if (!hasCommits(repository)) {\r
+                       return list;\r
+               }\r
+               try {\r
+                       // resolve branch\r
+                       ObjectId branchObject;\r
+                       if (StringUtils.isEmpty(objectId)) {\r
+                               branchObject = getDefaultBranch(repository);\r
+                       } else {\r
+                               branchObject = repository.resolve(objectId);\r
+                       }\r
+\r
+                       RevWalk rw = new RevWalk(repository);\r
+                       rw.markStart(rw.parseCommit(branchObject));\r
+                       rw.setRevFilter(CommitTimeRevFilter.after(minimumDate));\r
+                       Iterable<RevCommit> revlog = rw;\r
+                       for (RevCommit rev : revlog) {\r
+                               list.add(rev);\r
+                       }\r
+                       rw.dispose();\r
+               } catch (Throwable t) {\r
+                       error(t, repository, "{0} failed to get {1} revlog for minimum date {2}", objectId,\r
+                                       minimumDate);\r
+               }\r
+               return list;\r
+       }\r
+\r
        /**\r
         * Returns a list of commits starting from HEAD and working backwards.\r
         * \r
index 594d090d1df75120034ca1bf7a31f6321dfe8f75..7cd79adcdac5e7d5cd38eceaa457a20740a16ffd 100644 (file)
@@ -17,6 +17,7 @@ package com.gitblit.tests;
 \r
 import java.io.File;\r
 import java.io.FileOutputStream;\r
+import java.text.SimpleDateFormat;\r
 import java.util.Arrays;\r
 import java.util.Date;\r
 import java.util.List;\r
@@ -287,20 +288,24 @@ public class JGitUtilsTest extends TestCase {
        public void testRevlog() throws Exception {\r
                assertTrue(JGitUtils.getRevLog(null, 0).size() == 0);\r
                List<RevCommit> commits = JGitUtils.getRevLog(null, 10);\r
-               assertTrue(commits.size() == 0);\r
+               assertEquals(0, commits.size());\r
 \r
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
                // get most recent 10 commits\r
                commits = JGitUtils.getRevLog(repository, 10);\r
-               assertTrue(commits.size() == 10);\r
+               assertEquals(10, commits.size());\r
 \r
                // test paging and offset by getting the 10th most recent commit\r
                RevCommit lastCommit = JGitUtils.getRevLog(repository, null, 9, 1).get(0);\r
-               assertTrue(commits.get(9).equals(lastCommit));\r
+               assertEquals(lastCommit, commits.get(9));\r
 \r
                // grab the two most recent commits to java.java\r
                commits = JGitUtils.getRevLog(repository, null, "java.java", 0, 2);\r
-               assertTrue(commits.size() == 2);\r
+               assertEquals(2, commits.size());\r
+               \r
+               // grab the commits since 2008-07-15\r
+               commits = JGitUtils.getRevLog(repository, null, new SimpleDateFormat("yyyy-MM-dd").parse("2008-07-15"));\r
+               assertEquals(12, commits.size());\r
                repository.close();\r
        }\r
 \r