Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitCommand.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo.commands;
  6. import com.dmdirc.util.Downloader;
  7. import com.md87.charliebravo.Command;
  8. import com.md87.charliebravo.Followup;
  9. import com.md87.charliebravo.InputHandler;
  10. import com.md87.charliebravo.Response;
  11. import java.net.URLEncoder;
  12. import java.nio.charset.Charset;
  13. import java.util.List;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16. /**
  17. *
  18. * @author chris
  19. */
  20. public class GitCommand implements Command {
  21. public void execute(InputHandler handler, Response response, String line) throws Exception {
  22. if (line.isEmpty()) {
  23. response.sendMessage("You need to specify a revision", true);
  24. } else {
  25. final List<String> result = Downloader.getPage(
  26. "http://git.dmdirc.com/cgit.cgi/client/commit/?id="
  27. + URLEncoder.encode(line, Charset.defaultCharset().name()));
  28. final StringBuilder builder = new StringBuilder();
  29. for (String resline : result) {
  30. builder.append(resline);
  31. }
  32. if (builder.indexOf("<div class=\"error\">Bad object id:") > -1) {
  33. response.sendMessage("That commit was not found", true);
  34. } else {
  35. Matcher matcher = Pattern.compile("<th>author</th>"
  36. + "<td>(.*?) &lt;(.*?)&gt;</td><td class='right'>(.*?)</td>")
  37. .matcher(builder);
  38. matcher.find();
  39. final String authorName = matcher.group(1);
  40. final String authorEmail = matcher.group(2);
  41. final String commitDate = matcher.group(3);
  42. matcher = Pattern.compile("<th>commit</th><td colspan='2' class='sha1'>"
  43. + "<a href='/cgit.cgi/client/commit/\\?id=(.*?)'>").matcher(builder);
  44. matcher.find();
  45. final String commitHash = matcher.group(1);
  46. matcher = Pattern.compile("<div class='commit-subject'>(.*?)(<a .*?)?</div>")
  47. .matcher(builder);
  48. matcher.find();
  49. final String commitSubject = matcher.group(1);
  50. matcher = Pattern.compile("<div class='diffstat-summary'>(.*?)</div>")
  51. .matcher(builder);
  52. matcher.find();
  53. final String commitDiff = matcher.group(1);
  54. response.sendMessage("commit " + commitHash + " was made by "
  55. + authorName + " with message '" + commitSubject + "'");
  56. response.addFollowup(new DiffFollowup(commitDiff, commitHash));
  57. response.addFollowup(new DetailsFollowup(authorName, authorEmail,
  58. commitHash, commitDate));
  59. }
  60. }
  61. }
  62. protected static class DiffFollowup implements Followup {
  63. private final String data;
  64. private final String hash;
  65. public DiffFollowup(String data, String hash) {
  66. this.data = data;
  67. this.hash = hash;
  68. }
  69. public boolean matches(String line) {
  70. return line.equalsIgnoreCase("diff");
  71. }
  72. public void execute(InputHandler handler, Response response, String line) throws Exception {
  73. response.setInheritFollows(true);
  74. response.sendMessage("there were " + data.replaceAll("^(.*), (.*?)$", "$1 and $2")
  75. + " in commit "
  76. + hash + ". See http://git.dmdirc.com/cgit.cgi/client/diff/?id=" + hash);
  77. }
  78. }
  79. protected static class DetailsFollowup implements Followup {
  80. private final String authorName, authorEmail, hash, time;
  81. public DetailsFollowup(String authorName, String authorEmail, String hash, String time) {
  82. this.authorName = authorName;
  83. this.authorEmail = authorEmail;
  84. this.hash = hash;
  85. this.time = time;
  86. }
  87. public boolean matches(String line) {
  88. return line.equalsIgnoreCase("details");
  89. }
  90. public void execute(InputHandler handler, Response response, String line) throws Exception {
  91. response.setInheritFollows(true);
  92. response.sendMessage("commit " + hash + " was made on " + time + " by "
  93. + authorName + " <" + authorEmail + ">. " +
  94. "See http://git.dmdirc.com/cgit.cgi/client/commit/?id=" + hash);
  95. }
  96. }
  97. }