Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

HtmlDiffFormatter.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.gitblit.utils;
  2. import static org.eclipse.jgit.lib.Constants.encodeASCII;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import org.eclipse.jgit.diff.DiffFormatter;
  6. import org.eclipse.jgit.diff.RawText;
  7. public class HtmlDiffFormatter extends DiffFormatter {
  8. private final OutputStream os;
  9. public HtmlDiffFormatter(OutputStream os) {
  10. super(os);
  11. this.os = os;
  12. }
  13. /**
  14. * Output a hunk header
  15. *
  16. * @param aStartLine
  17. * within first source
  18. * @param aEndLine
  19. * within first source
  20. * @param bStartLine
  21. * within second source
  22. * @param bEndLine
  23. * within second source
  24. * @throws IOException
  25. */
  26. @Override
  27. protected void writeHunkHeader(int aStartLine, int aEndLine, int bStartLine, int bEndLine) throws IOException {
  28. os.write("<div class=\"diff hunk_header\"><span class=\"diff hunk_info\">".getBytes());
  29. os.write('@');
  30. os.write('@');
  31. writeRange('-', aStartLine + 1, aEndLine - aStartLine);
  32. writeRange('+', bStartLine + 1, bEndLine - bStartLine);
  33. os.write(' ');
  34. os.write('@');
  35. os.write('@');
  36. // TODO not sure if JGit can determine hunk section
  37. //os.write("<span class=\"diff hunk_section\">".getBytes());
  38. //os.write("</span>".getBytes());
  39. os.write("</span></div>".getBytes());
  40. }
  41. private void writeRange(final char prefix, final int begin, final int cnt) throws IOException {
  42. os.write(' ');
  43. os.write(prefix);
  44. switch (cnt) {
  45. case 0:
  46. // If the range is empty, its beginning number must
  47. // be the
  48. // line just before the range, or 0 if the range is
  49. // at the
  50. // start of the file stream. Here, begin is always 1
  51. // based,
  52. // so an empty file would produce "0,0".
  53. //
  54. os.write(encodeASCII(begin - 1));
  55. os.write(',');
  56. os.write('0');
  57. break;
  58. case 1:
  59. // If the range is exactly one line, produce only
  60. // the number.
  61. //
  62. os.write(encodeASCII(begin));
  63. break;
  64. default:
  65. os.write(encodeASCII(begin));
  66. os.write(',');
  67. os.write(encodeASCII(cnt));
  68. break;
  69. }
  70. }
  71. @Override
  72. protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException {
  73. switch (prefix) {
  74. case '+':
  75. os.write("<div class=\"diff add\">".getBytes());
  76. break;
  77. case '-':
  78. os.write("<div class=\"diff remove\">".getBytes());
  79. break;
  80. }
  81. os.write(prefix);
  82. text.writeLine(os, cur);
  83. switch (prefix) {
  84. case '+':
  85. case '-':
  86. os.write("</div>".getBytes());
  87. break;
  88. default:
  89. os.write('\n');
  90. }
  91. }
  92. /**
  93. * Workaround function for complex private methods in DiffFormatter. This
  94. * sets the html for the diff headers.
  95. *
  96. * @return
  97. */
  98. public String getHtml() {
  99. String html = os.toString();
  100. String[] lines = html.split("\n");
  101. StringBuilder sb = new StringBuilder();
  102. sb.append("<div class=\"diff\">");
  103. for (String line : lines) {
  104. if (line.startsWith("diff")) {
  105. sb.append("<div class=\"diff header\">").append(line).append("</div>");
  106. } else if (line.startsWith("---")) {
  107. sb.append("<div class=\"diff remove\">").append(line).append("</div>");
  108. } else if (line.startsWith("+++")) {
  109. sb.append("<div class=\"diff add\">").append(line).append("</div>");
  110. } else {
  111. sb.append(line).append('\n');
  112. }
  113. }
  114. sb.append("</div>");
  115. return sb.toString();
  116. }
  117. }