diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java index 3ecef453e..a4d2d7e79 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java @@ -195,6 +195,8 @@ public class TreeWalk implements AutoCloseable { private final ObjectReader reader; + private final boolean closeReader; + private final MutableObjectId idBuffer = new MutableObjectId(); private TreeFilter filter; @@ -217,22 +219,30 @@ public class TreeWalk implements AutoCloseable { * Create a new tree walker for a given repository. * * @param repo - * the repository the walker will obtain data from. + * the repository the walker will obtain data from. An + * ObjectReader will be created by the walker, and will be closed + * when the walker is closed. */ public TreeWalk(final Repository repo) { - this(repo.newObjectReader()); + this(repo.newObjectReader(), true); } /** * Create a new tree walker for a given repository. * * @param or - * the reader the walker will obtain tree data from. + * the reader the walker will obtain tree data from. The reader + * is not closed when the walker is closed. */ public TreeWalk(final ObjectReader or) { + this(or, false); + } + + private TreeWalk(final ObjectReader or, final boolean closeReader) { reader = or; filter = TreeFilter.ALL; trees = NO_TREES; + this.closeReader = closeReader; } /** @return the reader this walker is using to load objects. */ @@ -261,7 +271,9 @@ public class TreeWalk implements AutoCloseable { */ @Override public void close() { - reader.close(); + if (closeReader) { + reader.close(); + } } /**