From f4759059d4c363f5bd42550c9c35dd21f4d2bf41 Mon Sep 17 00:00:00 2001 From: James Moger Date: Sat, 17 Dec 2011 14:03:35 -0500 Subject: [PATCH] Fixed sendemail script to list all commits in a push. --- groovy/sendemail.groovy | 50 ++++++++++++++------- src/com/gitblit/utils/JGitUtils.java | 37 +++++++++++++++ tests/com/gitblit/tests/GitServletTest.java | 14 +++--- tests/com/gitblit/tests/JGitUtilsTest.java | 10 +++++ 4 files changed, 89 insertions(+), 22 deletions(-) diff --git a/groovy/sendemail.groovy b/groovy/sendemail.groovy index 1ba72a8a..29c5e5af 100644 --- a/groovy/sendemail.groovy +++ b/groovy/sendemail.groovy @@ -93,20 +93,42 @@ switch(repository.name) { toAddresses.add "dev-team@somewhere.com" toAddresses.add "qa-team@somewhere.com" break - default: - break } -// get the create/update commits from the repository to build message content -def commits = [] -for (ReceiveCommand command:commands) { +// construct a simple text summary of the changes contained in the push +def commitCount = 0 +def changes = "" +def table = { it.id.name[0..8] + " " + it.authorIdent.name.padRight(20, " ") + it.shortMessage } +for (command in commands) { switch (command.type) { - case ReceiveCommand.Type.UPDATE: case ReceiveCommand.Type.CREATE: - RevCommit commit = JGitUtils.getCommit(r, command.newId.name) - commits.add(commit) + def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name) + commitCount += commits.size() + // new branch commits table + changes += "created ${command.refName}\n\n" + changes += commits.collect(table).join("\n") + changes += "\n" + break + case ReceiveCommand.Type.UPDATE: + def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name) + commitCount += commits.size() + // fast-forward branch commits table + changes += "updated ${command.refName}\n\n" + changes += commits.collect(table).join("\n") + changes += "\n" + break + case ReceiveCommand.Type.UPDATE_NONFASTFORWARD: + def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name) + commitCount += commits.size() + // non-fast-forward branch commits table + changes += "updated ${command.refName} (NON fast-forward)\n\n" + changes += commits.collect(table).join("\n") + changes += "\n" + break + case ReceiveCommand.Type.DELETE: + // deleted branch + changes += "deleted ${command.refName}\n\n" break - default: break } @@ -121,14 +143,10 @@ if (gitblit.getBoolean(Keys.web.mountParameters, true)) else summaryUrl = url + "/summary?r=" + repository.name -// create a simple commits table -def table = commits.collect { it.id.name[0..8] + " " + it.authorIdent.name.padRight(20, " ") + it.shortMessage }.join("\n") - // create the message body -def msg = """${user.username} pushed ${commits.size} commits to ${repository.name} -${summaryUrl} +def msg = """${summaryUrl} -${table}""" +${changes}""" // tell Gitblit to send the message (Gitblit filters duplicate addresses) -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 diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index 99c2d0a8..31fd08ff 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -964,6 +964,43 @@ public class JGitUtils { return list; } + /** + * Returns a list of commits for the repository within the range specified + * by startRangeId and endRangeId. If the repository does not exist or is + * empty, an empty list is returned. + * + * @param repository + * @param startRangeId + * the first commit (not included in results) + * @param endRangeId + * the end commit (included in results) + * @return a list of commits + */ + public static List getRevLog(Repository repository, String startRangeId, + String endRangeId) { + List list = new ArrayList(); + if (!hasCommits(repository)) { + return list; + } + try { + ObjectId endRange = repository.resolve(endRangeId); + ObjectId startRange = repository.resolve(startRangeId); + + RevWalk rw = new RevWalk(repository); + rw.markStart(rw.parseCommit(endRange)); + rw.markUninteresting(rw.parseCommit(startRange)); + + Iterable revlog = rw; + for (RevCommit rev : revlog) { + list.add(rev); + } + rw.dispose(); + } catch (Throwable t) { + error(t, repository, "{0} failed to get revlog for {1}..{2}", startRangeId, endRangeId); + } + return list; + } + /** * Search the commit history for a case-insensitive match to the value. * Search results require a specified SearchType of AUTHOR, COMMITTER, or diff --git a/tests/com/gitblit/tests/GitServletTest.java b/tests/com/gitblit/tests/GitServletTest.java index 548a72bc..b3cf41ab 100644 --- a/tests/com/gitblit/tests/GitServletTest.java +++ b/tests/com/gitblit/tests/GitServletTest.java @@ -97,12 +97,14 @@ public class GitServletTest { public void testAnonymousCommit() throws Exception { Git git = Git.open(folder); File file = new File(folder, "TODO"); - OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true)); - BufferedWriter w = new BufferedWriter(os); - w.write("// " + new Date().toString() + "\n"); - w.close(); - git.add().addFilepattern(file.getName()).call(); - git.commit().setMessage("test commit").call(); + for (int i = 0; i < 3; i++) { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true)); + BufferedWriter w = new BufferedWriter(os); + w.write("// " + new Date().toString() + "\n"); + w.close(); + git.add().addFilepattern(file.getName()).call(); + git.commit().setMessage("test commit #" + (i + 1)).call(); + } git.push().setPushAll().call(); git.getRepository().close(); } diff --git a/tests/com/gitblit/tests/JGitUtilsTest.java b/tests/com/gitblit/tests/JGitUtilsTest.java index e330c426..4e85b49c 100644 --- a/tests/com/gitblit/tests/JGitUtilsTest.java +++ b/tests/com/gitblit/tests/JGitUtilsTest.java @@ -332,6 +332,16 @@ public class JGitUtilsTest { repository.close(); } + @Test + public void testRevLogRange() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + List commits = JGitUtils.getRevLog(repository, + "fbd14fa6d1a01d4aefa1fca725792683800fc67e", + "85a0e4087b8439c0aa6b1f4f9e08c26052ab7e87"); + repository.close(); + assertEquals(14, commits.size()); + } + @Test public void testSearchTypes() throws Exception { assertEquals(SearchType.COMMIT, SearchType.forName("commit")); -- 2.39.5