Diff processing utilities
cover Diff processing utilities
Back in late 2008 Dscho started writing some basic diff routines for JGit. That work got partially shelved when he had to move onto more important day-job topics. This series is starting to revive some portions of his work, and bring it into JGit.
In particular I want at least this much merged, so I can use it in Gerrit Code Review to massage an existing patch file before I display it. Currently most of that logic is within Gerrit, and I'd much rather push it down into JGit where anyone can reuse it.
To be clear, this series *does not* contain a way to create a diff. Dscho wrote much more code which is not yet ready for inclusion.
After applying this series we have an EditList describing what happened in an existing patch we parsed with the existing Patch class, and a DiffFormatter which can be used to reflow a patch script if the repository has both the pre and post image objects.
The formatter part is really only useful if the repository created the patch script, or received the objects over a git native object transport, like Gerrit Code Review does with SSH.
Dscho, can you give us yea-or-nay for SBO lines for the patches I've attributed to you?
Johannes E. Schindelin (3):
Add diff.Edit to describe an edit region within a file Add diff.RawText to index a file content for later compares Add diff.DiffFormatter to create Git style unified patch scripts
Shawn O. Pearce (3):
Add set to IntList
Add diff.EditList to provide for a list of Edit instances Teach FileHeader, HunkHeader how to create an EditList
.../spearce/jgit/patch/testEditList_Types.patch | 24 +++ .../tst/org/spearce/jgit/diff/EditListTest.java | 121 +++++++++++++ .../tst/org/spearce/jgit/diff/EditTest.java | 139 ++++++++++++++ .../tst/org/spearce/jgit/diff/RawTextTest.java | 93 ++++++++++ .../tst/org/spearce/jgit/patch/EditListTest.java | 95 ++++++++++ .../tst/org/spearce/jgit/util/IntListTest.java | 21 +++ .../src/org/spearce/jgit/diff/DiffFormatter.java | 187 +++++++++++++++++++ .../src/org/spearce/jgit/diff/Edit.java | 189 ++++++++++++++++++++ .../src/org/spearce/jgit/diff/EditList.java | 93 ++++++++++ .../src/org/spearce/jgit/diff/RawText.java | 172 ++++++++++++++++++ .../src/org/spearce/jgit/diff/Sequence.java | 78 ++++++++ .../src/org/spearce/jgit/patch/FileHeader.java | 9 + .../src/org/spearce/jgit/patch/HunkHeader.java | 48 +++++ .../src/org/spearce/jgit/util/IntList.java | 17 ++ 14 files changed, 1286 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testEditList_Types.patch create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditTest.java create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/RawTextTest.java create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/patch/EditListTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/Edit.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/RawText.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/Sequence.java
PATCH 1/6 Add set to IntList
Some applications may wish to modify an int list.
--- .../tst/org/spearce/jgit/util/IntListTest.java | 21 ++++++++++++++++++++ .../src/org/spearce/jgit/util/IntList.java | 17 ++++++++++++++++ 2 files changed, 38 insertions(+), 0 deletions(-)
org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java +21 -0
@@ -144,6 +144,27 @@ public void testClear() { } } + public void testSet() { + final IntList i = new IntList(); + i.add(1); + assertEquals(1, i.size()); + assertEquals(1, i.get(0)); + + i.set(0, 5); + assertEquals(5, i.get(0)); + + try { + i.set(5, 5); + fail("accepted set of 5 beyond end of list"); + } catch (ArrayIndexOutOfBoundsException e){ + assertTrue(true); + } + + i.set(1, 2); + assertEquals(2, i.size()); + assertEquals(2, i.get(1)); + } + public void testToString() { final IntList i = new IntList(); i.add(1);
org.spearce.jgit/src/org/spearce/jgit/util/IntList.java +17 -0
@@ -94,6 +94,23 @@ public void add(final int n) { } /** + * Assign an entry in the list. + * + * @param index + * index to set, must be in the range [0, {@link #size()}). + * @param n + * value to store at the position. + */ + public void set(final int index, final int n) { + if (count < index) + throw new ArrayIndexOutOfBoundsException(index); + else if (count == index) + add(n); + else + entries[index] = n; + } + + /** * Pad the list with entries. * * @param toIndex
Signature · 1 line
1.6.3.rc3.212.g8c698
PATCH 2/6 Add diff.Edit to describe an edit region within a file
From: Johannes E. Schindelin <[email protected]>
This type represents a difference region detected between two files.
--- .../tst/org/spearce/jgit/diff/EditTest.java | 139 ++++++++++++++ .../src/org/spearce/jgit/diff/Edit.java | 189 ++++++++++++++++++++ 2 files changed, 328 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/Edit.java
org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditTest.java new file +139 -0
@@ -0,0 +1,139 @@ +/* + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.diff; + +import junit.framework.TestCase; + +public class EditTest extends TestCase { + public void testCreate() { + final Edit e = new Edit(1, 2, 3, 4); + assertEquals(1, e.getBeginA()); + assertEquals(2, e.getEndA()); + assertEquals(3, e.getBeginB()); + assertEquals(4, e.getEndB()); + } + + public void testCreateEmpty() { + final Edit e = new Edit(1, 3); + assertEquals(1, e.getBeginA()); + assertEquals(1, e.getEndA()); + assertEquals(3, e.getBeginB()); + assertEquals(3, e.getEndB()); + } + + public void testSwap() { + final Edit e = new Edit(1, 2, 3, 4); + e.swap(); + assertEquals(3, e.getBeginA()); + assertEquals(4, e.getEndA()); + assertEquals(1, e.getBeginB()); + assertEquals(2, e.getEndB()); + } + + public void testType_Insert() { + final Edit e = new Edit(1, 1, 1, 2); + assertSame(Edit.Type.INSERT, e.getType()); + } + + public void testType_Delete() { + final Edit e = new Edit(1, 2, 1, 1); + assertSame(Edit.Type.DELETE, e.getType()); + } + + public void testType_Replace() { + final Edit e = new Edit(1, 2, 1, 4); + assertSame(Edit.Type.REPLACE, e.getType()); + } + + public void testType_Empty() { + assertSame(Edit.Type.EMPTY, new Edit(1, 1, 2, 2).getType()); + assertSame(Edit.Type.EMPTY, new Edit(1, 2).getType()); + } + + public void testToString() { + final Edit e = new Edit(1, 2, 1, 4); + assertEquals("REPLACE(1-2,1-4)", e.toString()); + } + + public void testEquals1() { + final Edit e1 = new Edit(1, 2, 3, 4); + final Edit e2 = new Edit(1, 2, 3, 4); + + assertTrue(e1.equals(e1)); + assertTrue(e1.equals(e2)); + assertTrue(e2.equals(e1)); + assertEquals(e1.hashCode(), e2.hashCode()); + assertFalse(e1.equals("")); + } + + public void testNotEquals1() { + assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(0, 2, 3, 4))); + } + + public void testNotEquals2() { + assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 0, 3, 4))); + } + + public void testNotEquals3() { + assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 0, 4))); + } + + public void testNotEquals4() { + assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 3, 0))); + } + + public void testExtendA() { + final Edit e = new Edit(1, 2, 1, 1); + + e.extendA(); + assertEquals(new Edit(1, 3, 1, 1), e); + + e.extendA(); + assertEquals(new Edit(1, 4, 1, 1), e); + } + + public void testExtendB() { + final Edit e = new Edit(1, 2, 1, 1); + + e.extendB(); + assertEquals(new Edit(1, 2, 1, 2), e); + + e.extendB(); + assertEquals(new Edit(1, 2, 1, 3), e); + } +}
org.spearce.jgit/src/org/spearce/jgit/diff/Edit.java new file +189 -0
189 lines not on this page · raw message
Signature · 1 line
1.6.3.rc3.212.g8c698
PATCH 3/6 Add diff.EditList to provide for a list of Edit instances
Often a file difference involves more than one modified region, in which case we need more than one Edit instance to describe the full change that was made.
List<Edit> could be used with any generic List implementation, but we may want to offer additional API functionality that is Edit list specific, so I'm creating a specialized List implementation for it.
--- .../tst/org/spearce/jgit/diff/EditListTest.java | 121 ++++++++++++++++++++ .../src/org/spearce/jgit/diff/EditList.java | 93 +++++++++++++++ 2 files changed, 214 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java
org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java new file +121 -0
@@ -0,0 +1,121 @@ +/* + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.diff; + +import java.util.Iterator; + +import junit.framework.TestCase; + +public class EditListTest extends TestCase { + public void testEmpty() { + final EditList l = new EditList(); + assertEquals(0, l.size()); + assertTrue(l.isEmpty()); + assertEquals("EditList[]", l.toString()); + + assertTrue(l.equals(l)); + assertTrue(l.equals(new EditList())); + assertFalse(l.equals("")); + assertEquals(l.hashCode(), new EditList().hashCode()); + } + + public void testAddOne() { + final Edit e = new Edit(1, 2, 1, 1); + final EditList l = new EditList(); + l.add(e); + assertEquals(1, l.size()); + assertFalse(l.isEmpty()); + assertSame(e, l.get(0)); + assertSame(e, l.iterator().next()); + + assertTrue(l.equals(l)); + assertFalse(l.equals(new EditList())); + + final EditList l2 = new EditList(); + l2.add(e); + assertTrue(l.equals(l2)); + assertTrue(l2.equals(l)); + assertEquals(l.hashCode(), l2.hashCode()); + } + + public void testAddTwo() { + final Edit e1 = new Edit(1, 2, 1, 1); + final Edit e2 = new Edit(8, 8, 8, 12); + final EditList l = new EditList(); + l.add(e1); + l.add(e2); + assertEquals(2, l.size()); + assertSame(e1, l.get(0)); + assertSame(e2, l.get(1)); + + final Iterator<Edit> i = l.iterator(); + assertSame(e1, i.next()); + assertSame(e2, i.next()); + + assertTrue(l.equals(l)); + assertFalse(l.equals(new EditList())); + + final EditList l2 = new EditList(); + l2.add(e1); + l2.add(e2); + assertTrue(l.equals(l2)); + assertTrue(l2.equals(l)); + assertEquals(l.hashCode(), l2.hashCode()); + } + + public void testSet() { + final Edit e1 = new Edit(1, 2, 1, 1); + final Edit e2 = new Edit(3, 4, 3, 3); + final EditList l = new EditList(); + l.add(e1); + assertSame(e1, l.get(0)); + assertSame(e1, l.set(0, e2)); + assertSame(e2, l.get(0)); + } + + public void testRemove() { + final Edit e1 = new Edit(1, 2, 1, 1); + final Edit e2 = new Edit(8, 8, 8, 12); + final EditList l = new EditList(); + l.add(e1); + l.add(e2); + l.remove(e1); + assertEquals(1, l.size()); + assertSame(e2, l.get(0)); + } +}
org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
1.6.3.rc3.212.g8c698
PATCH 4/6 Add diff.RawText to index a file content for later compares
From: Johannes E. Schindelin <[email protected]>
The RawText class converts a byte[] into an indexed map of file lines, assuming the source content is a UNIX formatted text file. This fits in line with Git's usage of libxdiff, where typically the file content is treated as a UNIX formatted text file, unless binary content was previously detected in the file stream.
--- .../tst/org/spearce/jgit/diff/RawTextTest.java | 93 +++++++++++ .../src/org/spearce/jgit/diff/RawText.java | 172 ++++++++++++++++++++ .../src/org/spearce/jgit/diff/Sequence.java | 78 +++++++++ 3 files changed, 343 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/RawTextTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/RawText.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/Sequence.java
org.spearce.jgit.test/tst/org/spearce/jgit/diff/RawTextTest.java new file +93 -0
@@ -0,0 +1,93 @@ +/* + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.diff; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.spearce.jgit.lib.Constants; +import org.spearce.jgit.util.RawParseUtils; + +public class RawTextTest extends TestCase { + public void testEmpty() { + final RawText r = new RawText(new byte[0]); + assertEquals(0, r.size()); + } + + public void testEquals() { + final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n")); + final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n")); + + assertEquals(2, a.size()); + assertEquals(2, b.size()); + + // foo-a != foo-b + assertFalse(a.equals(0, b, 0)); + assertFalse(b.equals(0, a, 0)); + + // foo-b == foo-b + assertTrue(a.equals(1, b, 0)); + assertTrue(b.equals(0, a, 1)); + } + + public void testWriteLine1() throws IOException { + final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n")); + final ByteArrayOutputStream o = new ByteArrayOutputStream(); + a.writeLine(o, 0); + final byte[] r = o.toByteArray(); + assertEquals("foo-a", RawParseUtils.decode(r)); + } + + public void testWriteLine2() throws IOException { + final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b")); + final ByteArrayOutputStream o = new ByteArrayOutputStream(); + a.writeLine(o, 1); + final byte[] r = o.toByteArray(); + assertEquals("foo-b", RawParseUtils.decode(r)); + } + + public void testWriteLine3() throws IOException { + final RawText a = new RawText(Constants.encodeASCII("a\n\nb\n")); + final ByteArrayOutputStream o = new ByteArrayOutputStream(); + a.writeLine(o, 1); + final byte[] r = o.toByteArray(); + assertEquals("", RawParseUtils.decode(r)); + } +}
org.spearce.jgit/src/org/spearce/jgit/diff/RawText.java new file +172 -0
173 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/diff/Sequence.java new file +78 -0
79 lines not on this page · raw message
Signature · 1 line
1.6.3.rc3.212.g8c698
PATCH 5/6 Teach FileHeader, HunkHeader how to create an EditList
The EditList type along with its member Edit instances can be very useful when mining a patch for information about the change it is trying to represent to the content. This can be useful to create a modified version of the patch, such as with larger or smaller number of context lines.
--- .../spearce/jgit/patch/testEditList_Types.patch | 24 +++++ .../tst/org/spearce/jgit/patch/EditListTest.java | 95 ++++++++++++++++++++ .../src/org/spearce/jgit/patch/FileHeader.java | 9 ++ .../src/org/spearce/jgit/patch/HunkHeader.java | 48 ++++++++++ 4 files changed, 176 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testEditList_Types.patch create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/patch/EditListTest.java
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testEditList_Types.patch new file +24 -0
@@ -0,0 +1,24 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -2,2 +2,3 @@ a + b ++c + d +@@ -16,4 +17,2 @@ p + q +-r +-s + t +@@ -22,4 +21,8 @@ v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst/org/spearce/jgit/patch/EditListTest.java new file +95 -0
95 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +9 -0
@@ -52,6 +52,7 @@ import java.util.Collections; import java.util.List; +import org.spearce.jgit.diff.EditList; import org.spearce.jgit.lib.AbbreviatedObjectId; import org.spearce.jgit.lib.Constants; import org.spearce.jgit.lib.FileMode;
@@ -423,6 +424,14 @@ public BinaryHunk getReverseBinaryHunk() { return reverseBinaryHunk; } + /** @return a list describing the content edits performed on this file. */ + public EditList toEditList() { + final EditList r = new EditList(); + for (final HunkHeader hunk : hunks) + r.addAll(hunk.toEditList()); + return r; + } + /** * Parse a "diff --git" or "diff --cc" line. *
org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java +48 -0
60 lines not on this page · raw message
Signature · 1 line
1.6.3.rc3.212.g8c698
PATCH 6/6 Add diff.DiffFormatter to create Git style unified patch scripts
From: Johannes E. Schindelin <[email protected]>
Currently the formatter can only copy an existing FileHeader, one that has been parsed from an existing patch script. This makes it only useful for increasing or decreasing the number of lines of context, assuming we already have both the old and new contet images to match.
--- .../src/org/spearce/jgit/diff/DiffFormatter.java | 187 ++++++++++++++++++++ 1 files changed, 187 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java
org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java new file +187 -0
@@ -0,0 +1,187 @@ +/* + * Copyright (C) 2008, Johannes E. Schindelin <[email protected]> + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.diff; + +import static org.spearce.jgit.lib.Constants.encodeASCII; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; + +import org.spearce.jgit.patch.FileHeader; + +/** + * Format an {@link EditList} as a Git style unified patch script. + */ +public class DiffFormatter { + private int context; + + /** Create a new formatter with a default level of context. */ + public DiffFormatter() { + setContext(3); + } + + /** + * Change the number of lines of context to display. + * + * @param 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 setContext(final int lineCount) { + if (lineCount < 0) + throw new IllegalArgumentException("context must be >= 0"); + context = lineCount; + } + + /** + * Format a patch script, reusing a previously parsed FileHeader. + * <p> + * 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. + * + * @param out + * stream to write the patch script out to. + * @param head + * existing file header containing the header lines to copy. + * @param a + * text source for the pre-image version of the content. This + * must match the content of {@link FileHeader#getOldId()}. + * @param b + * text source for the post-image version of the content. This + * must match the content of {@link FileHeader#getNewId()}. + * @throws IOException + * writing to the supplied stream failed. + */ + public void format(final OutputStream out, final FileHeader head, + final RawText a, final RawText b) + throws IOException { + // Reuse the existing FileHeader as-is by blindly copying its + // header lines, but avoiding its hunks. Instead we recreate + // the hunks from the text instances we have been supplied. + // + final int start = head.getStartOffset(); + int end = head.getEndOffset(); + if (!head.getHunks().isEmpty()) + end = head.getHunks().get(0).getStartOffset(); + out.write(head.getBuffer(), start, end - start); + + formatEdits(out, a, b, head.toEditList()); + } + + private void formatEdits(final OutputStream out, final RawText a, + final RawText b, final EditList edits) throws IOException { + for (int curIdx = 0; curIdx < edits.size();) { + Edit curEdit = edits.get(curIdx); + final int endIdx = findCombinedEnd(edits, curIdx); + final Edit endEdit = edits.get(endIdx); + + int aCur = Math.max(0, curEdit.getBeginA() - context); + int bCur = Math.max(0, curEdit.getBeginB() - context); + final int aEnd = Math.min(a.size(), endEdit.getEndA() + context); + final int bEnd = Math.min(b.size(), endEdit.getEndB() + context); + + out.write('@'); + out.write('@'); + writeLineGroup(out, '-', aCur + 1, aEnd - aCur); + writeLineGroup(out, '+', bCur + 1, bEnd - bCur); + out.write(' '); + out.write('@'); + out.write('@'); + out.write('\n'); + + while (aCur < aEnd || bCur < bEnd) { + if (aCur < curEdit.getBeginA() || endIdx + 1 < curIdx) { + out.write(' '); + a.writeLine(out, aCur); + out.write('\n'); + aCur++; + bCur++; + + } else if (aCur < curEdit.getEndA()) { + out.write('-'); + a.writeLine(out, aCur); + out.write('\n'); + aCur++; + + } else if (bCur < curEdit.getEndB()) { + out.write('+'); + b.writeLine(out, bCur); + out.write('\n'); + bCur++; + } + + if (!in(curEdit, aCur, bCur) && ++curIdx < edits.size()) + curEdit = edits.get(curIdx); + } + } + } + + private static void writeLineGroup(final OutputStream out, + final char prefix, final int begin, final int end) + throws IOException { + out.write(' '); + out.write(prefix); + out.write(encodeASCII(begin)); + out.write(','); + out.write(encodeASCII(end)); + } + + private int findCombinedEnd(final List<Edit> edits, final int i) { + int end = i + 1; + while (end < edits.size() + && (combineA(edits, end) || combineB(edits, end))) + end++; + return end - 1; + } + + private boolean combineA(final List<Edit> e, final int i) { + return e.get(i).getBeginA() - e.get(i - 1).getEndA() <= 2 * context; + } + + private boolean combineB(final List<Edit> e, final int i) { + return e.get(i).getBeginB() - e.get(i - 1).getEndB() <= 2 * context; + } + + private static boolean in(final Edit edit, final int a, final int b) { + return a < edit.getEndA() && b < edit.getEndB(); + } +}
Signature · 1 line
1.6.3.rc3.212.g8c698
PATCH v2 6/6 Add diff.DiffFormatter to create Git style unified patch scripts
From: Johannes E. Schindelin <[email protected]>
Currently the formatter can only copy an existing FileHeader, one that has been parsed from an existing patch script. This makes it only useful for increasing or decreasing the number of lines of context, assuming we already have both the old and new content images to match.
---
Replacement fixes a lot of bugs, and includes a bunch of test cases that helped me find and fix those bugs in the first place. :-)
There's a quirk about setContext(0) differing in JGit and CGit, see the thread I just posted a short while ago.
.../tst-rsrc/org/spearce/jgit/diff/E.patch | 6 + .../tst-rsrc/org/spearce/jgit/diff/E_PostImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/X.patch | 24 ++ .../tst-rsrc/org/spearce/jgit/diff/X_PostImage | 28 +++ .../tst-rsrc/org/spearce/jgit/diff/X_PreImage | 25 +++ .../tst-rsrc/org/spearce/jgit/diff/Y.patch | 8 + .../tst-rsrc/org/spearce/jgit/diff/Y_PostImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/Y_PreImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/Z.patch | 8 + .../tst-rsrc/org/spearce/jgit/diff/Z_PostImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/Z_PreImage | 1 + .../org/spearce/jgit/diff/testContext1.out | 24 ++ .../org/spearce/jgit/diff/testContext10.out | 37 ++++ .../org/spearce/jgit/diff/testContext100.out | 37 ++++ .../org/spearce/jgit/diff/testContext3.out | 30 +++ .../org/spearce/jgit/diff/testContext5.out | 34 +++ .../spearce/jgit/diff/DiffFormatterReflowTest.java | 170 +++++++++++++++ .../src/org/spearce/jgit/diff/DiffFormatter.java | 220 ++++++++++++++++++++ .../src/org/spearce/jgit/diff/RawText.java | 12 + 19 files changed, 668 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext1.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext10.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext100.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext3.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext5.out create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/DiffFormatterReflowTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E.patch new file +6 -0
@@ -0,0 +1,6 @@ +diff --git a/E b/E +index e69de29..7898192 100644 +--- a/E ++++ b/E +@@ -0,0 +1 @@ ++a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PostImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PreImage new file +0 -0
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X.patch new file +24 -0
@@ -0,0 +1,24 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -2,2 +2,3 @@ a + b ++c + d +@@ -16,4 +17,2 @@ p + q +-r +-s + t +@@ -22,4 +21,8 @@ v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PostImage new file +28 -0
@@ -0,0 +1,28 @@ +a +b +c +d +e +f +g +h +i +j +k +l +m +n +o +p +q +t +u +v +w +0 +1 +2 +3 +4 +5 +z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PreImage new file +25 -0
@@ -0,0 +1,25 @@ +a +b +d +e +f +g +h +i +j +k +l +m +n +o +p +q +r +s +t +u +v +w +x +y +z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y.patch new file +8 -0
@@ -0,0 +1,8 @@ +diff --git a/Y b/Y +index 2e65efe..7898192 100644 +--- a/Y ++++ b/Y +@@ -1 +1 @@ +-a +\ No newline at end of file ++a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PostImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PreImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z.patch new file +8 -0
@@ -0,0 +1,8 @@ +diff --git a/Z b/Z +index 7898192..2e65efe 100644 +--- a/Z ++++ b/Z +@@ -1 +1 @@ +-a ++a +\ No newline at end of file
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PostImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PreImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext1.out new file +24 -0
@@ -0,0 +1,24 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -2,2 +2,3 @@ + b ++c + d +@@ -16,4 +17,2 @@ + q +-r +-s + t +@@ -22,4 +21,8 @@ + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext10.out new file +37 -0
@@ -0,0 +1,37 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,25 +1,28 @@ + a + b ++c + d + e + f + g + h + i + j + k + l + m + n + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext100.out new file +37 -0
@@ -0,0 +1,37 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,25 +1,28 @@ + a + b ++c + d + e + f + g + h + i + j + k + l + m + n + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext3.out new file +30 -0
@@ -0,0 +1,30 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,5 +1,6 @@ + a + b ++c + d + e + f +@@ -14,12 +15,14 @@ + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext5.out new file +34 -0
@@ -0,0 +1,34 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,7 +1,8 @@ + a + b ++c + d + e + f + g + h +@@ -12,14 +13,16 @@ + m + n + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst/org/spearce/jgit/diff/DiffFormatterReflowTest.java new file +170 -0
170 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java new file +220 -0
220 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/diff/RawText.java +12 -0
@@ -140,6 +140,18 @@ public void writeLine(final OutputStream out, final int i) out.write(content, start, end - start); } + /** + * Determine if the file ends with a LF ('\n'). + * + * @return true if the last line has an LF; false otherwise. + */ + public boolean isMissingNewlineAtEnd() { + final int end = lines.get(lines.size() - 1); + if (end == 0) + return true; + return content[end - 1] != '\n'; + } + private IntList computeHashes() { final IntList r = new IntList(lines.size()); r.add(0);
Signature · 1 line
1.6.3.rc4.190.g4648
PATCH v3 6/6 Add diff.DiffFormatter to create Git style unified patch scripts
From: Johannes E. Schindelin <[email protected]>
Currently the formatter can only copy an existing FileHeader, one that has been parsed from an existing patch script. This makes it only useful for increasing or decreasing the number of lines of context, assuming we already have both the old and new content images to match.
---
Quoted 2 lines · Shawn O. Pearce
There's a quirk about setContext(0) differing in JGit and CGit, see the thread I just posted a short while ago.
v3 fixes this issue, thanks to Junio's comments.
This includes 7/6, which now passes, and makes 8/6 obsolete.
.../tst-rsrc/org/spearce/jgit/diff/E.patch | 6 + .../tst-rsrc/org/spearce/jgit/diff/E_PostImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/X.patch | 24 +++ .../tst-rsrc/org/spearce/jgit/diff/X_PostImage | 28 +++ .../tst-rsrc/org/spearce/jgit/diff/X_PreImage | 25 +++ .../tst-rsrc/org/spearce/jgit/diff/Y.patch | 8 + .../tst-rsrc/org/spearce/jgit/diff/Y_PostImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/Y_PreImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/Z.patch | 8 + .../tst-rsrc/org/spearce/jgit/diff/Z_PostImage | 1 + .../tst-rsrc/org/spearce/jgit/diff/Z_PreImage | 1 + .../org/spearce/jgit/diff/testContext0.out | 18 ++ .../org/spearce/jgit/diff/testContext1.out | 24 +++ .../org/spearce/jgit/diff/testContext10.out | 37 ++++ .../org/spearce/jgit/diff/testContext100.out | 37 ++++ .../org/spearce/jgit/diff/testContext3.out | 30 +++ .../org/spearce/jgit/diff/testContext5.out | 34 +++ .../spearce/jgit/diff/DiffFormatterReflowTest.java | 176 ++++++++++++++++ .../src/org/spearce/jgit/diff/DiffFormatter.java | 214 ++++++++++++++++++++ .../src/org/spearce/jgit/diff/RawText.java | 12 + 20 files changed, 686 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z.patch create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PostImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PreImage create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext0.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext1.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext10.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext100.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext3.out create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext5.out create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/DiffFormatterReflowTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E.patch new file +6 -0
@@ -0,0 +1,6 @@ +diff --git a/E b/E +index e69de29..7898192 100644 +--- a/E ++++ b/E +@@ -0,0 +1 @@ ++a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PostImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/E_PreImage new file +0 -0
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X.patch new file +24 -0
@@ -0,0 +1,24 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -2,2 +2,3 @@ a + b ++c + d +@@ -16,4 +17,2 @@ p + q +-r +-s + t +@@ -22,4 +21,8 @@ v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PostImage new file +28 -0
@@ -0,0 +1,28 @@ +a +b +c +d +e +f +g +h +i +j +k +l +m +n +o +p +q +t +u +v +w +0 +1 +2 +3 +4 +5 +z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/X_PreImage new file +25 -0
@@ -0,0 +1,25 @@ +a +b +d +e +f +g +h +i +j +k +l +m +n +o +p +q +r +s +t +u +v +w +x +y +z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y.patch new file +8 -0
@@ -0,0 +1,8 @@ +diff --git a/Y b/Y +index 2e65efe..7898192 100644 +--- a/Y ++++ b/Y +@@ -1 +1 @@ +-a +\ No newline at end of file ++a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PostImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Y_PreImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z.patch new file +8 -0
@@ -0,0 +1,8 @@ +diff --git a/Z b/Z +index 7898192..2e65efe 100644 +--- a/Z ++++ b/Z +@@ -1 +1 @@ +-a ++a +\ No newline at end of file
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PostImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/Z_PreImage new file +1 -0
@@ -0,0 +1 @@ +a
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext0.out new file +18 -0
@@ -0,0 +1,18 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -2,0 +3 @@ ++c +@@ -17,2 +17,0 @@ +-r +-s +@@ -23,2 +22,6 @@ +-x +-y ++0 ++1 ++2 ++3 ++4 ++5
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext1.out new file +24 -0
@@ -0,0 +1,24 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -2,2 +2,3 @@ + b ++c + d +@@ -16,4 +17,2 @@ + q +-r +-s + t +@@ -22,4 +21,8 @@ + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext10.out new file +37 -0
@@ -0,0 +1,37 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,25 +1,28 @@ + a + b ++c + d + e + f + g + h + i + j + k + l + m + n + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext100.out new file +37 -0
@@ -0,0 +1,37 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,25 +1,28 @@ + a + b ++c + d + e + f + g + h + i + j + k + l + m + n + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext3.out new file +30 -0
@@ -0,0 +1,30 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,5 +1,6 @@ + a + b ++c + d + e + f +@@ -14,12 +15,14 @@ + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/diff/testContext5.out new file +34 -0
@@ -0,0 +1,34 @@ +diff --git a/X b/X +index a3648a1..2d44096 100644 +--- a/X ++++ b/X +@@ -1,7 +1,8 @@ + a + b ++c + d + e + f + g + h +@@ -12,14 +13,16 @@ + m + n + o + p + q +-r +-s + t + u + v + w +-x +-y ++0 ++1 ++2 ++3 ++4 ++5 + z
org.spearce.jgit.test/tst/org/spearce/jgit/diff/DiffFormatterReflowTest.java new file +176 -0
176 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/diff/DiffFormatter.java new file +214 -0
214 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/diff/RawText.java +12 -0
@@ -140,6 +140,18 @@ public void writeLine(final OutputStream out, final int i) out.write(content, start, end - start); } + /** + * Determine if the file ends with a LF ('\n'). + * + * @return true if the last line has an LF; false otherwise. + */ + public boolean isMissingNewlineAtEnd() { + final int end = lines.get(lines.size() - 1); + if (end == 0) + return true; + return content[end - 1] != '\n'; + } + private IntList computeHashes() { final IntList r = new IntList(lines.size()); r.add(0);
Signature · 1 line
1.6.3.rc4.190.g4648
Re: [JGIT PATCH 1/6] Add set to IntList
In reply to Shawn O. Pearcelördag 02 maj 2009 04:08:42 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 23 lines
Some applications may wish to modify an int list.
Signed-off-by Shawn O. Pearce <[email protected]>--- .../tst/org/spearce/jgit/util/IntListTest.java | 21 ++++++++++++++++++++ .../src/org/spearce/jgit/util/IntList.java | 17 ++++++++++++++++ 2 files changed, 38 insertions(+), 0 deletions(-)org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java +7
-0@@ -144,6 +144,27 @@ public void testClear() { } } + public void testSet() { + final IntList i = new IntList(); + i.add(1); + assertEquals(1, i.size()); + assertEquals(1, i.get(0)); + + i.set(0, 5);
org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java public void testClear() { · line 163
Quoted 10 lines
org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java +10
-0+ assertEquals(5, i.get(0)); + + try { + i.set(5, 5); + fail("accepted set of 5 beyond end of list"); + } catch (ArrayIndexOutOfBoundsException e){ + assertTrue(true); + } + + i.set(1, 2);
Oh, you grow the array here. Not obvious.
Quoted 17 lines
org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java +4
-0+ assertEquals(2, i.size()); + assertEquals(2, i.get(1)); + } + public void testToString() { final IntList i = new IntList(); i.add(1);org.spearce.jgit/src/org/spearce/jgit/util/IntList.java +2
-0@@ -94,6 +94,23 @@ public void add(final int n) { } /** + * Assign an entry in the list. + *
org.spearce.jgit/src/org/spearce/jgit/util/IntList.java public void add(final int n) { · line 108
Quoted 10 lines
org.spearce.jgit/src/org/spearce/jgit/util/IntList.java +10
-0+ * @param index + * index to set, must be in the range [0, {@link #size()}). + * @param n + * value to store at the position. + */ + public void set(final int index, final int n) { + if (count < index) + throw new ArrayIndexOutOfBoundsException(index); + else if (count == index) + add(n);
The interface is quite obscure here. One the one hand it checks for assignment outside the set but it does grow the array when only one entry is missing. The reader of the code won't see that easily. A different name and reflection of the behaviour in the javadoc is needed. The class javadoc says "A more efficient List<Integer> using a primitive integer array." and I know of no java.util.List's that expand implicitly.
-- robin
Re: [JGIT PATCH 1/6] Add set to IntList
Quoted 2 lines · Robin Rosenberg
l?rdag 02 maj 2009 04:08:42 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 1 line
Some applications may wish to modify an int list.
OK, as it turns out, this is used by code which Dscho wrote but I haven't packaged up as patches yet. And I don't think that even uses the weird set(size(), ...) expands the list rules I implemented. So drop this patch for now from the series and I'll respin it at a later date.
Quoted 3 lines
Quoted 3 lines
.../tst/org/spearce/jgit/util/IntListTest.java | 21 ++++++++++++++++++++ .../src/org/spearce/jgit/util/IntList.java | 17 ++++++++++++++++ 2 files changed, 38 insertions(+), 0 deletions(-)
Signature · 1 line
Shawn.
Re: [JGIT PATCH 1/6] Add set to IntList
Hi,
Quoted 8 lines · Shawn O. Pearce
Quoted 2 lines · Robin Rosenberg
l?rdag 02 maj 2009 04:08:42 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 1 line
Some applications may wish to modify an int list.
OK, as it turns out, this is used by code which Dscho wrote but I haven't packaged up as patches yet. And I don't think that even uses the weird
set(size(), ...)expands the list rules I implemented. So drop this patch for now from the series and I'll respin it at a later date.
Indeed, my code only needs add(int i), which should expand the array on demand (i.e. after a clear(), it should _not_ blindly resize the array).
Ciao,
Dscho
Re: [JGIT PATCH 1/6] Add set to IntList
Quoted 6 lines · Johannes Schindelin
Quoted 4 lines · Shawn O. Pearce
Quoted 2 lines · Robin Rosenberg
l?rdag 02 maj 2009 04:08:42 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 1 line
Some applications may wish to modify an int list.
Quoted 7 lines
Quoted 4 lines
OK, as it turns out, this is used by code which Dscho wrote but I haven't packaged up as patches yet. And I don't think that even uses the weird
set(size(), ...)expands the list rules I implemented. So drop this patch for now from the series and I'll respin it at a later date.Indeed, my code only needs add(int i), which should expand the array on demand (i.e. after a
clear(), it should _not_ blindly resize the array).
Really? When I was starting to bring your code in and moved from IntArray to IntList I had compile errors on set calls, so I added it to IntList.
Huh. Well, add(int) is already there, and clear() resets the size to 0 but doesn't resize the internal array, so future adds up to the prior capacity are free.
Signature · 1 line
Shawn.
Re: [JGIT PATCH 1/6] Add set to IntList
Hi,
Quoted 13 lines · Shawn O. Pearce
Quoted 12 lines · Johannes Schindelin
Quoted 9 lines · Shawn O. Pearce
Quoted 3 lines · Robin Rosenberg
l?rdag 02 maj 2009 04:08:42 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 1 line
Some applications may wish to modify an int list.
OK, as it turns out, this is used by code which Dscho wrote but I haven't packaged up as patches yet. And I don't think that even uses the weird
set(size(), ...)expands the list rules I implemented. So drop this patch for now from the series and I'll respin it at a later date.
Quoted 6 lines
Quoted 2 lines
Indeed, my code only needs add(int i), which should expand the array on demand (i.e. after a
clear(), it should _not_ blindly resize the array).Really? When I was starting to bring your code in and moved from IntArray to IntList I had compile errors on set calls, so I added it to IntList.
Well, in the code you have, that might be true, but as I promised, I picked up work on it again. And my new version only needs add() ;-)
Quoted 3 lines
Huh. Well, add(int) is already there, and
clear()resets the size to 0 but doesn't resize the internal array, so future adds up to the prior capacity are free.
Exactly as it should be!
Thanks,
Dscho
Hi,
Quoted 2 lines · Shawn O. Pearce
To be clear, this series *does not* contain a way to create a diff. Dscho wrote much more code which is not yet ready for inclusion.
Indeed. I'm on it.
Quoted 2 lines
Dscho, can you give us yea-or-nay for SBO lines for the patches I've attributed to you?
Yays!
(I had only time to look over them once, but as I am working on the diff stuff again, I think I'll be able to find bugs on the go ;-)
Many thanks to you and Robin for finding bugs early!
Ciao,
Dscho