Its confusing that a new TreeWalk() needs to have reset() invoked on it before addTree(). This is a historical accident caused by how TreeWalk was abused within ObjectWalk. Drop the initial empty tree from the TreeWalk and thus remove a number of pointless reset() operations from unit tests and some of the internal JGit code. Existing application code which is still calling reset() will simply be incurring a few unnecessary field assignments, but they should consider cleaning up their code in the future. Change-Id: I434e94ffa43491019e7dff52ca420a4d2245f48b Signed-off-by: Shawn O. Pearce <spearce@spearce.org>tags/v0.10.1
@@ -74,7 +74,6 @@ class DiffTree extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
final TreeWalk walk = new TreeWalk(db); | |||
walk.reset(); | |||
walk.setRecursive(recursive); | |||
for (final AbstractTreeIterator i : trees) | |||
walk.addTree(i); |
@@ -62,7 +62,6 @@ class LsTree extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
final TreeWalk walk = new TreeWalk(db); | |||
walk.reset(); // drop the first empty tree, which we do not need here | |||
walk.setRecursive(recursive); | |||
walk.addTree(tree); | |||
@@ -59,7 +59,6 @@ import org.eclipse.jgit.diff.RawText; | |||
import org.eclipse.jgit.diff.RawTextComparator; | |||
import org.eclipse.jgit.errors.LargeObjectException; | |||
import org.eclipse.jgit.lib.AbbreviatedObjectId; | |||
import org.eclipse.jgit.lib.AnyObjectId; | |||
import org.eclipse.jgit.lib.Constants; | |||
import org.eclipse.jgit.lib.FileMode; | |||
import org.eclipse.jgit.lib.MutableObjectId; | |||
@@ -190,7 +189,7 @@ class DiffAlgorithms extends TextBuiltin { | |||
RevCommit p = c.getParent(0); | |||
rw.parseHeaders(p); | |||
tw.reset(new AnyObjectId[] { p.getTree(), c.getTree() }); | |||
tw.reset(p.getTree(), c.getTree()); | |||
while (tw.next()) { | |||
if (!isFile(tw, 0) || !isFile(tw, 1)) | |||
continue; |
@@ -71,7 +71,6 @@ public class DirCacheBuilderIteratorTest extends RepositoryTestCase { | |||
final int expIdx = 2; | |||
final DirCacheBuilder b = dc.builder(); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheBuildIterator(b)); | |||
tw.setRecursive(true); | |||
tw.setFilter(PathFilterGroup.createFromStrings(Collections |
@@ -90,7 +90,6 @@ public class DirCacheCGitCompatabilityTest extends LocalDiskRepositoryTestCase { | |||
{ | |||
final Iterator<CGitIndexRecord> rItr = ls.values().iterator(); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.setRecursive(true); | |||
tw.addTree(new DirCacheIterator(dc)); | |||
while (rItr.hasNext()) { |
@@ -64,7 +64,6 @@ public class DirCacheIteratorTest extends RepositoryTestCase { | |||
assertEquals(0, dc.getEntryCount()); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(dc)); | |||
assertFalse(tw.next()); | |||
} | |||
@@ -112,7 +111,6 @@ public class DirCacheIteratorTest extends RepositoryTestCase { | |||
final DirCacheIterator i = new DirCacheIterator(dc); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(i); | |||
int pathIdx = 0; | |||
while (tw.next()) { | |||
@@ -149,7 +147,6 @@ public class DirCacheIteratorTest extends RepositoryTestCase { | |||
final DirCacheIterator i = new DirCacheIterator(dc); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(i); | |||
tw.setRecursive(false); | |||
int pathIdx = 0; | |||
@@ -189,7 +186,6 @@ public class DirCacheIteratorTest extends RepositoryTestCase { | |||
final DirCacheIterator i = new DirCacheIterator(dc); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(i); | |||
tw.setRecursive(true); | |||
int pathIdx = 0; | |||
@@ -223,7 +219,6 @@ public class DirCacheIteratorTest extends RepositoryTestCase { | |||
b.finish(); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(dc)); | |||
tw.setRecursive(true); | |||
int pathIdx = 0; |
@@ -158,7 +158,6 @@ public class IgnoreNodeTest extends RepositoryTestCase { | |||
private void beginWalk() throws CorruptObjectException { | |||
walk = new TreeWalk(db); | |||
walk.reset(); | |||
walk.addTree(new FileTreeIterator(db)); | |||
} | |||
@@ -77,7 +77,6 @@ public class RacyGitTests extends RepositoryTestCase { | |||
FileTreeIteratorWithTimeControl fileIt = new FileTreeIteratorWithTimeControl( | |||
db, modTimes); | |||
NameConflictTreeWalk tw = new NameConflictTreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(fileIt); | |||
tw.setRecursive(true); | |||
FileTreeIterator t; |
@@ -685,7 +685,6 @@ public abstract class ReadTreeTest extends RepositoryTestCase { | |||
public void assertWorkDir(HashMap<String, String> i) | |||
throws CorruptObjectException, IOException { | |||
TreeWalk walk = new TreeWalk(db); | |||
walk.reset(); | |||
walk.setRecursive(true); | |||
walk.addTree(new FileTreeIterator(db)); | |||
String expectedValue; |
@@ -82,7 +82,6 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(tree0)); | |||
tw.addTree(new DirCacheIterator(tree1)); | |||
@@ -113,7 +112,6 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(tree0)); | |||
tw.addTree(new DirCacheIterator(tree1)); | |||
@@ -149,7 +147,6 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(tree0)); | |||
tw.addTree(new DirCacheIterator(tree1)); | |||
@@ -185,7 +182,6 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(tree0)); | |||
tw.addTree(new DirCacheIterator(tree1)); | |||
@@ -222,7 +218,6 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(tree0)); | |||
tw.addTree(new DirCacheIterator(tree1)); | |||
@@ -98,7 +98,6 @@ public class PostOrderTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.setPostOrderTraversal(false); | |||
tw.addTree(new DirCacheIterator(tree)); | |||
@@ -127,7 +126,6 @@ public class PostOrderTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.setPostOrderTraversal(true); | |||
tw.addTree(new DirCacheIterator(tree)); | |||
@@ -162,7 +160,6 @@ public class PostOrderTreeWalkTest extends RepositoryTestCase { | |||
} | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(); | |||
tw.setPostOrderTraversal(true); | |||
tw.addTree(new DirCacheIterator(tree)); | |||
@@ -105,7 +105,7 @@ public class TreeWalkBasicDiffTest extends RepositoryTestCase { | |||
inserter.release(); | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.reset(new ObjectId[] { oldTree, newTree }); | |||
tw.reset(oldTree, newTree); | |||
tw.setRecursive(true); | |||
tw.setFilter(TreeFilter.ANY_DIFF); | |||
@@ -44,11 +44,13 @@ | |||
package org.eclipse.jgit.treewalk.filter; | |||
import org.eclipse.jgit.lib.RepositoryTestCase; | |||
import org.eclipse.jgit.treewalk.EmptyTreeIterator; | |||
import org.eclipse.jgit.treewalk.TreeWalk; | |||
public class TreeFilterTest extends RepositoryTestCase { | |||
public void testALL_IncludesAnything() throws Exception { | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.addTree(new EmptyTreeIterator()); | |||
assertTrue(TreeFilter.ALL.include(tw)); | |||
} | |||
@@ -62,11 +64,13 @@ public class TreeFilterTest extends RepositoryTestCase { | |||
public void testNotALL_IncludesNothing() throws Exception { | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.addTree(new EmptyTreeIterator()); | |||
assertFalse(TreeFilter.ALL.negate().include(tw)); | |||
} | |||
public void testANY_DIFF_IncludesSingleTreeCase() throws Exception { | |||
final TreeWalk tw = new TreeWalk(db); | |||
tw.addTree(new EmptyTreeIterator()); | |||
assertTrue(TreeFilter.ANY_DIFF.include(tw)); | |||
} | |||
@@ -138,7 +138,6 @@ public class AddCommand extends GitCommand<DirCache> { | |||
DirCacheBuilder builder = dc.builder(); | |||
final TreeWalk tw = new TreeWalk(repo); | |||
tw.reset(); | |||
tw.addTree(new DirCacheBuildIterator(builder)); | |||
if (workingTreeIterator == null) | |||
workingTreeIterator = new FileTreeIterator(repo); |
@@ -431,7 +431,6 @@ public class DiffFormatter { | |||
assertHaveRepository(); | |||
TreeWalk walk = new TreeWalk(reader); | |||
walk.reset(); | |||
walk.addTree(a); | |||
walk.addTree(b); | |||
walk.setRecursive(true); |
@@ -165,7 +165,6 @@ public class DirCacheBuilder extends BaseDirCacheEditor { | |||
public void addTree(final byte[] pathPrefix, final int stage, | |||
final ObjectReader reader, final AnyObjectId tree) throws IOException { | |||
final TreeWalk tw = new TreeWalk(reader); | |||
tw.reset(); | |||
tw.addTree(new CanonicalTreeParser(pathPrefix, reader, tree | |||
.toObjectId())); | |||
tw.setRecursive(true); |
@@ -243,7 +243,6 @@ public class DirCacheCheckout { | |||
walk = new NameConflictTreeWalk(repo); | |||
builder = dc.builder(); | |||
walk.reset(); | |||
addTree(walk, headCommitTree); | |||
addTree(walk, mergeCommitTree); | |||
walk.addTree(new DirCacheBuildIterator(builder)); | |||
@@ -285,7 +284,6 @@ public class DirCacheCheckout { | |||
builder = dc.builder(); | |||
walk = new NameConflictTreeWalk(repo); | |||
walk.reset(); | |||
walk.addTree(mergeCommitTree); | |||
walk.addTree(new DirCacheBuildIterator(builder)); | |||
walk.addTree(workingTree); | |||
@@ -795,7 +793,6 @@ public class DirCacheCheckout { | |||
private boolean isModified(String path) throws CorruptObjectException, IOException { | |||
NameConflictTreeWalk tw = new NameConflictTreeWalk(repo); | |||
tw.reset(); | |||
tw.addTree(new DirCacheIterator(dc)); | |||
tw.addTree(new FileTreeIterator(repo.getWorkTree(), repo.getFS(), | |||
WorkingTreeOptions.createDefaultInstance())); |
@@ -169,7 +169,6 @@ public class IndexDiff { | |||
boolean changesExist = false; | |||
DirCache dirCache = repository.readDirCache(); | |||
TreeWalk treeWalk = new TreeWalk(repository); | |||
treeWalk.reset(); | |||
treeWalk.setRecursive(true); | |||
// add the trees (tree, dirchache, workdir) | |||
if (tree != null) |
@@ -59,10 +59,10 @@ import java.util.Map; | |||
import org.eclipse.jgit.JGitText; | |||
import org.eclipse.jgit.diff.DiffAlgorithm; | |||
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm; | |||
import org.eclipse.jgit.diff.RawText; | |||
import org.eclipse.jgit.diff.RawTextComparator; | |||
import org.eclipse.jgit.diff.Sequence; | |||
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm; | |||
import org.eclipse.jgit.dircache.DirCache; | |||
import org.eclipse.jgit.dircache.DirCacheBuildIterator; | |||
import org.eclipse.jgit.dircache.DirCacheBuilder; | |||
@@ -183,7 +183,6 @@ public class ResolveMerger extends ThreeWayMerger { | |||
DirCacheBuildIterator buildIt = new DirCacheBuildIterator(builder); | |||
tw = new NameConflictTreeWalk(db); | |||
tw.reset(); | |||
tw.addTree(mergeBase()); | |||
tw.addTree(sourceTrees[0]); | |||
tw.addTree(sourceTrees[1]); |
@@ -112,7 +112,6 @@ public class StrategySimpleTwoWayInCore extends ThreeWayMergeStrategy { | |||
@Override | |||
protected boolean mergeImpl() throws IOException { | |||
tw.reset(); | |||
tw.addTree(mergeBase()); | |||
tw.addTree(sourceTrees[0]); | |||
tw.addTree(sourceTrees[1]); |
@@ -84,6 +84,8 @@ import org.eclipse.jgit.util.RawParseUtils; | |||
* permitted, even from concurrent threads. | |||
*/ | |||
public class TreeWalk { | |||
private static final AbstractTreeIterator[] NO_TREES = {}; | |||
/** | |||
* Open a tree walk and filter to exactly one path. | |||
* <p> | |||
@@ -226,7 +228,7 @@ public class TreeWalk { | |||
public TreeWalk(final ObjectReader or) { | |||
reader = or; | |||
filter = TreeFilter.ALL; | |||
trees = new AbstractTreeIterator[] { new EmptyTreeIterator() }; | |||
trees = NO_TREES; | |||
} | |||
/** @return the reader this walker is using to load objects. */ | |||
@@ -337,7 +339,7 @@ public class TreeWalk { | |||
/** Reset this walker so new tree iterators can be added to it. */ | |||
public void reset() { | |||
trees = new AbstractTreeIterator[0]; | |||
trees = NO_TREES; | |||
advance = false; | |||
depth = 0; | |||
} | |||
@@ -400,7 +402,7 @@ public class TreeWalk { | |||
* @throws IOException | |||
* a loose object or pack file could not be read. | |||
*/ | |||
public void reset(final AnyObjectId[] ids) throws MissingObjectException, | |||
public void reset(final AnyObjectId... ids) throws MissingObjectException, | |||
IncorrectObjectTypeException, CorruptObjectException, IOException { | |||
final int oldLen = trees.length; | |||
final int newLen = ids.length; |