Bring back the "No Differences" message when files are identical

In 642e17aa35 when we moved the
patch entity out of the database and started relying on Ehcache to
store it we broke the "No Differences" by providing a BINARY file
when they were identical.  Instead of showing "No Differences"
Gerrit gave us a unified file view with an empty diff header.

By tossing the FileHeader object and creating an empty byte array
for the header we can get the UI to render this case correctly again.

Change-Id: I36b0eac54bfe49f1210b912bab616dc3f36e5b02
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-09-03 09:27:16 -07:00
parent d1caed16cd
commit d1695d29a6
3 changed files with 32 additions and 62 deletions

View File

@@ -33,30 +33,30 @@ import org.spearce.jgit.lib.Constants;
import org.spearce.jgit.lib.FileMode;
import org.spearce.jgit.patch.CombinedFileHeader;
import org.spearce.jgit.patch.FileHeader;
import org.spearce.jgit.util.IntList;
import org.spearce.jgit.util.RawParseUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class PatchListEntry {
private static final byte[] EMPTY_HEADER = {};
static PatchListEntry empty(final String fileName) {
final StringBuilder buf = new StringBuilder();
buf.append("diff --git a/");
buf.append(fileName);
buf.append(" b/");
buf.append(fileName);
buf.append("\n\n");
return new PatchListEntry(parse(Constants.encode(buf.toString())));
return new PatchListEntry(ChangeType.MODIFIED, PatchType.UNIFIED, null,
fileName, EMPTY_HEADER, Collections.<Edit> emptyList());
}
private final ChangeType changeType;
private final PatchType patchType;
private final String oldName;
private final String newName;
private final FileHeader header;
private final byte[] header;
private final List<Edit> edits;
PatchListEntry(final FileHeader hdr) {
@@ -99,7 +99,7 @@ public class PatchListEntry {
private PatchListEntry(final ChangeType changeType,
final PatchType patchType, final String oldName, final String newName,
final FileHeader header, final List<Edit> edits) {
final byte[] header, final List<Edit> edits) {
this.changeType = changeType;
this.patchType = patchType;
this.oldName = oldName;
@@ -124,14 +124,21 @@ public class PatchListEntry {
return newName;
}
public FileHeader getFileHeader() {
return header;
}
public List<Edit> getEdits() {
return edits;
}
public List<String> getHeaderLines() {
final IntList m = RawParseUtils.lineMap(header, 0, header.length);
final List<String> headerLines = new ArrayList<String>(m.size() - 1);
for (int i = 1; i < m.size() - 1; i++) {
final int b = m.get(i);
final int e = m.get(i + 1);
headerLines.add(RawParseUtils.decode(Constants.CHARSET, header, b, e));
}
return headerLines;
}
Patch toPatch(final PatchSet.Id setId) {
final Patch p = new Patch(new Patch.Key(setId, getNewName()));
p.setChangeType(getChangeType());
@@ -145,9 +152,7 @@ public class PatchListEntry {
writeEnum(out, patchType);
writeString(out, oldName);
writeString(out, newName);
final int hdrLen = end(header) - header.getStartOffset();
writeBytes(out, header.getBuffer(), header.getStartOffset(), hdrLen);
writeBytes(out, header);
writeVarInt32(out, edits.size());
for (final Edit e : edits) {
@@ -163,7 +168,7 @@ public class PatchListEntry {
final PatchType patchType = readEnum(in, PatchType.values());
final String oldName = readString(in);
final String newName = readString(in);
final FileHeader hdr = parse(readBytes(in));
final byte[] hdr = readBytes(in);
final int editCount = readVarInt32(in);
final Edit[] editArray = new Edit[editCount];
@@ -179,21 +184,15 @@ public class PatchListEntry {
Collections.unmodifiableList(Arrays.asList(editArray)));
}
private static FileHeader parse(final byte[] buf) {
final org.spearce.jgit.patch.Patch p = new org.spearce.jgit.patch.Patch();
p.parse(buf, 0, buf.length);
return p.getFiles().get(0);
}
private static FileHeader compact(final FileHeader h) {
private static byte[] compact(final FileHeader h) {
final int end = end(h);
if (h.getStartOffset() == 0 && end == h.getBuffer().length) {
return h;
return h.getBuffer();
}
final byte[] buf = new byte[end - h.getStartOffset()];
System.arraycopy(h.getBuffer(), h.getStartOffset(), buf, 0, buf.length);
return parse(buf);
return buf;
}
private static int end(final FileHeader h) {

View File

@@ -21,6 +21,7 @@ import com.google.gerrit.client.data.SparseFileContent;
import com.google.gerrit.client.data.PatchScript.DisplayMethod;
import com.google.gerrit.client.patches.CommentDetail;
import com.google.gerrit.client.reviewdb.PatchLineComment;
import com.google.gerrit.client.reviewdb.Patch.PatchType;
import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.patch.PatchListEntry;
import com.google.gerrit.server.patch.Text;
@@ -37,13 +38,9 @@ import org.spearce.jgit.lib.FileMode;
import org.spearce.jgit.lib.ObjectId;
import org.spearce.jgit.lib.ObjectLoader;
import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.patch.CombinedFileHeader;
import org.spearce.jgit.patch.FileHeader;
import org.spearce.jgit.revwalk.RevTree;
import org.spearce.jgit.revwalk.RevWalk;
import org.spearce.jgit.treewalk.TreeWalk;
import org.spearce.jgit.util.IntList;
import org.spearce.jgit.util.RawParseUtils;
import java.io.IOException;
import java.util.ArrayList;
@@ -101,14 +98,13 @@ class PatchScriptBuilder {
PatchScript toPatchScript(final PatchListEntry contentWS,
final CommentDetail comments, final PatchListEntry contentAct)
throws IOException {
if (contentAct.getFileHeader() instanceof CombinedFileHeader) {
if (contentAct.getPatchType() == PatchType.N_WAY) {
// For a diff --cc format we don't support converting it into
// a patch script. Instead treat everything as a file header.
//
edits = Collections.emptyList();
packHeader(contentAct.getFileHeader());
return new PatchScript(header, settings, a.dst, b.dst, edits,
a.displayMethod, b.displayMethod);
return new PatchScript(contentAct.getHeaderLines(), settings, a.dst,
b.dst, Collections.<Edit> emptyList(), a.displayMethod,
b.displayMethod);
}
a.path = oldName(contentAct);
@@ -119,7 +115,7 @@ class PatchScriptBuilder {
edits = new ArrayList<Edit>(contentAct.getEdits());
ensureCommentsVisible(comments);
packHeader(contentAct.getFileHeader());
header.addAll(contentAct.getHeaderLines());
if (a.mode == FileMode.GITLINK || b.mode == FileMode.GITLINK) {
@@ -295,26 +291,6 @@ class PatchScriptBuilder {
return last.getBeginA() + (b - last.getEndB());
}
private void packHeader(final FileHeader fh) {
final byte[] buf = fh.getBuffer();
final IntList m = RawParseUtils.lineMap(buf, fh.getStartOffset(), end(fh));
for (int i = 1; i < m.size() - 1; i++) {
final int b = m.get(i);
final int e = m.get(i + 1);
header.add(RawParseUtils.decode(Constants.CHARSET, buf, b, e));
}
}
private static int end(final FileHeader h) {
if (h instanceof CombinedFileHeader) {
return h.getEndOffset();
}
if (!h.getHunks().isEmpty()) {
return h.getHunks().get(0).getStartOffset();
}
return h.getEndOffset();
}
private void packContent() {
EditList list = new EditList(edits, context(), a.src.size(), b.src.size());
for (final EditList.Hunk hunk : list.getHunks()) {