您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GitBlitDiffFormatter.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import static org.eclipse.jgit.lib.Constants.encode;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.eclipse.jgit.diff.RawText;
  22. import org.eclipse.jgit.util.RawParseUtils;
  23. /**
  24. * Generates an html snippet of a diff in Gitblit's style.
  25. *
  26. * @author James Moger
  27. *
  28. */
  29. public class GitBlitDiffFormatter extends GitWebDiffFormatter {
  30. private final OutputStream os;
  31. private int left, right;
  32. public GitBlitDiffFormatter(OutputStream os) {
  33. super(os);
  34. this.os = os;
  35. }
  36. /**
  37. * Output a hunk header
  38. *
  39. * @param aStartLine
  40. * within first source
  41. * @param aEndLine
  42. * within first source
  43. * @param bStartLine
  44. * within second source
  45. * @param bEndLine
  46. * within second source
  47. * @throws IOException
  48. */
  49. @Override
  50. protected void writeHunkHeader(int aStartLine, int aEndLine, int bStartLine, int bEndLine)
  51. throws IOException {
  52. os.write("<tr><th>..</th><th>..</th><td class='hunk_header'>".getBytes());
  53. os.write('@');
  54. os.write('@');
  55. writeRange('-', aStartLine + 1, aEndLine - aStartLine);
  56. writeRange('+', bStartLine + 1, bEndLine - bStartLine);
  57. os.write(' ');
  58. os.write('@');
  59. os.write('@');
  60. os.write("</td></tr>\n".getBytes());
  61. left = aStartLine + 1;
  62. right = bStartLine + 1;
  63. }
  64. @Override
  65. protected void writeLine(final char prefix, final RawText text, final int cur)
  66. throws IOException {
  67. os.write("<tr>".getBytes());
  68. switch (prefix) {
  69. case '+':
  70. os.write(("<th></th><th>" + (right++) + "</th>").getBytes());
  71. os.write("<td><div class=\"diff add2\">".getBytes());
  72. break;
  73. case '-':
  74. os.write(("<th>" + (left++) + "</th><th></th>").getBytes());
  75. os.write("<td><div class=\"diff remove2\">".getBytes());
  76. break;
  77. default:
  78. os.write(("<th>" + (left++) + "</th><th>" + (right++) + "</th>").getBytes());
  79. os.write("<td>".getBytes());
  80. break;
  81. }
  82. os.write(prefix);
  83. String line = text.getString(cur);
  84. line = StringUtils.escapeForHtml(line, false);
  85. os.write(encode(line));
  86. switch (prefix) {
  87. case '+':
  88. case '-':
  89. os.write("</div>".getBytes());
  90. break;
  91. default:
  92. os.write("</td>".getBytes());
  93. }
  94. os.write("</tr>\n".getBytes());
  95. }
  96. /**
  97. * Workaround function for complex private methods in DiffFormatter. This
  98. * sets the html for the diff headers.
  99. *
  100. * @return
  101. */
  102. @Override
  103. public String getHtml() {
  104. ByteArrayOutputStream bos = (ByteArrayOutputStream) os;
  105. String html = RawParseUtils.decode(bos.toByteArray());
  106. String[] lines = html.split("\n");
  107. StringBuilder sb = new StringBuilder();
  108. boolean inFile = false;
  109. String oldnull = "a/dev/null";
  110. for (String line : lines) {
  111. if (line.startsWith("index")) {
  112. // skip index lines
  113. } else if (line.startsWith("new file")) {
  114. // skip new file lines
  115. } else if (line.startsWith("\\ No newline")) {
  116. // skip no new line
  117. } else if (line.startsWith("---") || line.startsWith("+++")) {
  118. // skip --- +++ lines
  119. } else if (line.startsWith("diff")) {
  120. line = StringUtils.convertOctal(line);
  121. if (line.indexOf(oldnull) > -1) {
  122. // a is null, use b
  123. line = line.substring(("diff --git " + oldnull).length()).trim();
  124. // trim b/
  125. line = line.substring(2).trim();
  126. } else {
  127. // use a
  128. line = line.substring("diff --git ".length()).trim();
  129. line = line.substring(line.startsWith("\"a/") ? 3 : 2);
  130. line = line.substring(0, line.indexOf(" b/") > -1 ? line.indexOf(" b/") : line.indexOf("\"b/")).trim();
  131. }
  132. if (line.charAt(0) == '"') {
  133. line = line.substring(1);
  134. }
  135. if (line.charAt(line.length() - 1) == '"') {
  136. line = line.substring(0, line.length() - 1);
  137. }
  138. if (inFile) {
  139. sb.append("</tbody></table></div>\n");
  140. inFile = false;
  141. }
  142. sb.append("<div class='header'>").append(line).append("</div>");
  143. sb.append("<div class=\"diff\">");
  144. sb.append("<table><tbody>");
  145. inFile = true;
  146. } else {
  147. sb.append(line);
  148. }
  149. }
  150. sb.append("</table></div>");
  151. return sb.toString();
  152. }
  153. }