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.

GitBlitDiffFormatter.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import org.eclipse.jgit.diff.RawText;
  21. public class GitBlitDiffFormatter extends GitWebDiffFormatter {
  22. private final OutputStream os;
  23. private int left = 0, right = 0;
  24. public GitBlitDiffFormatter(OutputStream os) {
  25. super(os);
  26. this.os = os;
  27. }
  28. /**
  29. * Output a hunk header
  30. *
  31. * @param aStartLine
  32. * within first source
  33. * @param aEndLine
  34. * within first source
  35. * @param bStartLine
  36. * within second source
  37. * @param bEndLine
  38. * within second source
  39. * @throws IOException
  40. */
  41. @Override
  42. protected void writeHunkHeader(int aStartLine, int aEndLine, int bStartLine, int bEndLine) throws IOException {
  43. os.write("<tr><th>..</th><th>..</th><td class='hunk_header'>".getBytes());
  44. os.write('@');
  45. os.write('@');
  46. writeRange('-', aStartLine + 1, aEndLine - aStartLine);
  47. writeRange('+', bStartLine + 1, bEndLine - bStartLine);
  48. os.write(' ');
  49. os.write('@');
  50. os.write('@');
  51. os.write("</td></tr>\n".getBytes());
  52. left = aStartLine + 1;
  53. right = bStartLine + 1;
  54. }
  55. @Override
  56. protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException {
  57. os.write("<tr>".getBytes());
  58. switch (prefix) {
  59. case '+':
  60. os.write(("<th></th><th>" + (right++) + "</th>").getBytes());
  61. os.write("<td><div class=\"diff add2\">".getBytes());
  62. break;
  63. case '-':
  64. os.write(("<th>" + (left++) + "</th><th></th>").getBytes());
  65. os.write("<td><div class=\"diff remove2\">".getBytes());
  66. break;
  67. default:
  68. os.write(("<th>" + (left++) + "</th><th>" + (right++) + "</th>").getBytes());
  69. os.write("<td>".getBytes());
  70. break;
  71. }
  72. os.write(prefix);
  73. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  74. text.writeLine(bos, cur);
  75. String line = bos.toString();
  76. line = StringUtils.escapeForHtml(line, false);
  77. os.write(line.getBytes());
  78. switch (prefix) {
  79. case '+':
  80. case '-':
  81. os.write("</div>".getBytes());
  82. break;
  83. default:
  84. os.write("</td>".getBytes());
  85. }
  86. os.write("</tr>\n".getBytes());
  87. }
  88. /**
  89. * Workaround function for complex private methods in DiffFormatter. This
  90. * sets the html for the diff headers.
  91. *
  92. * @return
  93. */
  94. @Override
  95. public String getHtml() {
  96. String html = os.toString();
  97. String[] lines = html.split("\n");
  98. StringBuilder sb = new StringBuilder();
  99. boolean inFile = false;
  100. String oldnull = "a/dev/null";
  101. for (String line : lines) {
  102. if (line.startsWith("index")) {
  103. // skip index lines
  104. } else if (line.startsWith("new file")) {
  105. // skip new file lines
  106. } else if (line.startsWith("\\ No newline")) {
  107. // skip no new line
  108. } else if (line.startsWith("---") || line.startsWith("+++")) {
  109. // skip --- +++ lines
  110. } else if (line.startsWith("diff")) {
  111. if (line.indexOf(oldnull) > -1) {
  112. // a is null, use b
  113. line = line.substring(("diff --git " + oldnull).length()).trim();
  114. line = line.substring(2); // trim b/
  115. } else {
  116. // use a
  117. line = line.substring("diff --git a/".length()).trim();
  118. line = line.substring(0, line.indexOf(" b/")).trim();
  119. }
  120. if (inFile) {
  121. sb.append("</tbody></table></div>\n");
  122. inFile = false;
  123. }
  124. sb.append("<div class='header'>").append(line).append("</div>");
  125. sb.append("<div class=\"diff\">");
  126. sb.append("<table><tbody>");
  127. inFile = true;
  128. } else {
  129. sb.append(line);
  130. }
  131. }
  132. sb.append("</table></div>");
  133. return sb.toString();
  134. }
  135. }