Subject: Re: [JGIT PATCH 2/3] Fix ObjectWalk to handle single-entry subtrees correctly

From: "Shawn O. Pearce" <[email-masked]>
Date: Fri, 13 Mar 2009 11:37:03 -0700

"Shawn O. Pearce" <[email-masked]> wrote:
> 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 8028b14..5c331ca 100644
> --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java
> +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java
> @@ -145,8 +145,19 @@ public CanonicalTreeParser resetRoot(final Repository repo,
>  
>  	/** @return this iterator, or its parent, if the tree is at eof. */
>  	public CanonicalTreeParser next() {
> -		next(1);
> -		return eof() && parent != null ? (CanonicalTreeParser) parent : this;
> +		CanonicalTreeParser p = this;
> +		for (;;) {
> +			p.next(1);
> +			if (p.eof() && parent != null) {
> +				// Parent was left pointing at the entry for us; advance
> +				// the parent to the next entry, possibly unwinding many
> +				// levels up the tree.
> +				//
> +				p = (CanonicalTreeParser) p.parent;
> +				continue;
> +			}
> +			return p;
> +		}

The parent != null test is wrong, we need "p.parent" here:

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 5c331ca..7f89cff 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java
@@ -148,7 +148,7 @@ public CanonicalTreeParser next() {
 		CanonicalTreeParser p = this;
 		for (;;) {
 			p.next(1);
-			if (p.eof() && parent != null) {
+			if (p.eof() && p.parent != null) {
 				// Parent was left pointing at the entry for us; advance
 				// the parent to the next entry, possibly unwinding many
 				// levels up the tree.
-- 
Shawn.