public class DiffFormatter
extends java.lang.Object
implements java.lang.AutoCloseable
Modifier and Type | Class and Description |
---|---|
private static class |
DiffFormatter.FormatResult |
Modifier and Type | Field and Description |
---|---|
private int |
abbreviationLength |
private int |
binaryFileThreshold |
private boolean |
closeReader |
private RawTextComparator |
comparator |
private int |
context |
private static int |
DEFAULT_BINARY_FILE_THRESHOLD |
private DiffAlgorithm |
diffAlgorithm |
private DiffConfig |
diffCfg |
private static byte[] |
EMPTY
Magic return content indicating it is empty or no content present.
|
private java.lang.String |
newPrefix |
private static byte[] |
noNewLine |
private java.lang.String |
oldPrefix |
private java.io.OutputStream |
out |
private TreeFilter |
pathFilter |
private ProgressMonitor |
progressMonitor |
private java.lang.Boolean |
quotePaths |
private ObjectReader |
reader |
private RenameDetector |
renameDetector |
private Repository |
repository |
private ContentSource.Pair |
source |
Constructor and Description |
---|
DiffFormatter(java.io.OutputStream out)
Create a new formatter with a default level of context.
|
Modifier and Type | Method and Description |
---|---|
private void |
assertHaveReader() |
void |
close() |
private boolean |
combineA(java.util.List<Edit> e,
int i) |
private boolean |
combineB(java.util.List<Edit> e,
int i) |
private DiffFormatter.FormatResult |
createFormatResult(DiffEntry ent) |
private java.util.List<DiffEntry> |
detectRenames(java.util.List<DiffEntry> files) |
private EditList |
diff(RawText a,
RawText b) |
private static boolean |
end(Edit edit,
int a,
int b) |
private int |
findCombinedEnd(java.util.List<Edit> edits,
int i) |
void |
flush()
Flush the underlying output stream of this formatter.
|
private java.lang.String |
format(AbbreviatedObjectId id) |
void |
format(AbstractTreeIterator a,
AbstractTreeIterator b)
Format the differences between two trees.
|
void |
format(AnyObjectId a,
AnyObjectId b)
Format the differences between two trees.
|
void |
format(DiffEntry ent)
Format a patch script for one file entry.
|
void |
format(EditList edits,
RawText a,
RawText b)
Formats a list of edits in unified diff format
|
void |
format(FileHeader head,
RawText a,
RawText b)
Format a patch script, reusing a previously parsed FileHeader.
|
void |
format(java.util.List<? extends DiffEntry> entries)
Format a patch script from a list of difference entries.
|
void |
format(RevTree a,
RevTree b)
Format the differences between two trees.
|
protected void |
formatGitDiffFirstHeaderLine(java.io.ByteArrayOutputStream o,
DiffEntry.ChangeType type,
java.lang.String oldPath,
java.lang.String newPath)
Output the first header line
|
private void |
formatHeader(java.io.ByteArrayOutputStream o,
DiffEntry ent) |
protected void |
formatIndexLine(java.io.OutputStream o,
DiffEntry ent)
Format index line
|
private void |
formatOldNewPaths(java.io.ByteArrayOutputStream o,
DiffEntry ent) |
private static TreeFilter |
getDiffTreeFilterFor(AbstractTreeIterator a,
AbstractTreeIterator b) |
java.lang.String |
getNewPrefix()
Get the prefix applied in front of new file paths.
|
java.lang.String |
getOldPrefix()
Get the prefix applied in front of old file paths.
|
protected java.io.OutputStream |
getOutputStream()
Get output stream
|
TreeFilter |
getPathFilter()
Get path filter
|
RenameDetector |
getRenameDetector()
Get rename detector
|
private boolean |
isAdd(java.util.List<DiffEntry> files) |
boolean |
isDetectRenames()
Get if rename detection is enabled
|
private static boolean |
isEndOfLineMissing(RawText text,
int line) |
private static boolean |
isRename(DiffEntry ent) |
private AbstractTreeIterator |
makeIteratorFromTreeOrNull(RevTree tree) |
private RawText |
open(DiffEntry.Side side,
DiffEntry entry) |
private java.lang.String |
quotePath(java.lang.String path) |
java.util.List<DiffEntry> |
scan(AbstractTreeIterator a,
AbstractTreeIterator b)
Determine the differences between two trees.
|
java.util.List<DiffEntry> |
scan(AnyObjectId a,
AnyObjectId b)
Determine the differences between two trees.
|
java.util.List<DiffEntry> |
scan(RevTree a,
RevTree b)
Determine the differences between two trees.
|
void |
setAbbreviationLength(int count)
Change the number of digits to show in an ObjectId.
|
void |
setBinaryFileThreshold(int threshold)
Set maximum file size for text files.
|
void |
setContext(int lineCount)
Change the number of lines of context to display.
|
void |
setDetectRenames(boolean on)
Enable or disable rename detection.
|
void |
setDiffAlgorithm(DiffAlgorithm alg)
Set the algorithm that constructs difference output.
|
void |
setDiffComparator(RawTextComparator cmp)
Set the line equivalence function for text file differences.
|
void |
setNewPrefix(java.lang.String prefix)
Set the prefix applied in front of new file paths.
|
void |
setOldPrefix(java.lang.String prefix)
Set the prefix applied in front of old file paths.
|
void |
setPathFilter(TreeFilter filter)
Set the filter to produce only specific paths.
|
void |
setProgressMonitor(ProgressMonitor pm)
Set the progress monitor for long running rename detection.
|
void |
setQuotePaths(boolean quote)
Sets whether or not path names should be quoted.
|
void |
setReader(ObjectReader reader,
Config cfg)
Set the repository the formatter can load object contents from.
|
private void |
setReader(ObjectReader reader,
Config cfg,
boolean closeReader) |
void |
setRepository(Repository repository)
Set the repository the formatter can load object contents from.
|
private ContentSource |
source(AbstractTreeIterator iterator) |
FileHeader |
toFileHeader(DiffEntry ent)
Creates a
FileHeader representing the
given DiffEntry |
private java.util.List<DiffEntry> |
updateFollowFilter(java.util.List<DiffEntry> files) |
protected void |
writeAddedLine(RawText text,
int line)
Output an added line.
|
protected void |
writeContextLine(RawText text,
int line)
Output a line of context (unmodified line).
|
private static byte[] |
writeGitLinkText(AbbreviatedObjectId id) |
protected void |
writeHunkHeader(int aStartLine,
int aEndLine,
int bStartLine,
int bEndLine)
Output a hunk header
|
protected void |
writeLine(char prefix,
RawText text,
int cur)
Write a standard patch script line.
|
private void |
writeRange(char prefix,
int begin,
int cnt) |
protected void |
writeRemovedLine(RawText text,
int line)
Output a removed line
|
private static final int DEFAULT_BINARY_FILE_THRESHOLD
private static final byte[] noNewLine
private static final byte[] EMPTY
private final java.io.OutputStream out
private ObjectReader reader
private boolean closeReader
private DiffConfig diffCfg
private int context
private int abbreviationLength
private DiffAlgorithm diffAlgorithm
private RawTextComparator comparator
private int binaryFileThreshold
private java.lang.String oldPrefix
private java.lang.String newPrefix
private TreeFilter pathFilter
private RenameDetector renameDetector
private ProgressMonitor progressMonitor
private ContentSource.Pair source
private Repository repository
private java.lang.Boolean quotePaths
public DiffFormatter(java.io.OutputStream out)
out
- the stream the formatter will write line data to. This stream
should have buffering arranged by the caller, as many small
writes are performed to it.protected java.io.OutputStream getOutputStream()
public void setRepository(Repository repository)
repository
- source repository holding referenced objects.public void setReader(ObjectReader reader, Config cfg)
reader
- source reader holding referenced objects. Caller is responsible
for closing the reader.cfg
- config specifying diff algorithm and rename detection options.private void setReader(ObjectReader reader, Config cfg, boolean closeReader)
public void setContext(int lineCount)
lineCount
- number of lines of context to see before the first
modification and after the last modification within a hunk of
the modified file.public void setAbbreviationLength(int count)
count
- number of digits to show in an ObjectId.public void setDiffAlgorithm(DiffAlgorithm alg)
alg
- the algorithm to produce text file differences.HistogramDiff
public void setDiffComparator(RawTextComparator cmp)
cmp
- The equivalence function used to determine if two lines of
text are identical. The function can be changed to ignore
various types of whitespace.RawTextComparator.DEFAULT
,
RawTextComparator.WS_IGNORE_ALL
,
RawTextComparator.WS_IGNORE_CHANGE
,
RawTextComparator.WS_IGNORE_LEADING
,
RawTextComparator.WS_IGNORE_TRAILING
public void setBinaryFileThreshold(int threshold)
threshold
- the limit, in bytes. Files larger than this size will be
assumed to be binary, even if they aren't.public void setOldPrefix(java.lang.String prefix)
prefix
- the prefix in front of old paths. Typically this is the
standard string "a/"
, but may be any prefix desired by
the caller. Must not be null. Use the empty string to have no
prefix at all.public java.lang.String getOldPrefix()
public void setNewPrefix(java.lang.String prefix)
prefix
- the prefix in front of new paths. Typically this is the
standard string "b/"
, but may be any prefix desired by
the caller. Must not be null. Use the empty string to have no
prefix at all.public java.lang.String getNewPrefix()
public boolean isDetectRenames()
public void setDetectRenames(boolean on)
setRepository(Repository)
. Once enabled the detector can be
configured away from its defaults by obtaining the instance directly from
getRenameDetector()
and invoking configuration.on
- if rename detection should be enabled.public RenameDetector getRenameDetector()
public void setProgressMonitor(ProgressMonitor pm)
pm
- progress monitor to receive rename detection status through.public void setQuotePaths(boolean quote)
By default the setting of git config core.quotePath
is active,
but this can be overridden through this method.
quote
- whether to quote path namespublic void setPathFilter(TreeFilter filter)
FollowFilter
, the filter path will be
updated during successive scan or format invocations. The updated path
can be obtained from getPathFilter()
.filter
- the tree filter to apply.public TreeFilter getPathFilter()
public void flush() throws java.io.IOException
java.io.IOException
- the stream's own flush method threw an exception.public void close()
Release the internal ObjectReader state.
close
in interface java.lang.AutoCloseable
public java.util.List<DiffEntry> scan(AnyObjectId a, AnyObjectId b) throws java.io.IOException
FileHeader
instances with a
complete edit list by calling toFileHeader(DiffEntry)
.
Either side may be null to indicate that the tree has beed added or removed. The diff will be computed against nothing.
a
- the old (or previous) side or nullb
- the new (or updated) side or nulljava.io.IOException
- trees cannot be read or file contents cannot be read.public java.util.List<DiffEntry> scan(RevTree a, RevTree b) throws java.io.IOException
FileHeader
instances with a
complete edit list by calling toFileHeader(DiffEntry)
.
Either side may be null to indicate that the tree has beed added or removed. The diff will be computed against nothing.
a
- the old (or previous) side or nullb
- the new (or updated) side or nulljava.io.IOException
- trees cannot be read or file contents cannot be read.private AbstractTreeIterator makeIteratorFromTreeOrNull(RevTree tree) throws IncorrectObjectTypeException, java.io.IOException
IncorrectObjectTypeException
java.io.IOException
public java.util.List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b) throws java.io.IOException
FileHeader
instances with a
complete edit list by calling toFileHeader(DiffEntry)
.a
- the old (or previous) side.b
- the new (or updated) side.java.io.IOException
- trees cannot be read or file contents cannot be read.private static TreeFilter getDiffTreeFilterFor(AbstractTreeIterator a, AbstractTreeIterator b)
private ContentSource source(AbstractTreeIterator iterator)
private java.util.List<DiffEntry> detectRenames(java.util.List<DiffEntry> files) throws java.io.IOException
java.io.IOException
private boolean isAdd(java.util.List<DiffEntry> files)
private java.util.List<DiffEntry> updateFollowFilter(java.util.List<DiffEntry> files)
private static boolean isRename(DiffEntry ent)
public void format(AnyObjectId a, AnyObjectId b) throws java.io.IOException
a
to make it
b
.
Either side may be null to indicate that the tree has beed added or removed. The diff will be computed against nothing.
a
- the old (or previous) side or nullb
- the new (or updated) side or nulljava.io.IOException
- trees cannot be read, file contents cannot be read, or the
patch cannot be output.public void format(RevTree a, RevTree b) throws java.io.IOException
a
to make it
b
.
Either side may be null to indicate that the tree has beed added or removed. The diff will be computed against nothing.
a
- the old (or previous) side or nullb
- the new (or updated) side or nulljava.io.IOException
- trees cannot be read, file contents cannot be read, or the
patch cannot be output.public void format(AbstractTreeIterator a, AbstractTreeIterator b) throws java.io.IOException
a
to make it
b
.
Either side may be null to indicate that the tree has beed added or removed. The diff will be computed against nothing.
a
- the old (or previous) side or nullb
- the new (or updated) side or nulljava.io.IOException
- trees cannot be read, file contents cannot be read, or the
patch cannot be output.public void format(java.util.List<? extends DiffEntry> entries) throws java.io.IOException
scan(AbstractTreeIterator, AbstractTreeIterator)
to have been
called first.entries
- entries describing the affected files.java.io.IOException
- a file's content cannot be read, or the output stream cannot
be written to.public void format(DiffEntry ent) throws java.io.IOException
ent
- the entry to be formatted.java.io.IOException
- a file's content cannot be read, or the output stream cannot
be written to.private static byte[] writeGitLinkText(AbbreviatedObjectId id)
private java.lang.String format(AbbreviatedObjectId id)
private java.lang.String quotePath(java.lang.String path)
public void format(FileHeader head, RawText a, RawText b) throws java.io.IOException
This formatter is primarily useful for editing an existing patch script to increase or reduce the number of lines of context within the script. All header lines are reused as-is from the supplied FileHeader.
head
- existing file header containing the header lines to copy.a
- text source for the pre-image version of the content. This
must match the content of
DiffEntry.getOldId()
.b
- text source for the post-image version of the content. This
must match the content of
DiffEntry.getNewId()
.java.io.IOException
- writing to the supplied stream failed.public void format(EditList edits, RawText a, RawText b) throws java.io.IOException
edits
- some differences which have been calculated between A and Ba
- the text A which was comparedb
- the text B which was comparedjava.io.IOException
protected void writeContextLine(RawText text, int line) throws java.io.IOException
text
- RawText for accessing raw dataline
- the line number within textjava.io.IOException
private static boolean isEndOfLineMissing(RawText text, int line)
protected void writeAddedLine(RawText text, int line) throws java.io.IOException
text
- RawText for accessing raw dataline
- the line number within textjava.io.IOException
protected void writeRemovedLine(RawText text, int line) throws java.io.IOException
text
- RawText for accessing raw dataline
- the line number within textjava.io.IOException
protected void writeHunkHeader(int aStartLine, int aEndLine, int bStartLine, int bEndLine) throws java.io.IOException
aStartLine
- within first sourceaEndLine
- within first sourcebStartLine
- within second sourcebEndLine
- within second sourcejava.io.IOException
private void writeRange(char prefix, int begin, int cnt) throws java.io.IOException
java.io.IOException
protected void writeLine(char prefix, RawText text, int cur) throws java.io.IOException
prefix
- prefix before the line, typically '-', '+', ' '.text
- the text object to obtain the line from.cur
- line number to output.java.io.IOException
- the stream threw an exception while writing to it.public FileHeader toFileHeader(DiffEntry ent) throws java.io.IOException, CorruptObjectException, MissingObjectException
FileHeader
representing the
given DiffEntry
This method does not use the OutputStream associated with this
DiffFormatter instance. It is therefore safe to instantiate this
DiffFormatter instance with a
DisabledOutputStream
if this method is
the only one that will be used.
ent
- the DiffEntry to create the FileHeader forHunkHeader
.java.io.IOException
- the stream threw an exception while writing to it, or one of
the blobs referenced by the DiffEntry could not be read.CorruptObjectException
- one of the blobs referenced by the DiffEntry is corrupt.MissingObjectException
- one of the blobs referenced by the DiffEntry is missing.private DiffFormatter.FormatResult createFormatResult(DiffEntry ent) throws java.io.IOException, CorruptObjectException, MissingObjectException
java.io.IOException
CorruptObjectException
MissingObjectException
private void assertHaveReader()
private RawText open(DiffEntry.Side side, DiffEntry entry) throws java.io.IOException, BinaryBlobException
java.io.IOException
BinaryBlobException
protected void formatGitDiffFirstHeaderLine(java.io.ByteArrayOutputStream o, DiffEntry.ChangeType type, java.lang.String oldPath, java.lang.String newPath) throws java.io.IOException
o
- The stream the formatter will write the first header line totype
- The DiffEntry.ChangeType
oldPath
- old path to the filenewPath
- new path to the filejava.io.IOException
- the stream threw an exception while writing to it.private void formatHeader(java.io.ByteArrayOutputStream o, DiffEntry ent) throws java.io.IOException
java.io.IOException
protected void formatIndexLine(java.io.OutputStream o, DiffEntry ent) throws java.io.IOException
o
- the stream the formatter will write line data toent
- the DiffEntry to create the FileHeader forjava.io.IOException
- writing to the supplied stream failed.private void formatOldNewPaths(java.io.ByteArrayOutputStream o, DiffEntry ent) throws java.io.IOException
java.io.IOException
private int findCombinedEnd(java.util.List<Edit> edits, int i)
private boolean combineA(java.util.List<Edit> e, int i)
private boolean combineB(java.util.List<Edit> e, int i)
private static boolean end(Edit edit, int a, int b)