Browse Source

InitCommand: Don't leave Repository open after Git is closed

The InitCommand returns a Git that is instantiated with the newly
created Repository, but the Repository is not closed with the Git
resulting in resource leaks.

Create the Git with `closeRepo` set to true, such that the Repository
is also closed when the Git is closed.

Adjust the tests to use try-with-resource on the Git instance.

Change-Id: Ib26e7428c7d8840956d1edb09e53b93e23e6fe5a
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-4.11
David Pursehouse 7 years ago
parent
commit
f527d4548f
  1. 41
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java
  2. 5
      org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java

41
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java

@ -69,14 +69,14 @@ public class InitCommandTest extends RepositoryTestCase {
} }
@Test @Test
public void testInitRepository() throws IOException, JGitInternalException, public void testInitRepository()
GitAPIException { throws IOException, JGitInternalException, GitAPIException {
File directory = createTempDirectory("testInitRepository"); File directory = createTempDirectory("testInitRepository");
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setDirectory(directory); command.setDirectory(directory);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); assertNotNull(git.getRepository());
assertNotNull(repository); }
} }
@Test @Test
@ -89,9 +89,9 @@ public class InitCommandTest extends RepositoryTestCase {
assertTrue(directory.listFiles().length > 0); assertTrue(directory.listFiles().length > 0);
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setDirectory(directory); command.setDirectory(directory);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); assertNotNull(git.getRepository());
assertNotNull(repository); }
} }
@Test @Test
@ -101,11 +101,12 @@ public class InitCommandTest extends RepositoryTestCase {
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setDirectory(directory); command.setDirectory(directory);
command.setBare(true); command.setBare(true);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); Repository repository = git.getRepository();
assertNotNull(repository); assertNotNull(repository);
assertTrue(repository.isBare()); assertTrue(repository.isBare());
} }
}
// non-bare repos where gitDir and directory is set. Same as // non-bare repos where gitDir and directory is set. Same as
// "git init --separate-git-dir /tmp/a /tmp/b" // "git init --separate-git-dir /tmp/a /tmp/b"
@ -117,12 +118,13 @@ public class InitCommandTest extends RepositoryTestCase {
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setDirectory(wt); command.setDirectory(wt);
command.setGitDir(gitDir); command.setGitDir(gitDir);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); Repository repository = git.getRepository();
assertNotNull(repository); assertNotNull(repository);
assertEqualsFile(wt, repository.getWorkTree()); assertEqualsFile(wt, repository.getWorkTree());
assertEqualsFile(gitDir, repository.getDirectory()); assertEqualsFile(gitDir, repository.getDirectory());
} }
}
// non-bare repos where only gitDir is set. Same as // non-bare repos where only gitDir is set. Same as
// "git init --separate-git-dir /tmp/a" // "git init --separate-git-dir /tmp/a"
@ -135,13 +137,14 @@ public class InitCommandTest extends RepositoryTestCase {
File gitDir = createTempDirectory("testInitRepository/.git"); File gitDir = createTempDirectory("testInitRepository/.git");
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setGitDir(gitDir); command.setGitDir(gitDir);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); Repository repository = git.getRepository();
assertNotNull(repository); assertNotNull(repository);
assertEqualsFile(gitDir, repository.getDirectory()); assertEqualsFile(gitDir, repository.getDirectory());
assertEqualsFile(new File(reader.getProperty("user.dir")), assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getWorkTree()); repository.getWorkTree());
} }
}
// Bare repos where gitDir and directory is set will only work if gitDir and // Bare repos where gitDir and directory is set will only work if gitDir and
// directory is pointing to same dir. Same as // directory is pointing to same dir. Same as
@ -169,14 +172,15 @@ public class InitCommandTest extends RepositoryTestCase {
.getAbsolutePath()); .getAbsolutePath());
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setBare(false); command.setBare(false);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); Repository repository = git.getRepository();
assertNotNull(repository); assertNotNull(repository);
assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"), assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
repository.getDirectory()); repository.getDirectory());
assertEqualsFile(new File(reader.getProperty("user.dir")), assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getWorkTree()); repository.getWorkTree());
} }
}
// If neither directory nor gitDir is set in a bare repo make sure // If neither directory nor gitDir is set in a bare repo make sure
// worktree and gitDir are set correctly. Standard case. Same as // worktree and gitDir are set correctly. Standard case. Same as
@ -189,13 +193,14 @@ public class InitCommandTest extends RepositoryTestCase {
.getAbsolutePath()); .getAbsolutePath());
InitCommand command = new InitCommand(); InitCommand command = new InitCommand();
command.setBare(true); command.setBare(true);
Repository repository = command.call().getRepository(); try (Git git = command.call()) {
addRepoToClose(repository); Repository repository = git.getRepository();
assertNotNull(repository); assertNotNull(repository);
assertEqualsFile(new File(reader.getProperty("user.dir")), assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getDirectory()); repository.getDirectory());
assertNull(repository.getWorkTree()); assertNull(repository.getWorkTree());
} }
}
// In a non-bare repo when directory and gitDir is set then they shouldn't // In a non-bare repo when directory and gitDir is set then they shouldn't
// point to the same dir. Same as // point to the same dir. Same as

5
org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java

@ -75,6 +75,9 @@ public class InitCommand implements Callable<Git> {
* {@inheritDoc} * {@inheritDoc}
* <p> * <p>
* Executes the {@code Init} command. * Executes the {@code Init} command.
*
* @return a {@code Git} instance that owns the {@code Repository} that it
* wraps.
*/ */
@Override @Override
public Git call() throws GitAPIException { public Git call() throws GitAPIException {
@ -120,7 +123,7 @@ public class InitCommand implements Callable<Git> {
Repository repository = builder.build(); Repository repository = builder.build();
if (!repository.getObjectDatabase().exists()) if (!repository.getObjectDatabase().exists())
repository.create(bare); repository.create(bare);
return new Git(repository); return new Git(repository, true);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e); throw new JGitInternalException(e.getMessage(), e);
} }

Loading…
Cancel
Save