diff options
author | Jeff Schumacher <jeffschu@google.com> | 2010-07-08 09:59:36 -0700 |
---|---|---|
committer | Jeff Schumacher <jeffschu@google.com> | 2010-07-08 16:58:55 -0700 |
commit | a8b29afd82795b3d98b42bf214fea27ab61984cc (patch) | |
tree | 68df10df1a09042f143bac43e47ddb9d1f250fd7 /org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java | |
parent | 1913b41bc749ed8a176d7ea8100fb4b260547523 (diff) | |
download | jgit-a8b29afd82795b3d98b42bf214fea27ab61984cc.tar.gz jgit-a8b29afd82795b3d98b42bf214fea27ab61984cc.zip |
Create FileHeader from DiffEntry
Added support for converting DiffEntrys to FileHeaders. FileHeaders
are DiffEntrys with a buffer containing the diff output as well as
a list of HunkHeaders. The HunkHeaders contain EditLists. The
createFileHeader(DiffEntry) method in DiffFormatter performs a Myers
Diff on the files refered to by the DiffEntry, then puts the returned
EditList into a single HunkHeader, which is then put into the
FileHeader to be returned. It also generates the appropriate diff
header an puts it into the FileHeader's buffer. The rest of the diff
output, which would normally be parsed to generate the HunkHeaders,
is not generated. In fact, the purpose of this method is to avoid
the costly diff output generation and parsing normally required to
create a FileHeader.
Change-Id: I7d8b18c0f6c85e3d02ad58995d3d231e69af5887
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java | 97 |
1 files changed, 58 insertions, 39 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java b/org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java index bfb20b64e9..a6ea74eb16 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/patch/HunkHeader.java @@ -116,6 +116,8 @@ public class HunkHeader { /** Total number of lines of context appearing in this hunk */ int nContext; + private EditList editList; + HunkHeader(final FileHeader fh, final int offset) { this(fh, offset, new OldImage() { @Override @@ -131,6 +133,21 @@ public class HunkHeader { old = oi; } + HunkHeader(final FileHeader fh, final EditList editList) { + this(fh, fh.buf.length); + this.editList = editList; + endOffset = startOffset; + nContext = 0; + if (editList.isEmpty()) { + newStartLine = 0; + newLineCount = 0; + } else { + newStartLine = editList.get(0).getBeginB(); + Edit last = editList.get(editList.size() - 1); + newLineCount = last.getEndB() - newStartLine; + } + } + /** @return header for the file this hunk applies to */ public FileHeader getFileHeader() { return file; @@ -173,48 +190,50 @@ public class HunkHeader { /** @return a list describing the content edits performed within the hunk. */ public EditList toEditList() { - final EditList r = new EditList(); - final byte[] buf = file.buf; - int c = nextLF(buf, startOffset); - int oLine = old.startLine; - int nLine = newStartLine; - Edit in = null; - - SCAN: for (; c < endOffset; c = nextLF(buf, c)) { - switch (buf[c]) { - case ' ': - case '\n': - in = null; - oLine++; - nLine++; - continue; - - case '-': - if (in == null) { - in = new Edit(oLine - 1, nLine - 1); - r.add(in); + if (editList == null) { + editList = new EditList(); + final byte[] buf = file.buf; + int c = nextLF(buf, startOffset); + int oLine = old.startLine; + int nLine = newStartLine; + Edit in = null; + + SCAN: for (; c < endOffset; c = nextLF(buf, c)) { + switch (buf[c]) { + case ' ': + case '\n': + in = null; + oLine++; + nLine++; + continue; + + case '-': + if (in == null) { + in = new Edit(oLine - 1, nLine - 1); + editList.add(in); + } + oLine++; + in.extendA(); + continue; + + case '+': + if (in == null) { + in = new Edit(oLine - 1, nLine - 1); + editList.add(in); + } + nLine++; + in.extendB(); + continue; + + case '\\': // Matches "\ No newline at end of file" + continue; + + default: + break SCAN; } - oLine++; - in.extendA(); - continue; - - case '+': - if (in == null) { - in = new Edit(oLine - 1, nLine - 1); - r.add(in); - } - nLine++; - in.extendB(); - continue; - - case '\\': // Matches "\ No newline at end of file" - continue; - - default: - break SCAN; } } - return r; + return editList; } void parseHeader() { |