]> source.dussan.org Git - gitblit.git/commitdiff
Fixed sendemail script to list all commits in a push.
authorJames Moger <james.moger@gitblit.com>
Sat, 17 Dec 2011 19:03:35 +0000 (14:03 -0500)
committerJames Moger <james.moger@gitblit.com>
Sat, 17 Dec 2011 19:03:35 +0000 (14:03 -0500)
groovy/sendemail.groovy
src/com/gitblit/utils/JGitUtils.java
tests/com/gitblit/tests/GitServletTest.java
tests/com/gitblit/tests/JGitUtilsTest.java

index 1ba72a8a255f481f4defafdf3e129a998d578064..29c5e5afca10e6d04d4b15cb4456e48c508a4cde 100644 (file)
@@ -93,20 +93,42 @@ switch(repository.name) {
                toAddresses.add "dev-team@somewhere.com"\r
                toAddresses.add "qa-team@somewhere.com"\r
                break\r
-       default:                \r
-               break\r
 }\r
 \r
-// get the create/update commits from the repository to build message content\r
-def commits = []\r
-for (ReceiveCommand command:commands) {\r
+// construct a simple text summary of the changes contained in the push\r
+def commitCount = 0\r
+def changes = ""\r
+def table = { it.id.name[0..8] + " " + it.authorIdent.name.padRight(20, " ") + it.shortMessage }\r
+for (command in commands) {\r
        switch (command.type) {\r
-               case ReceiveCommand.Type.UPDATE:\r
                case ReceiveCommand.Type.CREATE:\r
-                       RevCommit commit = JGitUtils.getCommit(r, command.newId.name)\r
-                       commits.add(commit)\r
+                       def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)\r
+                       commitCount += commits.size()\r
+                       // new branch commits table\r
+                       changes += "created ${command.refName}\n\n"\r
+                       changes += commits.collect(table).join("\n")\r
+                       changes += "\n"\r
+                       break\r
+               case ReceiveCommand.Type.UPDATE:\r
+                       def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)\r
+                       commitCount += commits.size()\r
+                       // fast-forward branch commits table\r
+                       changes += "updated ${command.refName}\n\n"\r
+                       changes += commits.collect(table).join("\n")\r
+                       changes += "\n"\r
+                       break\r
+               case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:\r
+                       def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)\r
+                       commitCount += commits.size()\r
+                       // non-fast-forward branch commits table\r
+                       changes += "updated ${command.refName} (NON fast-forward)\n\n"\r
+                       changes += commits.collect(table).join("\n")\r
+                       changes += "\n"\r
+                       break\r
+               case ReceiveCommand.Type.DELETE:\r
+                       // deleted branch\r
+                       changes += "deleted ${command.refName}\n\n"\r
                        break\r
-                       \r
                default:\r
                        break\r
        }\r
@@ -121,14 +143,10 @@ if (gitblit.getBoolean(Keys.web.mountParameters, true))
 else\r
        summaryUrl = url + "/summary?r=" + repository.name\r
 \r
-// create a simple commits table\r
-def table = commits.collect { it.id.name[0..8] + " " + it.authorIdent.name.padRight(20, " ") + it.shortMessage }.join("\n")\r
-\r
 // create the message body\r
-def msg = """${user.username} pushed ${commits.size} commits to ${repository.name}\r
-${summaryUrl}\r
+def msg = """${summaryUrl}\r
 \r
-${table}"""\r
+${changes}"""\r
 \r
 // tell Gitblit to send the message (Gitblit filters duplicate addresses)\r
-gitblit.notifyUsers("${emailprefix} ${user.username} pushed ${commits.size} commits => ${repository.name}", msg, toAddresses)
\ No newline at end of file
+gitblit.notifyUsers("${emailprefix} ${user.username} pushed ${commitCount} commits => ${repository.name}", msg, toAddresses)
\ No newline at end of file
index 99c2d0a86f408e9bf7b35ff88483148e7cf8058a..31fd08ffc58d9a7aadf7a6c640210f3e4353c83d 100644 (file)
@@ -964,6 +964,43 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
+       /**\r
+        * Returns a list of commits for the repository within the range specified\r
+        * by startRangeId and endRangeId. If the repository does not exist or is\r
+        * empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param startRangeId\r
+        *            the first commit (not included in results)\r
+        * @param endRangeId\r
+        *            the end commit (included in results)\r
+        * @return a list of commits\r
+        */\r
+       public static List<RevCommit> getRevLog(Repository repository, String startRangeId,\r
+                       String endRangeId) {\r
+               List<RevCommit> list = new ArrayList<RevCommit>();\r
+               if (!hasCommits(repository)) {\r
+                       return list;\r
+               }\r
+               try {\r
+                       ObjectId endRange = repository.resolve(endRangeId);\r
+                       ObjectId startRange = repository.resolve(startRangeId);\r
+\r
+                       RevWalk rw = new RevWalk(repository);\r
+                       rw.markStart(rw.parseCommit(endRange));\r
+                       rw.markUninteresting(rw.parseCommit(startRange));\r
+\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 revlog for {1}..{2}", startRangeId, endRangeId);\r
+               }\r
+               return list;\r
+       }\r
+\r
        /**\r
         * Search the commit history for a case-insensitive match to the value.\r
         * Search results require a specified SearchType of AUTHOR, COMMITTER, or\r
index 548a72bcfdc36f82a4be9d6967a6d0238d87474d..b3cf41abf740644e9450bf4ffdc5f491a35fbaba 100644 (file)
@@ -97,12 +97,14 @@ public class GitServletTest {
        public void testAnonymousCommit() throws Exception {\r
                Git git = Git.open(folder);\r
                File file = new File(folder, "TODO");\r
-               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
-               BufferedWriter w = new BufferedWriter(os);\r
-               w.write("// " + new Date().toString() + "\n");\r
-               w.close();\r
-               git.add().addFilepattern(file.getName()).call();\r
-               git.commit().setMessage("test commit").call();\r
+               for (int i = 0; i < 3; i++) {\r
+                       OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
+                       BufferedWriter w = new BufferedWriter(os);\r
+                       w.write("// " + new Date().toString() + "\n");\r
+                       w.close();\r
+                       git.add().addFilepattern(file.getName()).call();\r
+                       git.commit().setMessage("test commit #" + (i + 1)).call();\r
+               }\r
                git.push().setPushAll().call();\r
                git.getRepository().close();\r
        }\r
index e330c4262c21a079d6bc9efb641db6889d82033b..4e85b49ca5808a2d5bfee9d045782c886b0f5184 100644 (file)
@@ -332,6 +332,16 @@ public class JGitUtilsTest {
                repository.close();\r
        }\r
 \r
+       @Test\r
+       public void testRevLogRange() throws Exception {\r
+               Repository repository = GitBlitSuite.getHelloworldRepository();\r
+               List<RevCommit> commits = JGitUtils.getRevLog(repository,\r
+                               "fbd14fa6d1a01d4aefa1fca725792683800fc67e",\r
+                               "85a0e4087b8439c0aa6b1f4f9e08c26052ab7e87");\r
+               repository.close();\r
+               assertEquals(14, commits.size());\r
+       }\r
+\r
        @Test\r
        public void testSearchTypes() throws Exception {\r
                assertEquals(SearchType.COMMIT, SearchType.forName("commit"));\r