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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo.commands;
  23. import com.dmdirc.util.Downloader;
  24. import com.md87.charliebravo.Command;
  25. import com.md87.charliebravo.Followup;
  26. import com.md87.charliebravo.InputHandler;
  27. import com.md87.charliebravo.Response;
  28. import java.net.URLEncoder;
  29. import java.nio.charset.Charset;
  30. import java.util.List;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. /**
  34. *
  35. * @author chris
  36. */
  37. public class GitCommand implements Command {
  38. public void execute(InputHandler handler, Response response, String line) throws Exception {
  39. if (line.isEmpty()) {
  40. response.sendMessage("You need to specify a revision", true);
  41. } else {
  42. final List<String> result = Downloader.getPage(
  43. "http://git.dmdirc.com/cgit.cgi/client/commit/?id="
  44. + URLEncoder.encode(line, Charset.defaultCharset().name()));
  45. final StringBuilder builder = new StringBuilder();
  46. for (String resline : result) {
  47. builder.append(resline);
  48. }
  49. if (builder.indexOf("<div class=\"error\">Bad object id:") > -1) {
  50. response.sendMessage("That commit was not found", true);
  51. } else {
  52. Matcher matcher = Pattern.compile("<th>author</th>"
  53. + "<td>(.*?) &lt;(.*?)&gt;</td><td class='right'>(.*?)</td>")
  54. .matcher(builder);
  55. matcher.find();
  56. final String authorName = matcher.group(1);
  57. final String authorEmail = matcher.group(2);
  58. final String commitDate = matcher.group(3);
  59. matcher = Pattern.compile("<th>commit</th><td colspan='2' class='sha1'>"
  60. + "<a href='/cgit.cgi/client/commit/\\?id=(.*?)'>").matcher(builder);
  61. matcher.find();
  62. final String commitHash = matcher.group(1);
  63. matcher = Pattern.compile("<div class='commit-subject'>(.*?)(<a .*?)?</div>")
  64. .matcher(builder);
  65. matcher.find();
  66. final String commitSubject = matcher.group(1);
  67. matcher = Pattern.compile("<div class='diffstat-summary'>(.*?)</div>")
  68. .matcher(builder);
  69. matcher.find();
  70. final String commitDiff = matcher.group(1);
  71. response.sendMessage("commit " + commitHash + " was made by "
  72. + authorName + " with message '" + commitSubject + "'");
  73. response.addFollowup(new DiffFollowup(commitDiff, commitHash));
  74. response.addFollowup(new DetailsFollowup(authorName, authorEmail,
  75. commitHash, commitDate));
  76. }
  77. }
  78. }
  79. protected static class DiffFollowup implements Followup {
  80. private final String data;
  81. private final String hash;
  82. public DiffFollowup(String data, String hash) {
  83. this.data = data;
  84. this.hash = hash;
  85. }
  86. public boolean matches(String line) {
  87. return line.equalsIgnoreCase("diff");
  88. }
  89. public void execute(InputHandler handler, Response response, String line) throws Exception {
  90. response.setInheritFollows(true);
  91. response.sendMessage("there were " + data.replaceAll("^(.*), (.*?)$", "$1 and $2")
  92. + " in commit "
  93. + hash + ". See http://git.dmdirc.com/cgit.cgi/client/diff/?id=" + hash);
  94. }
  95. }
  96. protected static class DetailsFollowup implements Followup {
  97. private final String authorName, authorEmail, hash, time;
  98. public DetailsFollowup(String authorName, String authorEmail, String hash, String time) {
  99. this.authorName = authorName;
  100. this.authorEmail = authorEmail;
  101. this.hash = hash;
  102. this.time = time;
  103. }
  104. public boolean matches(String line) {
  105. return line.equalsIgnoreCase("details");
  106. }
  107. public void execute(InputHandler handler, Response response, String line) throws Exception {
  108. response.setInheritFollows(true);
  109. response.sendMessage("commit " + hash + " was made on " + time + " by "
  110. + authorName + " <" + authorEmail + ">. " +
  111. "See http://git.dmdirc.com/cgit.cgi/client/commit/?id=" + hash);
  112. }
  113. }
  114. }