Milos Cubrilo
2015-01-11 a9a2ffcf9a34bd25fe2e05bfdd4cde74725bb17d
src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
@@ -19,8 +19,10 @@
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;
@@ -34,6 +36,7 @@
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;
@@ -71,7 +74,7 @@
    */
   private static final int GLOBAL_DIFF_LIMIT = 20000;
   private final ResettableByteArrayOutputStream os;
   private final DiffOutputStream os;
   private final DiffStat diffStat;
@@ -122,9 +125,45 @@
   /** 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();
   /**
    * 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) {
      super(new DiffOutputStream());
      this.os = (DiffOutputStream) getOutputStream();
      this.os.setFormatter(this, handler);
      this.diffStat = new DiffStat(commitId);
      // If we have a full commitdiff, install maxima to avoid generating a super-long diff listing that
      // will only tax the browser too much.
@@ -210,14 +249,14 @@
         }
         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><tbody>\n");
         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><br />\n".getBytes());
         os.write("</tbody></table></div>\n".getBytes());
      }
   }
@@ -235,13 +274,7 @@
   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;
      }
@@ -277,13 +310,7 @@
      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);
   }
   /**
@@ -352,6 +379,22 @@
         os.write(',');
         os.write(encodeASCII(cnt));
         break;
      }
   }
   /**
    * 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
      }
   }
@@ -453,13 +496,14 @@
            if (gitLinkDiff) {
               sb.append("</td></tr>");
            }
            sb.append('\n');
         }
      }
      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 (DiffEntry entry : skipped) {