diff.jsp revision 1465
<%--
$Id$
CDDL HEADER START
The contents of this file are subject to the terms of the
Common Development and Distribution License (the "License").
You may not use this file except in compliance with the License.
See LICENSE.txt included in this distribution for the specific
language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each
file and include the License file at LICENSE.txt.
If applicable, add the following below this CDDL HEADER, with the
fields enclosed by brackets "[]" replaced with your own identifying
information: Portions Copyright [yyyy] [name of copyright owner]
CDDL HEADER END
Copyright 2009 Sun Microsystems, Inc. All rights reserved.
Use is subject to license terms.
Portions Copyright 2011 Jens Elkner.
--%><%@page import="
java.io.BufferedReader,
java.io.FileNotFoundException,
java.io.InputStream,
java.io.InputStreamReader,
java.io.UnsupportedEncodingException,
java.net.URLDecoder,
java.util.ArrayList,
org.apache.commons.jrcs.diff.Chunk,
org.apache.commons.jrcs.diff.Delta,
org.apache.commons.jrcs.diff.Diff,
org.apache.commons.jrcs.diff.Revision,
org.opensolaris.opengrok.analysis.AnalyzerGuru,
org.opensolaris.opengrok.analysis.FileAnalyzer.Genre,
org.opensolaris.opengrok.web.DiffData,
org.opensolaris.opengrok.web.DiffType"
%><%!
private static final String getAnnotateRevision(DiffData dd) {
if (dd.type == DiffType.OLD || dd.type == DiffType.NEW) {
return "<script type=\"text/javascript\">/* <![CDATA[ */ "
+ "O.rev='r=" + (dd.type == DiffType.NEW ? dd.rev2 : dd.rev1)
+ "'; /* ]]> */</script>";
}
return "";
}
/**
* Dump unchanged lines of a file.
* @param line the current line number (start of dump)
* @param howMany number fo lines to dump.
* @param trailing number of trailing lines to print after a "hidden" block
* (leading is always 8).
* @param file source code lines of the file to partially dump.
* @param bn where to append the line numbers
* @param bs where to append the source code lines
* @param dd diff data needed to emit anchors for hidden blocks
* @param a if {code true}, line numbers are get an anchor named with
* the linenumber
* @throws IndexOutOfBoundsException if <var>file</var> contains less than
* <var>line</var> + <var>howMany</var> entries.
*/
private static final int dumpFile(int line, int howMany, int trailing,
String[] file, StringBuilder bn, StringBuilder bs, DiffData dd, boolean a)
{
int max = line + howMany;
if (dd.full || howMany < 20) {
line++;
for (;line <= max; line++) {
if (a) {
bn.append("<a name=\"").append(line).append("\">")
.append(line).append("</a>\n");
} else {
bn.append(line).append('\n');
}
bs.append(Util.htmlize(file[line-1])).append('\n');
}
} else {
// row n
for (int j = line+1; j <= line + 8; j++) {
if (a) {
bn.append("<a name=\"").append(j).append("\">")
.append(j).append("</a>\n");
} else {
bn.append(j).append('\n');
}
bs.append(Util.htmlize(file[j-1])).append('\n');
}
// should be row n+1
String eol = trailing > 0 ? "---\n\n" : "---\n";
bn.append('\n').append(eol);
bs.append("\n<b> ").append(howMany - 8 - trailing)
.append(" unchanged lines hidden</b> (<a href=\"")
.append(dd.reqURI).append("?r1=").append(dd.rp1)
.append("&amp;r2=").append(dd.rp2)
.append("&amp;format=").append(dd.type.getAbbrev())
.append("&amp;full=1#").append(line)
.append("\">view full</a>) ").append(eol);
line += howMany - trailing + 1;
// should be row n+2
for (; line <= max; line++) {
if (a) {
bn.append("<a name=\"").append(line).append("\">")
.append(line).append("</a>\n");
} else {
bn.append(line).append('\n');
}
bs.append(Util.htmlize(file[line-1])).append('\n');
}
}
return --line;
}
private static final int dumpChunk(int cn, int cl, int line, String[] file,
StringBuilder bn, StringBuilder bs, boolean a)
{
line++;
for (int j = cn; j <= cl ; j++, line++) {
if (a) {
bn.append("<a name=\"").append(line).append("\">")
.append(line).append("</a>\n");
} else {
bn.append(line).append('\n');
}
bs.append(file[j]).append('\n');
}
return --line;
}
private static final String basename(String path) {
int idx = path.lastIndexOf('/');
if (idx < 0) {
return path;
}
return path.substring(idx+1);
}
%><%@
include file="mast.jsp"
%><%
/* ---------------------- diff.jsp start --------------------- */
{
cfg = PageConfig.get(request);
DiffData dd = cfg.getDiffData();
if (dd.errorMsg != null) {
%>
<div class="src">
<h3 class="error">Error:</h3>
<p><%= dd.errorMsg %></p>
</div><%
} else if (dd.genre == Genre.IMAGE) {
String link = request.getContextPath() + Prefix.RAW_P;
int idx1 = dd.rp1.indexOf('@');
int idx2 = dd.rp2.indexOf('@');
%>
<div id="difftable">
<table class="dtimage">
<thead>
<tr><th title="<%= Util.htmlize(dd.fp1) %>"
><%= Util.htmlize(basename(dd.fp1)) %> (revision <%= dd.rev1 %>)</th>
<th title="<%= Util.htmlize(dd.fp2) %>"
><%= Util.htmlize(basename(dd.fp2)) %> (revision <%= dd.rev2 %>)</th>
</tr>
</thead>
<tbody>
<tr><td class="dti"><img src="<%= link + dd.rp1.substring(0, idx1)
%>?r=<%= dd.rp1.substring(idx1 + 2) %>"/></td>
<td class="dti"><img src="<%= link + dd.rp2.substring(0, idx2)
%>?r=<%= dd.rp2.substring(idx2 + 2) %>"/></td>
</tr>
</tbody>
</table>
</div><%
} else if (dd.genre != Genre.PLAIN && dd.genre != Genre.HTML) {
String link = request.getContextPath() + Prefix.RAW_P;
int idx1 = dd.rp1.indexOf('@');
int idx2 = dd.rp2.indexOf('@');
%>
<div id="src">Diffs for binary files cannot be displayed! Files are <a
href="<%= link + dd.rp1.substring(0, idx1) %>?r=<%= dd.rp1.substring(idx1 + 2)
%>" title="<%= Util.htmlize(dd.fp1) %>"
><%= Util.htmlize(basename(dd.fp1)) %>(revision <%= dd.rev1 %>)</a> and <a
href="<%= link + dd.rp2.substring(0, idx2) %>?r=<%= dd.rp2.substring(idx2 + 2)
%>" title="<%= Util.htmlize(dd.fp2) %>"
><%= Util.htmlize(basename(dd.fp2)) %>(revision <%= dd.rev2 %>)</a>.
</div><%
} else if (dd.revision.size() == 0) {
%>
<%= getAnnotateRevision(dd) %>
<b>No differences found!</b><%
} else {
//-------- Do THE DIFFS ------------
%>
<%= getAnnotateRevision(dd) %>
<div id="diffbar">
<div class="dblegend">
<span class="m">Deleted</span>
<span class="p">Added</span>
</div>
<div class="dbtabs"><%
for (DiffType t : DiffType.values()) {
if (dd.type == t) {
%> <span class="active dbtab"><%= t.toString() %><%
if (t == DiffType.OLD) {
%> ( <%= dd.rev1 %> )<%
} else if (t == DiffType.NEW) {
%> ( <%= dd.rev2 %> )<%
}
%></span><%
} else {
%> <span class="dbtab"><a href="<%= dd.reqURI %>?r1=<%= dd.rp1
%>&amp;r2=<%= dd.rp2 %>&amp;format=<%= t.getAbbrev()
%>&amp;full=<%= dd.full ? '1' : '0' %>"><%= t.toString() %><%
if (t == DiffType.OLD) {
%> ( <%= dd.rev1 %> )<%
} else if (t == DiffType.NEW) {
%> ( <%= dd.rev2 %> )<%
}
%></a></span><%
}
}
%></div>
<div class="dbformats"><%
if (!dd.full) {
%>
<span class="dbformat"><a href="<%= dd.reqURI %>?r1=<%= dd.rp1
%>&amp;r2=<%= dd.rp2 %>&amp;format=<%= dd.type.getAbbrev()
%>&amp;full=1">full</a></span>
<span class="active dbformat">compact</span><%
} else {
%>
<span class="active dbformat">full</span>
<span class="dbformat"> <a href="<%= dd.reqURI %>?r1=<%= dd.rp1
%>&amp;r2=<%= dd.rp2 %>&amp;format=<%= dd.type.getAbbrev()
%>&amp;full=0">compact</a></span><%
}
%></div>
</div>
<div id="difftable"><%
if (dd.type != DiffType.TEXT) {
%><table id="<%= "dt" + dd.type %>"><%
if (dd.type == DiffType.SIDEBYSIDE) {
%>
<thead><tr>
<th colspan="2" title="<%= Util.htmlize(dd.fp1) %>"
><%= Util.htmlize(basename(dd.fp1)) %> (<%= dd.rev1 %>)</th>
<th colspan="2" title="<%= Util.htmlize(dd.fp2) %>"
><%= Util.htmlize(basename(dd.fp2)) %> (<%= dd.rev2 %>)</th>
</tr></thead><%
}
%>
<tbody><%
}
StringBuilder bs1 = new StringBuilder(256);
StringBuilder bs2 = new StringBuilder(256);
StringBuilder bn1 = new StringBuilder(32);
StringBuilder bn2 = new StringBuilder(32);
int ln1 = 0;
int ln2 = 0;
String[] file1 = dd.file[0];
String[] file2 = dd.file[1];
for (int i=0; i < dd.revision.size(); i++) {
Delta d = dd.revision.getDelta(i);
if (dd.type == DiffType.TEXT) {
%><%= Util.htmlize(d.toString()) %><%
continue;
}
Chunk c1 = d.getOriginal();
Chunk c2 = d.getRevised();
int cn1 = c1.first();
int cl1 = c1.last();
int cn2 = c2.first();
int cl2 = c2.last();
int i1 = cn1, i2 = cn2;
// changed
for (; i1 <= cl1 && i2 <= cl2; i1++, i2++) {
bs1.setLength(0);
bs2.setLength(0);
Util.htmlize(file1[i1], bs1);
Util.htmlize(file2[i2], bs2);
String[] ss = Util.diffline(bs1, bs2);
file1[i1] = ss[0];
file2[i2] = ss[1];
}
// deleted
for (; i1 <= cl1; i1++) {
bs1.setLength(0);
bs1.append(Util.SPAN_D);
Util.htmlize(file1[i1], bs1);
file1[i1] = bs1.append("</span>").toString();
}
// added
for (; i2 <= cl2; i2++) {
bs2.setLength(0);
bs2.append(Util.SPAN_A);
Util.htmlize(file2[i2], bs2);
file2[i2] = bs2.append("</span>").toString();
}
bn2.setLength(0);
bs2.setLength(0);
int td = 1;
String tClass = " class='dtk'";
if (dd.type == DiffType.OLD && cn1 > ln1) {
tClass = "";
ln1 = dumpFile(ln1, cn1 - ln1, 8, file1, bn2, bs2, dd, true);
} else if (dd.type == DiffType.NEW && cn2 > ln2) {
tClass = "";
ln2 = dumpFile(ln2, cn2 - ln2, 8, file2, bn2, bs2, dd, true);
} else if (dd.type == DiffType.UNIFIED && (cn1 > ln1 || cn2 > ln2)) {
ln2 = dumpFile(ln2, cn2 - ln2, 8, file2, bn2, bs2, dd, true);
ln1 = cn1;
} else if (dd.type == DiffType.SIDEBYSIDE && (cn1 > ln1 || cn2 > ln2)) {
bn1.setLength(0);
bs1.setLength(0);
boolean old = dd.full; // force same dump strategy for both
dd.full |= cn2 - ln2 < 20;
ln1 = dumpFile(ln1, cn1 - ln1, 8, file1, bn1, bs1, dd, false);
dd.full = old; // restore
ln2 = dumpFile(ln2, cn2 - ln2, 8, file2, bn2, bs2, dd, true);
td++;
} else {
td = 0;
}
if (td > 0) {
%>
<tr <%= tClass %>><%
if (td > 1) {
%>
<td class="dtn"><%= bn1 %></td>
<td class="dts"><%= bs1 %></td><%
}
%>
<td class="dtn"><%= bn2 %></td>
<td class="dts"><%= bs2 %></td>
</tr><%
}
bn2.setLength(0);
bs2.setLength(0);
td = 1;
tClass = " class='dtk'";
String tdClass = "dtp";
if (dd.type == DiffType.OLD && cn1 <= cl1) {
tdClass = "dtm"; tClass = "";
ln1 = dumpChunk(cn1, cl1, ln1, file1, bn2, bs2, true);
} else if (dd.type == DiffType.NEW && cn2 <= cl2) {
tClass = "";
ln2 = dumpChunk(cn2, cl2, ln2, file2, bn2, bs2, true);
} else if (dd.type == DiffType.UNIFIED) {
td = 0;
if (cn1 <= cl1) {
ln1 = dumpChunk(cn1, cl1, ln1, file1, bn2, bs2, false);
%>
<tr>
<td class="dtm"><%= bn2 %></td>
<td class="dts"><%= bs2 %></td>
</tr><%
}
if (cn2 <= cl2) {
bn2.setLength(0);
bs2.setLength(0);
ln2 = dumpChunk(cn2, cl2, ln2, file2, bn2, bs2, true);
td = 1;
}
} else if (dd.type == DiffType.SIDEBYSIDE
&& (cn1 <= cl1 || cn2 <= cl2))
{
bn1.setLength(0);
bs1.setLength(0);
ln1 = dumpChunk(cn1, cl1, ln1, file1, bn1, bs1, false);
ln2 = dumpChunk(cn2, cl2, ln2, file2, bn2, bs2, true);
td++;
} else {
td = 0;
}
if (td > 0) {
%>
<tr <%= tClass %>><%
if (td > 1) {
%>
<td class="dtm"><%= bn1 %></td>
<td class="dts"><%= bs1 %></td><%
}
%>
<td class="<%= tdClass %>"><%= bn2 %></td>
<td class="dts"><%= bs2 %></td>
</tr><%
}
} // for
// deltas done, dump the remaining
if (file1.length >= ln1) {
bn2.setLength(0);
bs2.setLength(0);
int td = 1;
if (dd.type == DiffType.OLD && file1.length > ln1) {
ln1 = dumpFile(ln1, file1.length - ln1, 0, file1, bn2, bs2, dd, true);
} else if (dd.type == DiffType.NEW && file2.length > ln2) {
ln2 = dumpFile(ln2, file2.length - ln2, 0, file2, bn2, bs2, dd, true);
} else if (dd.type == DiffType.UNIFIED && file2.length > ln2) {
ln2 = dumpFile(ln2, file2.length - ln2, 0, file2, bn2, bs2, dd, true);
} else if (dd.type == DiffType.SIDEBYSIDE
&& (file1.length > ln1 || file2.length > ln2))
{
bn1.setLength(0);
bs1.setLength(0);
boolean old = dd.full; // force same dump strategy for both
dd.full |= file1.length - ln1 < 20;
ln1 = dumpFile(ln1, file1.length - ln1, 0, file1, bn1, bs1, dd, false);
dd.full = old; // restore
ln2 = dumpFile(ln2, file2.length - ln2, 0, file2, bn2, bs2, dd, true);
td++;
} else {
td = 0;
}
if (td > 0) {
%>
<tr><%
if (td > 1) {
%>
<td class="dtn"><%= bn1 %></td>
<td class="dts"><%= bs1 %></td><%
}
%>
<td class="dtn"><%= bn2 %></td>
<td class="dts"><%= bs2 %></td>
</tr><%
}
} // EOFs
if (dd.type != DiffType.TEXT ) {
%>
</tbody>
</table><%
}
//----DIFFS Done--------
%></div><%
}
}
/* ---------------------- diff.jsp end --------------------- */
%><%@
include file="foot.jspf"
%>