| | |
| | | import static org.eclipse.jgit.lib.Constants.encodeASCII; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.MessageFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | import org.eclipse.jgit.util.RawParseUtils; |
| | | |
| | | import com.gitblit.models.PathModel.PathChangeModel; |
| | | import com.gitblit.utils.DiffUtils.BinaryDiffHandler; |
| | | import com.gitblit.utils.DiffUtils.DiffStat; |
| | | import com.gitblit.wicket.GitBlitWebApp; |
| | | |
| | |
| | | */ |
| | | private static final int GLOBAL_DIFF_LIMIT = 20000; |
| | | |
| | | private final ResettableByteArrayOutputStream os; |
| | | private static final boolean CONVERT_TABS = true; |
| | | |
| | | private final DiffOutputStream os; |
| | | |
| | | private final DiffStat diffStat; |
| | | |
| | |
| | | /** If {@link #truncated}, contains all entries skipped. */ |
| | | private final List<DiffEntry> skipped = new ArrayList<DiffEntry>(); |
| | | |
| | | public GitBlitDiffFormatter(String commitId, String path) { |
| | | super(new ResettableByteArrayOutputStream()); |
| | | this.os = (ResettableByteArrayOutputStream) getOutputStream(); |
| | | private int tabLength; |
| | | |
| | | /** |
| | | * A {@link ResettableByteArrayOutputStream} that intercept the "Binary files differ" message produced |
| | | * by the super implementation. Unfortunately the super implementation has far too many things private; |
| | | * otherwise we'd just have re-implemented {@link GitBlitDiffFormatter#format(DiffEntry) format(DiffEntry)} |
| | | * completely without ever calling the super implementation. |
| | | */ |
| | | private static class DiffOutputStream extends ResettableByteArrayOutputStream { |
| | | |
| | | private static final String BINARY_DIFFERENCE = "Binary files differ\n"; |
| | | |
| | | private GitBlitDiffFormatter formatter; |
| | | private BinaryDiffHandler binaryDiffHandler; |
| | | |
| | | public void setFormatter(GitBlitDiffFormatter formatter, BinaryDiffHandler handler) { |
| | | this.formatter = formatter; |
| | | this.binaryDiffHandler = handler; |
| | | } |
| | | |
| | | @Override |
| | | public void write(byte[] b, int offset, int length) { |
| | | if (binaryDiffHandler != null |
| | | && RawParseUtils.decode(Arrays.copyOfRange(b, offset, offset + length)).contains(BINARY_DIFFERENCE)) |
| | | { |
| | | String binaryDiff = binaryDiffHandler.renderBinaryDiff(formatter.entry); |
| | | if (binaryDiff != null) { |
| | | byte[] bb = ("<tr><td colspan='4' align='center'>" + binaryDiff + "</td></tr>").getBytes(StandardCharsets.UTF_8); |
| | | super.write(bb, 0, bb.length); |
| | | return; |
| | | } |
| | | } |
| | | super.write(b, offset, length); |
| | | } |
| | | |
| | | } |
| | | |
| | | public GitBlitDiffFormatter(String commitId, String path, BinaryDiffHandler handler, int tabLength) { |
| | | super(new DiffOutputStream()); |
| | | this.os = (DiffOutputStream) getOutputStream(); |
| | | this.os.setFormatter(this, handler); |
| | | this.diffStat = new DiffStat(commitId); |
| | | this.tabLength = tabLength; |
| | | // If we have a full commitdiff, install maxima to avoid generating a super-long diff listing that |
| | | // will only tax the browser too much. |
| | | maxDiffLinesPerFile = path != null ? -1 : getLimit(DIFF_LIMIT_PER_FILE_KEY, 500, DIFF_LIMIT_PER_FILE); |
| | |
| | | super.format(ent); |
| | | if (!truncated) { |
| | | // Close the table |
| | | os.write("</tbody></table></div><br />\n".getBytes()); |
| | | os.write("</tbody></table></div>\n".getBytes()); |
| | | } |
| | | } |
| | | |
| | |
| | | private void reset() { |
| | | if (!isOff) { |
| | | os.resetTo(startCurrent); |
| | | try { |
| | | os.write("<tr><td class='diff-cell' colspan='4'>".getBytes()); |
| | | os.write(StringUtils.escapeForHtml(getMsg("gb.diffFileDiffTooLarge", "Diff too large"), false).getBytes()); |
| | | os.write("</td></tr>\n".getBytes()); |
| | | } catch (IOException ex) { |
| | | // Cannot happen with a ByteArrayOutputStream |
| | | } |
| | | writeFullWidthLine(getMsg("gb.diffFileDiffTooLarge", "Diff too large")); |
| | | totalNofLinesCurrent = totalNofLinesPrevious; |
| | | isOff = true; |
| | | } |
| | |
| | | default: |
| | | return; |
| | | } |
| | | try { |
| | | os.write("<tr><td class='diff-cell' colspan='4'>".getBytes()); |
| | | os.write(StringUtils.escapeForHtml(message, false).getBytes()); |
| | | os.write("</td></tr>\n".getBytes()); |
| | | } catch (IOException ex) { |
| | | // Cannot happen with a ByteArrayOutputStream |
| | | } |
| | | writeFullWidthLine(message); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Writes a line spanning the full width of the code view, including the gutter. |
| | | * |
| | | * @param text |
| | | * to put on that line; will be HTML-escaped. |
| | | */ |
| | | private void writeFullWidthLine(String text) { |
| | | try { |
| | | os.write("<tr><td class='diff-cell' colspan='4'>".getBytes()); |
| | | os.write(StringUtils.escapeForHtml(text, false).getBytes()); |
| | | os.write("</td></tr>\n".getBytes()); |
| | | } catch (IOException ex) { |
| | | // Cannot happen with a ByteArrayOutputStream |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException { |
| | | if (nofLinesCurrent++ == 0) { |
| | |
| | | // Highlight trailing whitespace on deleted/added lines. |
| | | Matcher matcher = trailingWhitespace.matcher(line); |
| | | if (matcher.find()) { |
| | | StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), false)); |
| | | StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), CONVERT_TABS, tabLength)); |
| | | result.append("<span class='trailingws-").append(prefix == '+' ? "add" : "sub").append("'>"); |
| | | result.append(StringUtils.escapeForHtml(matcher.group(1), false)); |
| | | result.append("</span>"); |
| | | return result.toString(); |
| | | } |
| | | } |
| | | return StringUtils.escapeForHtml(line, false); |
| | | return StringUtils.escapeForHtml(line, CONVERT_TABS, tabLength); |
| | | } |
| | | |
| | | /** |
| | |
| | | String[] lines = html.split("\n"); |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (String line : lines) { |
| | | if (line.startsWith("index")) { |
| | | if (line.startsWith("index") || line.startsWith("similarity") |
| | | || line.startsWith("rename from ") || line.startsWith("rename to ")) { |
| | | // skip index lines |
| | | } else if (line.startsWith("new file") || line.startsWith("deleted file")) { |
| | | // skip new file lines |
| | |
| | | } else { |
| | | sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">"); |
| | | } |
| | | line = StringUtils.escapeForHtml(line.substring(1), false); |
| | | line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS, tabLength); |
| | | } |
| | | sb.append(line); |
| | | if (gitLinkDiff) { |
| | | sb.append("</td></tr>"); |
| | | } |
| | | sb.append('\n'); |
| | | } |
| | | } |
| | | if (truncated) { |