| | |
| | | import static org.eclipse.jgit.lib.Constants.encodeASCII; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.MessageFormat; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import org.apache.wicket.Application; |
| | | import org.apache.wicket.Localizer; |
| | |
| | | import org.eclipse.jgit.diff.DiffEntry.ChangeType; |
| | | import org.eclipse.jgit.diff.DiffFormatter; |
| | | import org.eclipse.jgit.diff.RawText; |
| | | import org.eclipse.jgit.lib.Repository; |
| | | 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; |
| | | |
| | | /** |
| | | * Generates an html snippet of a diff in Gitblit's style, tracks changed paths, and calculates diff stats. |
| | | * |
| | | * |
| | | * @author James Moger |
| | | * @author Tom <tw201207@gmail.com> |
| | | * |
| | | * |
| | | */ |
| | | public class GitBlitDiffFormatter extends DiffFormatter { |
| | | |
| | | /** Regex pattern identifying trailing whitespace. */ |
| | | private static final Pattern trailingWhitespace = Pattern.compile("(\\s+?)\r?\n?$"); |
| | | |
| | | /** |
| | | * gitblit.properties key for the per-file limit on the number of diff lines. |
| | |
| | | */ |
| | | 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; |
| | | |
| | |
| | | private int truncateTo; |
| | | /** Whether we decided to truncate the commitdiff. */ |
| | | private boolean truncated; |
| | | /** If {@link #truncated}, contains all files skipped,possibly with a suffix message as value to be displayed. */ |
| | | private final Map<String, String> skipped = new HashMap<String, String>(); |
| | | /** 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(); |
| | | this.diffStat = new DiffStat(commitId); |
| | | 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, Repository repository, String path, BinaryDiffHandler handler, int tabLength) { |
| | | super(new DiffOutputStream()); |
| | | this.os = (DiffOutputStream) getOutputStream(); |
| | | this.os.setFormatter(this, handler); |
| | | this.diffStat = new DiffStat(commitId, repository); |
| | | 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); |
| | |
| | | |
| | | /** |
| | | * Determines a limit to use for HTML diff output. |
| | | * |
| | | * |
| | | * @param key |
| | | * to use to read the value from the GitBlit settings, if available. |
| | | * @param minimum |
| | |
| | | |
| | | /** |
| | | * Returns a localized message string, if there is a localization; otherwise the given default value. |
| | | * |
| | | * |
| | | * @param key |
| | | * message key for the message |
| | | * @param defaultValue |
| | |
| | | } else { |
| | | isOff = true; |
| | | } |
| | | if (isOff) { |
| | | if (ent.getChangeType().equals(ChangeType.DELETE)) { |
| | | skipped.put(ent.getOldPath(), getMsg("gb.diffDeletedFileSkipped", "(deleted file)")); |
| | | if (truncated) { |
| | | skipped.add(ent); |
| | | } else { |
| | | // Produce a header here and now |
| | | String path; |
| | | String id; |
| | | if (ChangeType.DELETE.equals(ent.getChangeType())) { |
| | | path = ent.getOldPath(); |
| | | id = ent.getOldId().name(); |
| | | } else { |
| | | skipped.put(ent.getNewPath(), null); |
| | | path = ent.getNewPath(); |
| | | id = ent.getNewId().name(); |
| | | } |
| | | StringBuilder sb = new StringBuilder(MessageFormat.format("<div class='header'><div class=\"diffHeader\" id=\"n{0}\"><i class=\"icon-file\"></i> ", id)); |
| | | sb.append(StringUtils.escapeForHtml(path, false)).append("</div></div>"); |
| | | sb.append("<div class=\"diff\"><table cellpadding='0'><tbody>\n"); |
| | | os.write(sb.toString().getBytes()); |
| | | } |
| | | // Keep formatting, but if off, don't produce anything anymore. We just keep on counting. |
| | | super.format(ent); |
| | | if (!truncated) { |
| | | // Close the table |
| | | os.write("</tbody></table></div>\n".getBytes()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * Output a hunk header |
| | | * |
| | | * |
| | | * @param aStartLine |
| | | * within first source |
| | | * @param aEndLine |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 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) { |
| | |
| | | os.write("<td class='diff-cell context2'>".getBytes()); |
| | | break; |
| | | } |
| | | String line = text.getString(cur); |
| | | line = StringUtils.escapeForHtml(line, false); |
| | | os.write(encode(line)); |
| | | os.write(encode(codeLineToHtml(prefix, text.getString(cur)))); |
| | | os.write("</td></tr>\n".getBytes()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Convert the given code line to HTML. |
| | | * |
| | | * @param prefix |
| | | * the diff prefix (+/-) indicating whether the line was added or removed. |
| | | * @param line |
| | | * the line to format as HTML |
| | | * @return the HTML-formatted line, safe for inserting as is into HTML. |
| | | */ |
| | | private String codeLineToHtml(final char prefix, final String line) { |
| | | if ((prefix == '+' || prefix == '-')) { |
| | | // 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()), 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, CONVERT_TABS, tabLength); |
| | | } |
| | | |
| | | /** |
| | | * Workaround function for complex private methods in DiffFormatter. This sets the html for the diff headers. |
| | | * |
| | | * |
| | | * @return |
| | | */ |
| | | public String getHtml() { |
| | | String html = RawParseUtils.decode(os.toByteArray()); |
| | | String[] lines = html.split("\n"); |
| | | StringBuilder sb = new StringBuilder(); |
| | | boolean inFile = false; |
| | | String oldnull = "a/dev/null"; |
| | | 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 if (line.startsWith("---") || line.startsWith("+++")) { |
| | | // skip --- +++ lines |
| | | } else if (line.startsWith("diff")) { |
| | | line = StringUtils.convertOctal(line); |
| | | if (line.indexOf(oldnull) > -1) { |
| | | // a is null, use b |
| | | line = line.substring(("diff --git " + oldnull).length()).trim(); |
| | | // trim b/ |
| | | line = line.substring(2).trim(); |
| | | } else { |
| | | // use a |
| | | line = line.substring("diff --git ".length()).trim(); |
| | | line = line.substring(line.startsWith("\"a/") ? 3 : 2); |
| | | line = line.substring(0, line.indexOf(" b/") > -1 ? line.indexOf(" b/") : line.indexOf("\"b/")).trim(); |
| | | } |
| | | |
| | | if (line.charAt(0) == '"') { |
| | | line = line.substring(1); |
| | | } |
| | | if (line.charAt(line.length() - 1) == '"') { |
| | | line = line.substring(0, line.length() - 1); |
| | | } |
| | | if (inFile) { |
| | | sb.append("</tbody></table></div>\n"); |
| | | inFile = false; |
| | | } |
| | | |
| | | sb.append(MessageFormat.format("<div class='header'><div class=\"diffHeader\" id=\"{0}\"><i class=\"icon-file\"></i> ", line)).append(line) |
| | | .append("</div></div>"); |
| | | sb.append("<div class=\"diff\">"); |
| | | sb.append("<table><tbody>"); |
| | | inFile = true; |
| | | // skip diff lines |
| | | } else { |
| | | boolean gitLinkDiff = line.length() > 0 && line.substring(1).startsWith("Subproject commit"); |
| | | if (gitLinkDiff) { |
| | |
| | | } else { |
| | | sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">"); |
| | | } |
| | | line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS, tabLength); |
| | | } |
| | | sb.append(line); |
| | | if (gitLinkDiff) { |
| | | sb.append("</td></tr>"); |
| | | } |
| | | sb.append('\n'); |
| | | } |
| | | } |
| | | sb.append("</tbody></table></div>"); |
| | | if (truncated) { |
| | | sb.append(MessageFormat.format("<div class='header'><div class='diffHeader'>{0}</div></div>", |
| | | StringUtils.escapeForHtml(getMsg("gb.diffTruncated", "Diff truncated after the above file"), false))); |
| | | // List all files not shown. We can be sure we do have at least one path in skipped. |
| | | sb.append("<div class='diff'><table><tbody><tr><td class='diff-cell' colspan='4'>"); |
| | | sb.append("<div class='diff'><table cellpadding='0'><tbody><tr><td class='diff-cell' colspan='4'>"); |
| | | String deletedSuffix = StringUtils.escapeForHtml(getMsg("gb.diffDeletedFileSkipped", "(deleted)"), false); |
| | | boolean first = true; |
| | | for (Map.Entry<String, String> s : skipped.entrySet()) { |
| | | for (DiffEntry entry : skipped) { |
| | | if (!first) { |
| | | sb.append('\n'); |
| | | } |
| | | String path = StringUtils.escapeForHtml(s.getKey(), false); |
| | | String comment = s.getValue(); |
| | | if (comment != null) { |
| | | sb.append("<span id='" + path + "'>" + path + ' ' + StringUtils.escapeForHtml(comment, false) + "</span>"); |
| | | if (ChangeType.DELETE.equals(entry.getChangeType())) { |
| | | sb.append("<span id=\"n" + entry.getOldId().name() + "\">" + StringUtils.escapeForHtml(entry.getOldPath(), false) + ' ' + deletedSuffix + "</span>"); |
| | | } else { |
| | | sb.append("<span id='" + path + "'>" + path + "</span>"); |
| | | sb.append("<span id=\"n" + entry.getNewId().name() + "\">" + StringUtils.escapeForHtml(entry.getNewPath(), false) + "</span>"); |
| | | } |
| | | first = false; |
| | | } |
| | | skipped.clear(); |
| | | sb.append("</td></tr></tbody></table></div>"); |
| | | } |
| | | return sb.toString(); |