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.

GitWebDiffFormatter.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.encodeASCII;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.eclipse.jgit.diff.DiffFormatter;
  22. import org.eclipse.jgit.diff.RawText;
  23. public class GitWebDiffFormatter extends DiffFormatter {
  24. private final OutputStream os;
  25. public GitWebDiffFormatter(OutputStream os) {
  26. super(os);
  27. this.os = os;
  28. }
  29. /**
  30. * Output a hunk header
  31. *
  32. * @param aStartLine
  33. * within first source
  34. * @param aEndLine
  35. * within first source
  36. * @param bStartLine
  37. * within second source
  38. * @param bEndLine
  39. * within second source
  40. * @throws IOException
  41. */
  42. @Override
  43. protected void writeHunkHeader(int aStartLine, int aEndLine, int bStartLine, int bEndLine) throws IOException {
  44. os.write("<div class=\"diff hunk_header\"><span class=\"diff hunk_info\">".getBytes());
  45. os.write('@');
  46. os.write('@');
  47. writeRange('-', aStartLine + 1, aEndLine - aStartLine);
  48. writeRange('+', bStartLine + 1, bEndLine - bStartLine);
  49. os.write(' ');
  50. os.write('@');
  51. os.write('@');
  52. os.write("</span></div>".getBytes());
  53. }
  54. protected void writeRange(final char prefix, final int begin, final int cnt) throws IOException {
  55. os.write(' ');
  56. os.write(prefix);
  57. switch (cnt) {
  58. case 0:
  59. // If the range is empty, its beginning number must
  60. // be the
  61. // line just before the range, or 0 if the range is
  62. // at the
  63. // start of the file stream. Here, begin is always 1
  64. // based,
  65. // so an empty file would produce "0,0".
  66. //
  67. os.write(encodeASCII(begin - 1));
  68. os.write(',');
  69. os.write('0');
  70. break;
  71. case 1:
  72. // If the range is exactly one line, produce only
  73. // the number.
  74. //
  75. os.write(encodeASCII(begin));
  76. break;
  77. default:
  78. os.write(encodeASCII(begin));
  79. os.write(',');
  80. os.write(encodeASCII(cnt));
  81. break;
  82. }
  83. }
  84. @Override
  85. protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException {
  86. switch (prefix) {
  87. case '+':
  88. os.write("<span class=\"diff add\">".getBytes());
  89. break;
  90. case '-':
  91. os.write("<span class=\"diff remove\">".getBytes());
  92. break;
  93. }
  94. os.write(prefix);
  95. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  96. text.writeLine(bos, cur);
  97. String line = bos.toString();
  98. line = StringUtils.escapeForHtml(line, false);
  99. os.write(line.getBytes());
  100. switch (prefix) {
  101. case '+':
  102. case '-':
  103. os.write("</span>\n".getBytes());
  104. break;
  105. default:
  106. os.write('\n');
  107. }
  108. }
  109. /**
  110. * Workaround function for complex private methods in DiffFormatter. This
  111. * sets the html for the diff headers.
  112. *
  113. * @return
  114. */
  115. public String getHtml() {
  116. String html = os.toString();
  117. String[] lines = html.split("\n");
  118. StringBuilder sb = new StringBuilder();
  119. sb.append("<div class=\"diff\">");
  120. for (String line : lines) {
  121. if (line.startsWith("diff")) {
  122. sb.append("<div class=\"diff header\">").append(line).append("</div>");
  123. } else if (line.startsWith("---")) {
  124. sb.append("<span class=\"diff remove\">").append(line).append("</span><br/>");
  125. } else if (line.startsWith("+++")) {
  126. sb.append("<span class=\"diff add\">").append(line).append("</span><br/>");
  127. } else {
  128. sb.append(line).append('\n');
  129. }
  130. }
  131. sb.append("</div>\n");
  132. return sb.toString();
  133. }
  134. }