Remove duplicated code in DiffFormatter
Instead of trying to stream out the header, we can drop a redundant code path by formatting the header into a temporary buffer and then streaming out the actual line differences later. Its a small amount of unnecessary work to buffer the file header, but these are typically very tiny so the cost to format and reparse is relatively low. Change-Id: Id14a527a74ee0bd7e07f46fdec760c22b02d5bdf Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
parent
1ea356b346
commit
797d5c4d40
|
@ -212,33 +212,8 @@ public void format(List<? extends DiffEntry> entries) throws IOException {
|
||||||
* be written to.
|
* be written to.
|
||||||
*/
|
*/
|
||||||
public void format(DiffEntry ent) throws IOException {
|
public void format(DiffEntry ent) throws IOException {
|
||||||
writeDiffHeader(out, ent);
|
FormatResult res = createFormatResult(ent);
|
||||||
|
format(res.header, res.a, res.b);
|
||||||
if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) {
|
|
||||||
writeGitLinkDiffText(out, ent);
|
|
||||||
} else {
|
|
||||||
if (db == null)
|
|
||||||
throw new IllegalStateException(
|
|
||||||
JGitText.get().repositoryIsRequired);
|
|
||||||
|
|
||||||
ObjectReader reader = db.newObjectReader();
|
|
||||||
byte[] aRaw, bRaw;
|
|
||||||
try {
|
|
||||||
aRaw = open(reader, ent.getOldMode(), ent.getOldId());
|
|
||||||
bRaw = open(reader, ent.getNewMode(), ent.getNewId());
|
|
||||||
} finally {
|
|
||||||
reader.release();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) {
|
|
||||||
out.write(encodeASCII("Binary files differ\n"));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
RawText a = rawTextFactory.create(aRaw);
|
|
||||||
RawText b = rawTextFactory.create(bRaw);
|
|
||||||
formatEdits(a, b, new MyersDiff(a, b).getEdits());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeGitLinkDiffText(OutputStream o, DiffEntry ent)
|
private void writeGitLinkDiffText(OutputStream o, DiffEntry ent)
|
||||||
|
@ -406,8 +381,8 @@ public void format(final FileHeader head, final RawText a, final RawText b)
|
||||||
if (!head.getHunks().isEmpty())
|
if (!head.getHunks().isEmpty())
|
||||||
end = head.getHunks().get(0).getStartOffset();
|
end = head.getHunks().get(0).getStartOffset();
|
||||||
out.write(head.getBuffer(), start, end - start);
|
out.write(head.getBuffer(), start, end - start);
|
||||||
|
if (head.getPatchType() == PatchType.UNIFIED)
|
||||||
formatEdits(a, b, head.toEditList());
|
formatEdits(a, b, head.toEditList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -603,6 +578,20 @@ protected void writeLine(final char prefix, final RawText text,
|
||||||
*/
|
*/
|
||||||
public FileHeader createFileHeader(DiffEntry ent) throws IOException,
|
public FileHeader createFileHeader(DiffEntry ent) throws IOException,
|
||||||
CorruptObjectException, MissingObjectException {
|
CorruptObjectException, MissingObjectException {
|
||||||
|
return createFormatResult(ent).header;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class FormatResult {
|
||||||
|
FileHeader header;
|
||||||
|
|
||||||
|
RawText a;
|
||||||
|
|
||||||
|
RawText b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FormatResult createFormatResult(DiffEntry ent) throws IOException,
|
||||||
|
CorruptObjectException, MissingObjectException {
|
||||||
|
final FormatResult res = new FormatResult();
|
||||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||||
final EditList editList;
|
final EditList editList;
|
||||||
final FileHeader.PatchType type;
|
final FileHeader.PatchType type;
|
||||||
|
@ -631,14 +620,15 @@ public FileHeader createFileHeader(DiffEntry ent) throws IOException,
|
||||||
editList = new EditList();
|
editList = new EditList();
|
||||||
type = PatchType.BINARY;
|
type = PatchType.BINARY;
|
||||||
} else {
|
} else {
|
||||||
RawText a = rawTextFactory.create(aRaw);
|
res.a = rawTextFactory.create(aRaw);
|
||||||
RawText b = rawTextFactory.create(bRaw);
|
res.b = rawTextFactory.create(bRaw);
|
||||||
editList = new MyersDiff(a, b).getEdits();
|
editList = new MyersDiff(res.a, res.b).getEdits();
|
||||||
type = PatchType.UNIFIED;
|
type = PatchType.UNIFIED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new FileHeader(buf.toByteArray(), editList, type);
|
res.header = new FileHeader(buf.toByteArray(), editList, type);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int findCombinedEnd(final List<Edit> edits, final int i) {
|
private int findCombinedEnd(final List<Edit> edits, final int i) {
|
||||||
|
|
Loading…
Reference in New Issue