import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
// should not get here
}
}
+
+ @Test
+ /**
+ * Tests that a repository with packfiles can be deleted after calling
+ * Git.close(). On Windows the first try to delete the worktree will fail
+ * (because file handles on packfiles are still open) but the second
+ * attempt after a close will succeed.
+ *
+ * @throws IOException
+ * @throws JGitInternalException
+ * @throws GitAPIException
+ */
+ public void testClose() throws IOException, JGitInternalException,
+ GitAPIException {
+ File workTree = db.getWorkTree();
+ Git git = Git.wrap(db);
+ git.gc().setExpire(null).call();
+ git.checkout().setName(git.getRepository().resolve("HEAD^").getName())
+ .call();
+ try {
+ FileUtils.delete(workTree, FileUtils.RECURSIVE);
+ } catch (IOException e) {
+ git.close();
+ FileUtils.delete(workTree, FileUtils.RECURSIVE);
+ }
+ }
}
return new Git(repo);
}
+ /**
+ * Frees resources held by the underlying {@link Repository} instance. It is
+ * recommended to call this method as soon as you don't need a reference to
+ * this {@link Git} instance and the underlying {@link Repository} instance
+ * anymore. This method closes the underlying object and ref databases. This
+ * will free memory and file handles. E.g. on Windows the repository will
+ * keep file handles on pack files unless you call this method. Such open
+ * file handles may for example prevent that the repository folder in the
+ * filesystem can be deleted.
+ * <p>
+ * After calling close() you should not use this {@link Git} instance and
+ * the underlying {@link Repository} instance anymore.
+ *
+ * @since 3.2
+ */
+ public void close() {
+ if (repo != null)
+ repo.close();
+ }
+
/**
* Returns a command object to execute a {@code clone} command
*