Callers like TreeWalk need to know if back(1) is going to be a valid operation for a given AbstractTreeIterator before they try to make a call to move the iterator backwards. The new method first() returns true only if the iterator is already positioned on its first entry, in which case a call to back(n) (for any n) is invalid. Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx> --- .../spearce/jgit/dircache/DirCacheIterator.java | 12 +++++++++++- .../jgit/treewalk/AbstractTreeIterator.java | 13 +++++++++++++ .../spearce/jgit/treewalk/CanonicalTreeParser.java | 5 +++++ .../spearce/jgit/treewalk/EmptyTreeIterator.java | 5 +++++ .../spearce/jgit/treewalk/WorkingTreeIterator.java | 5 +++++ 5 files changed, 39 insertions(+), 1 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheIterator.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheIterator.java index 84cefa5..8384723 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheIterator.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheIterator.java @@ -64,6 +64,9 @@ /** The tree this iterator is walking. */ private final DirCacheTree tree; + /** First position in this tree. */ + private final int treeStart; + /** Last position in this tree. */ private final int treeEnd; @@ -95,6 +98,7 @@ public DirCacheIterator(final DirCache dc) { cache = dc; tree = dc.getCacheTree(true); + treeStart = 0; treeEnd = tree.getEntrySpan(); subtreeId = new byte[Constants.OBJECT_ID_LENGTH]; if (!eof()) @@ -105,7 +109,8 @@ protected DirCacheIterator(final DirCacheIterator p, final DirCacheTree dct) { super(p, p.path, p.pathLen + 1); cache = p.cache; tree = dct; - treeEnd = p.ptr + tree.getEntrySpan(); + treeStart = p.ptr; + treeEnd = treeStart + tree.getEntrySpan(); subtreeId = p.subtreeId; ptr = p.ptr; parseEntry(); @@ -139,6 +144,11 @@ public int idOffset() { } @Override + public boolean first() { + return ptr == treeStart; + } + + @Override public boolean eof() { return ptr == treeEnd; } diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java index 8ec506c..c1b7ad8 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java @@ -340,6 +340,19 @@ public abstract AbstractTreeIterator createSubtreeIterator(Repository repo) throws IncorrectObjectTypeException, IOException; /** + * Is this tree iterator positioned on its first entry? + * <p> + * An iterator is positioned on the first entry if <code>back(1)</code> + * would be an invalid request as there is no entry before the current one. + * <p> + * An empty iterator (one with no entries) will be + * <code>first() && eof()</code>. + * + * @return true if the iterator is positioned on the first entry. + */ + public abstract boolean first(); + + /** * Is this tree iterator at its EOF point (no more entries)? * <p> * An iterator is at EOF if there is no current entry. diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java index 111d03b..dcc53cd 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java @@ -126,6 +126,11 @@ public int idOffset() { return nextPtr - Constants.OBJECT_ID_LENGTH; } + @Override + public boolean first() { + return currPtr == 0; + } + public boolean eof() { return currPtr == raw.length; } diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/EmptyTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/EmptyTreeIterator.java index 232e3b1..eaca04e 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/EmptyTreeIterator.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/EmptyTreeIterator.java @@ -79,6 +79,11 @@ public int idOffset() { } @Override + public boolean first() { + return true; + } + + @Override public boolean eof() { return true; } diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java index 41fd47b..9c53224 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java @@ -231,6 +231,11 @@ public int idOffset() { } @Override + public boolean first() { + return ptr == 0; + } + + @Override public boolean eof() { return ptr == entryCnt; } -- 1.6.0.87.g2858d -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html