/// <summary>
 /// Create a new iterator for an already loaded <see cref="DirCache"/> instance.
 /// <para/>
 /// The iterator implementation may copy part of the cache's data during
 /// construction, so the cache must be Read in prior to creating the
 /// iterator.
 /// </summary>
 /// <param name="parentIterator">The parent iterator</param>
 /// <param name="cacheTree">The cache tree</param>
 DirCacheBuildIterator(DirCacheBuildIterator parentIterator, DirCacheTree cacheTree)
     : base(parentIterator, cacheTree)
 {
     _builder = parentIterator._builder;
 }
 /// <summary>
 /// Create a new iterator for an already loaded <see cref="DirCache"/> instance.
 /// <para/>
 /// The iterator implementation may copy part of the cache's data during
 /// construction, so the cache must be Read in prior to creating the
 /// iterator.
 /// </summary>
 /// <param name="builder">
 /// The cache builder for the cache to walk. The cache must be
 /// already loaded into memory.
 /// </param>
 public DirCacheBuildIterator(DirCacheBuilder builder)
     : base(builder.getDirCache())
 {
     _builder = builder;
 }
		    protected override bool MergeImpl()
            {
			    _tw.reset();
			    _tw.addTree(MergeBase());
			    _tw.addTree(SourceTrees[0]);
			    _tw.addTree(SourceTrees[1]);

			    bool hasConflict = false;
			    _builder = _cache.builder();
			    while (_tw.next()) 
                {
				    int modeO = _tw.getRawMode(Ours);
				    int modeT = _tw.getRawMode(Theirs);
				    if (modeO == modeT && _tw.idEqual(Ours, Theirs)) 
                    {
					    Add(Ours, DirCacheEntry.STAGE_0);
					    continue;
				    }

				    int modeB = _tw.getRawMode(Base);
				    if (modeB == modeO && _tw.idEqual(Base, Ours))
					    Add(Theirs, DirCacheEntry.STAGE_0);
				    else if (modeB == modeT && _tw.idEqual(Base, Theirs))
					    Add(Ours, DirCacheEntry.STAGE_0);
				    else if (_tw.isSubtree()) 
                    {
					    if (NonTree(modeB)) 
                        {
						    Add(Base, DirCacheEntry.STAGE_1);
						    hasConflict = true;
					    }
					    if (NonTree(modeO)) 
                        {
						    Add(Ours, DirCacheEntry.STAGE_2);
						    hasConflict = true;
					    }
					    if (NonTree(modeT)) 
                        {
						    Add(Theirs, DirCacheEntry.STAGE_3);
						    hasConflict = true;
					    }
					    _tw.enterSubtree();
				    } 
                    else 
                    {
					    Add(Base, DirCacheEntry.STAGE_1);
					    Add(Ours, DirCacheEntry.STAGE_2);
					    Add(Theirs, DirCacheEntry.STAGE_3);
					    hasConflict = true;
				    }
			    }
			    _builder.finish();
			    _builder = null;

			    if (hasConflict)
				    return false;
			    try {
				    _resultTree = _cache.writeTree(GetObjectWriter());
				    return true;
			    } catch (UnmergedPathException) 
                {
				    _resultTree = null;
				    return false;
			    }
		    }