Remove empty iterator from TreeWalk

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>
This commit is contained in:
Shawn O. Pearce 2010-12-07 16:49:51 -08:00
parent 45731756a5
commit a02be9725c
21 changed files with 12 additions and 36 deletions

View File

@ -74,7 +74,6 @@ void tree_0(final AbstractTreeIterator c) {
@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);

View File

@ -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);

View File

@ -59,7 +59,6 @@
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 @@ private void run(Repository db) throws Exception {
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;

View File

@ -71,7 +71,6 @@ public void testPathFilterGroup_DoesNotSkipTail() throws Exception {
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

View File

@ -90,7 +90,6 @@ public void testTreeWalk_LsFiles() throws Exception {
{
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()) {

View File

@ -64,7 +64,6 @@ public void testEmptyTree_WithTreeWalk() throws Exception {
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 void testNoSubtree_WithTreeWalk() throws Exception {
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 void testSingleSubtree_NoRecursion() throws Exception {
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 void testSingleSubtree_Recursive() throws Exception {
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 void testTwoLevelSubtree_Recursive() throws Exception {
b.finish();
final TreeWalk tw = new TreeWalk(db);
tw.reset();
tw.addTree(new DirCacheIterator(dc));
tw.setRecursive(true);
int pathIdx = 0;

View File

@ -158,7 +158,6 @@ public void testWithSlashDoesNotMatchInSubDirectory() throws IOException {
private void beginWalk() throws CorruptObjectException {
walk = new TreeWalk(db);
walk.reset();
walk.addTree(new FileTreeIterator(db));
}

View File

@ -77,7 +77,6 @@ public void testIterator() throws IllegalStateException, IOException,
FileTreeIteratorWithTimeControl fileIt = new FileTreeIteratorWithTimeControl(
db, modTimes);
NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
tw.reset();
tw.addTree(fileIt);
tw.setRecursive(true);
FileTreeIterator t;

View File

@ -685,7 +685,6 @@ interface Checkout {
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;

View File

@ -82,7 +82,6 @@ public void testNoDF_NoGap() throws Exception {
}
final TreeWalk tw = new TreeWalk(db);
tw.reset();
tw.addTree(new DirCacheIterator(tree0));
tw.addTree(new DirCacheIterator(tree1));
@ -113,7 +112,6 @@ public void testDF_NoGap() throws Exception {
}
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
tw.reset();
tw.addTree(new DirCacheIterator(tree0));
tw.addTree(new DirCacheIterator(tree1));
@ -149,7 +147,6 @@ public void testDF_GapByOne() throws Exception {
}
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
tw.reset();
tw.addTree(new DirCacheIterator(tree0));
tw.addTree(new DirCacheIterator(tree1));
@ -185,7 +182,6 @@ public void testDF_SkipsSeenSubtree() throws Exception {
}
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
tw.reset();
tw.addTree(new DirCacheIterator(tree0));
tw.addTree(new DirCacheIterator(tree1));
@ -222,7 +218,6 @@ public void testDF_DetectConflict() throws Exception {
}
final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
tw.reset();
tw.addTree(new DirCacheIterator(tree0));
tw.addTree(new DirCacheIterator(tree1));

View File

@ -98,7 +98,6 @@ public void testNoPostOrder() throws Exception {
}
final TreeWalk tw = new TreeWalk(db);
tw.reset();
tw.setPostOrderTraversal(false);
tw.addTree(new DirCacheIterator(tree));
@ -127,7 +126,6 @@ public void testWithPostOrder_EnterSubtree() throws Exception {
}
final TreeWalk tw = new TreeWalk(db);
tw.reset();
tw.setPostOrderTraversal(true);
tw.addTree(new DirCacheIterator(tree));
@ -162,7 +160,6 @@ public void testWithPostOrder_NoEnterSubtree() throws Exception {
}
final TreeWalk tw = new TreeWalk(db);
tw.reset();
tw.setPostOrderTraversal(true);
tw.addTree(new DirCacheIterator(tree));

View File

@ -105,7 +105,7 @@ public void testMissingSubtree_DetectFileAdded_FileModified()
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);

View File

@ -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 void testALL_IdentityClone() throws Exception {
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));
}

View File

@ -138,7 +138,6 @@ public DirCache call() throws NoFilepatternException {
DirCacheBuilder builder = dc.builder();
final TreeWalk tw = new TreeWalk(repo);
tw.reset();
tw.addTree(new DirCacheBuildIterator(builder));
if (workingTreeIterator == null)
workingTreeIterator = new FileTreeIterator(repo);

View File

@ -431,7 +431,6 @@ public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b)
assertHaveRepository();
TreeWalk walk = new TreeWalk(reader);
walk.reset();
walk.addTree(a);
walk.addTree(b);
walk.setRecursive(true);

View File

@ -165,7 +165,6 @@ public void keep(final int pos, int cnt) {
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);

View File

@ -243,7 +243,6 @@ public void preScanTwoTrees() throws CorruptObjectException, IOException {
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 void prescanOneTree()
builder = dc.builder();
walk = new NameConflictTreeWalk(repo);
walk.reset();
walk.addTree(mergeCommitTree);
walk.addTree(new DirCacheBuildIterator(builder));
walk.addTree(workingTree);
@ -795,7 +793,6 @@ private void cleanUpConflicts() throws CheckoutConflictException {
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()));

View File

@ -169,7 +169,6 @@ public boolean diff() throws IOException {
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)

View File

@ -59,10 +59,10 @@
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 @@ protected boolean mergeImpl() throws IOException {
DirCacheBuildIterator buildIt = new DirCacheBuildIterator(builder);
tw = new NameConflictTreeWalk(db);
tw.reset();
tw.addTree(mergeBase());
tw.addTree(sourceTrees[0]);
tw.addTree(sourceTrees[1]);

View File

@ -112,7 +112,6 @@ private static class InCoreMerger extends ThreeWayMerger {
@Override
protected boolean mergeImpl() throws IOException {
tw.reset();
tw.addTree(mergeBase());
tw.addTree(sourceTrees[0]);
tw.addTree(sourceTrees[1]);

View File

@ -84,6 +84,8 @@
* 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 TreeWalk(final Repository repo) {
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 void setPostOrderTraversal(final boolean b) {
/** 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 void reset(final AnyObjectId id) throws MissingObjectException,
* @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;