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

This commit is contained in:
David Pursehouse 2018-02-14 18:18:35 -05:00 committed by Gerrit Code Review @ Eclipse.org
commit 184ddc5afa
2 changed files with 45 additions and 37 deletions

View File

@ -69,14 +69,14 @@ public void setUp() throws Exception {
}
@Test
public void testInitRepository() throws IOException, JGitInternalException,
GitAPIException {
public void testInitRepository()
throws IOException, JGitInternalException, GitAPIException {
File directory = createTempDirectory("testInitRepository");
InitCommand command = new InitCommand();
command.setDirectory(directory);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
try (Git git = command.call()) {
assertNotNull(git.getRepository());
}
}
@Test
@ -89,9 +89,9 @@ public void testInitNonEmptyRepository() throws IOException,
assertTrue(directory.listFiles().length > 0);
InitCommand command = new InitCommand();
command.setDirectory(directory);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
try (Git git = command.call()) {
assertNotNull(git.getRepository());
}
}
@Test
@ -101,10 +101,11 @@ public void testInitBareRepository() throws IOException,
InitCommand command = new InitCommand();
command.setDirectory(directory);
command.setBare(true);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
assertTrue(repository.isBare());
try (Git git = command.call()) {
Repository repository = git.getRepository();
assertNotNull(repository);
assertTrue(repository.isBare());
}
}
// non-bare repos where gitDir and directory is set. Same as
@ -117,11 +118,12 @@ public void testInitWithExplicitGitDir() throws IOException,
InitCommand command = new InitCommand();
command.setDirectory(wt);
command.setGitDir(gitDir);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
assertEqualsFile(wt, repository.getWorkTree());
assertEqualsFile(gitDir, repository.getDirectory());
try (Git git = command.call()) {
Repository repository = git.getRepository();
assertNotNull(repository);
assertEqualsFile(wt, repository.getWorkTree());
assertEqualsFile(gitDir, repository.getDirectory());
}
}
// non-bare repos where only gitDir is set. Same as
@ -135,12 +137,13 @@ public void testInitWithOnlyExplicitGitDir() throws IOException,
File gitDir = createTempDirectory("testInitRepository/.git");
InitCommand command = new InitCommand();
command.setGitDir(gitDir);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
assertEqualsFile(gitDir, repository.getDirectory());
assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getWorkTree());
try (Git git = command.call()) {
Repository repository = git.getRepository();
assertNotNull(repository);
assertEqualsFile(gitDir, repository.getDirectory());
assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getWorkTree());
}
}
// Bare repos where gitDir and directory is set will only work if gitDir and
@ -169,13 +172,14 @@ public void testInitWithDefaultsNonBare() throws JGitInternalException,
.getAbsolutePath());
InitCommand command = new InitCommand();
command.setBare(false);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
repository.getDirectory());
assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getWorkTree());
try (Git git = command.call()) {
Repository repository = git.getRepository();
assertNotNull(repository);
assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
repository.getDirectory());
assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getWorkTree());
}
}
// If neither directory nor gitDir is set in a bare repo make sure
@ -189,12 +193,13 @@ public void testInitWithDefaultsBare() throws JGitInternalException,
.getAbsolutePath());
InitCommand command = new InitCommand();
command.setBare(true);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getDirectory());
assertNull(repository.getWorkTree());
try (Git git = command.call()) {
Repository repository = git.getRepository();
assertNotNull(repository);
assertEqualsFile(new File(reader.getProperty("user.dir")),
repository.getDirectory());
assertNull(repository.getWorkTree());
}
}
// In a non-bare repo when directory and gitDir is set then they shouldn't

View File

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