diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java index 6c65b5364..94fdc69e8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java @@ -52,6 +52,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.util.Iterator; import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.CommitBuilder; @@ -444,6 +445,83 @@ public class NoteMapTest extends RepositoryTestCase { assertEquals("empty tree", empty, n.getTree()); } + public void testIteratorEmptyMap() { + Iterator it = NoteMap.newEmptyMap().iterator(); + assertFalse(it.hasNext()); + } + + public void testIteratorFlatTree() throws Exception { + RevBlob a = tr.blob("a"); + RevBlob b = tr.blob("b"); + RevBlob data1 = tr.blob("data1"); + RevBlob data2 = tr.blob("data2"); + RevBlob nonNote = tr.blob("non note"); + + RevCommit r = tr.commit() // + .add(a.name(), data1) // + .add(b.name(), data2) // + .add("nonNote", nonNote) // + .create(); + tr.parseBody(r); + + Iterator it = NoteMap.read(reader, r).iterator(); + assertEquals(2, count(it)); + } + + public void testIteratorFanoutTree2_38() throws Exception { + RevBlob a = tr.blob("a"); + RevBlob b = tr.blob("b"); + RevBlob data1 = tr.blob("data1"); + RevBlob data2 = tr.blob("data2"); + RevBlob nonNote = tr.blob("non note"); + + RevCommit r = tr.commit() // + .add(fanout(2, a.name()), data1) // + .add(fanout(2, b.name()), data2) // + .add("nonNote", nonNote) // + .create(); + tr.parseBody(r); + + Iterator it = NoteMap.read(reader, r).iterator(); + assertEquals(2, count(it)); + } + + public void testIteratorFanoutTree2_2_36() throws Exception { + RevBlob a = tr.blob("a"); + RevBlob b = tr.blob("b"); + RevBlob data1 = tr.blob("data1"); + RevBlob data2 = tr.blob("data2"); + RevBlob nonNote = tr.blob("non note"); + + RevCommit r = tr.commit() // + .add(fanout(4, a.name()), data1) // + .add(fanout(4, b.name()), data2) // + .add("nonNote", nonNote) // + .create(); + tr.parseBody(r); + + Iterator it = NoteMap.read(reader, r).iterator(); + assertEquals(2, count(it)); + } + + public void testIteratorFullyFannedOut() throws Exception { + RevBlob a = tr.blob("a"); + RevBlob b = tr.blob("b"); + RevBlob data1 = tr.blob("data1"); + RevBlob data2 = tr.blob("data2"); + RevBlob nonNote = tr.blob("non note"); + + RevCommit r = tr.commit() // + .add(fanout(38, a.name()), data1) // + .add(fanout(38, b.name()), data2) // + .add("nonNote", nonNote) // + .create(); + tr.parseBody(r); + + Iterator it = NoteMap.read(reader, r).iterator(); + assertEquals(2, count(it)); + } + private RevCommit commitNoteMap(NoteMap map) throws IOException { tr.tick(600); @@ -469,4 +547,13 @@ public class NoteMapTest extends RepositoryTestCase { } return r.toString(); } + + private static int count(Iterator it) { + int c = 0; + while (it.hasNext()) { + c++; + it.next(); + } + return c; + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java index d365f9bfe..00b32132e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java @@ -47,7 +47,7 @@ import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.ObjectId; /** In-memory representation of a single note attached to one object. */ -class Note extends ObjectId { +public class Note extends ObjectId { private ObjectId data; /** @@ -63,7 +63,8 @@ class Note extends ObjectId { data = noteData; } - ObjectId getData() { + /** @return the note content */ + public ObjectId getData() { return data; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java index 6a7b5cffb..abde6db76 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java @@ -44,6 +44,7 @@ package org.eclipse.jgit.notes; import java.io.IOException; +import java.util.Iterator; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.IncorrectObjectTypeException; @@ -52,6 +53,7 @@ import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.AbbreviatedObjectId; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.MutableObjectId; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectReader; @@ -66,7 +68,7 @@ import org.eclipse.jgit.revwalk.RevTree; * is not released by this class. The caller should arrange for releasing the * shared {@code ObjectReader} at the proper times. */ -public class NoteMap { +public class NoteMap implements Iterable { /** * Construct a new empty note map. * @@ -165,6 +167,18 @@ public class NoteMap { this.reader = reader; } + /** + * @return an iterator that iterates over notes of this NoteMap. Non note + * entries are ignored by this iterator. + */ + public Iterator iterator() { + try { + return root.iterator(new MutableObjectId(), reader); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + /** * Lookup a note for a specific ObjectId. *