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

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