diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java index ffab1a746..14e4fa6fd 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java @@ -745,14 +745,15 @@ public class PackWriter implements AutoCloseable { @NonNull Set want, @NonNull Set have, @NonNull Set shallow) throws IOException { - ObjectWalk ow; - if (shallowPack) { - ow = new DepthWalk.ObjectWalk(reader, depth - 1); - } else { - ow = new ObjectWalk(reader); + try (ObjectWalk ow = getObjectWalk()) { + ow.assumeShallow(shallow); + preparePack(countingMonitor, ow, want, have); } - ow.assumeShallow(shallow); - preparePack(countingMonitor, ow, want, have); + } + + private ObjectWalk getObjectWalk() { + return shallowPack ? new DepthWalk.ObjectWalk(reader, depth - 1) + : new ObjectWalk(reader); } /**