diff --git a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index d99de8b..b2019e1 100644 --- a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -30,10 +30,7 @@ import org.eclipse.jgit.util.FS; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.DirectoryNotEmptyException; -import java.nio.file.Files; -import java.nio.file.NotDirectoryException; -import java.nio.file.Path; +import java.nio.file.*; /** * Initialise a new GitDB Repo. @@ -59,9 +56,10 @@ class InitGitDBRepo { final InitGitDBRepo initRepo = new InitGitDBRepo(); final File validDbDir = initRepo.validDbDir(dbDir.toFile()); validDbDir.mkdirs(); - final Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false); - repository.create(true); - initRepo.createInitialBranchOnMaster(repository); + try (final Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false)) { + repository.create(true); + initRepo.createInitialBranchOnMaster(repository); + } } private void createInitialBranchOnMaster(final Repository repository) throws IOException { @@ -102,8 +100,10 @@ class InitGitDBRepo { } private void verifyIsEmpty(final File dbDir) throws IOException { - if (Files.newDirectoryStream(dbDir.toPath()).iterator().hasNext()) { - throw new DirectoryNotEmptyException(dbDir.toString()); + try (final DirectoryStream directoryStream = Files.newDirectoryStream(dbDir.toPath())) { + if (directoryStream.iterator().hasNext()) { + throw new DirectoryNotEmptyException(dbDir.toString()); + } } } diff --git a/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java b/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java index 69f9d86..b27cdc3 100644 --- a/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java +++ b/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java @@ -22,10 +22,7 @@ package net.kemitix.gitdb.impl; import lombok.RequiredArgsConstructor; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.TreeFormatter; +import org.eclipse.jgit.lib.*; import java.io.IOException; import java.util.Optional; @@ -108,6 +105,8 @@ class KeyRemover { * @throws IOException the object could not be stored. */ private ObjectId insertTree(final TreeFormatter treeFormatter) throws IOException { - return repository.getObjectDatabase().newInserter().insert(treeFormatter); + try (final ObjectInserter inserter = repository.getObjectDatabase().newInserter()) { + return inserter.insert(treeFormatter); + } } }