diff --git a/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java b/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java index 8d12ce3ad..f23f3b753 100644 --- a/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java +++ b/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java @@ -109,7 +109,7 @@ public void execute() throws BuildException { CloneCommand clone = Git.cloneRepository(); try { clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare); - clone.call(); + clone.call().getRepository().close(); } catch (Exception e) { log("Could not clone repository: " + e, e, Project.MSG_ERR); throw new BuildException("Could not clone repository: " + e.getMessage(), e); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index 4441ea930..7370091d5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -281,6 +281,7 @@ public void testCloneRepositoryWithSubmodules() throws Exception { command.setURI(uri); Repository repo = command.call(); assertNotNull(repo); + addRepoToClose(repo); git.add().addFilepattern(path) .addFilepattern(Constants.DOT_GIT_MODULES).call(); git.commit().setMessage("adding submodule").call(); @@ -342,15 +343,19 @@ public void testCloneRepositoryWithNestedSubmodules() throws Exception { assertNotNull(sub2Head); // Add submodule 2 to submodule 1 - assertNotNull(sub1Git.submoduleAdd().setPath(path) - .setURI(sub2.getDirectory().toURI().toString()).call()); + Repository r = sub1Git.submoduleAdd().setPath(path) + .setURI(sub2.getDirectory().toURI().toString()).call(); + assertNotNull(r); + addRepoToClose(r); RevCommit sub1Head = sub1Git.commit().setAll(true) .setMessage("Adding submodule").call(); assertNotNull(sub1Head); // Add submodule 1 to default repository - assertNotNull(git.submoduleAdd().setPath(path) - .setURI(sub1.getDirectory().toURI().toString()).call()); + r = git.submoduleAdd().setPath(path) + .setURI(sub1.getDirectory().toURI().toString()).call(); + assertNotNull(r); + addRepoToClose(r); assertNotNull(git.commit().setAll(true).setMessage("Adding submodule") .call()); @@ -383,6 +388,7 @@ public void testCloneRepositoryWithNestedSubmodules() throws Exception { SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository()); assertTrue(walk.next()); Repository clonedSub1 = walk.getRepository(); + addRepoToClose(clonedSub1); assertNotNull(clonedSub1); status = new SubmoduleStatusCommand(clonedSub1); statuses = status.call(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java index 9b597d32d..e558d6178 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java @@ -179,6 +179,7 @@ public void commitNewSubmodule() throws Exception { command.setURI(uri); Repository repo = command.call(); assertNotNull(repo); + addRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); @@ -187,7 +188,9 @@ public void commitNewSubmodule() throws Exception { assertEquals(uri, generator.getModulesUrl()); assertEquals(path, generator.getModulesPath()); assertEquals(uri, generator.getConfigUrl()); - assertNotNull(generator.getRepository()); + Repository subModRepo = generator.getRepository(); + addRepoToClose(subModRepo); + assertNotNull(subModRepo); assertEquals(commit, repo.resolve(Constants.HEAD)); RevCommit submoduleCommit = git.commit().setMessage("submodule add") @@ -224,6 +227,7 @@ public void commitSubmoduleUpdate() throws Exception { command.setURI(uri); Repository repo = command.call(); assertNotNull(repo); + addRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); @@ -232,7 +236,9 @@ public void commitSubmoduleUpdate() throws Exception { assertEquals(uri, generator.getModulesUrl()); assertEquals(path, generator.getModulesPath()); assertEquals(uri, generator.getConfigUrl()); - assertNotNull(generator.getRepository()); + Repository subModRepo = generator.getRepository(); + addRepoToClose(subModRepo); + assertNotNull(subModRepo); assertEquals(commit2, repo.resolve(Constants.HEAD)); RevCommit submoduleAddCommit = git.commit().setMessage("submodule add") diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java index 940a78ac4..211709f49 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java @@ -78,7 +78,7 @@ public class SubmoduleAddTest extends RepositoryTestCase { @Test public void commandWithNullPath() throws GitAPIException { try { - new SubmoduleAddCommand(db).setURI("uri").call(); + new SubmoduleAddCommand(db).setURI("uri").call().close(); fail("Exception not thrown"); } catch (IllegalArgumentException e) { assertEquals(JGitText.get().pathNotConfigured, e.getMessage()); @@ -88,7 +88,8 @@ public void commandWithNullPath() throws GitAPIException { @Test public void commandWithEmptyPath() throws GitAPIException { try { - new SubmoduleAddCommand(db).setPath("").setURI("uri").call(); + new SubmoduleAddCommand(db).setPath("").setURI("uri").call() + .close(); fail("Exception not thrown"); } catch (IllegalArgumentException e) { assertEquals(JGitText.get().pathNotConfigured, e.getMessage()); @@ -98,7 +99,7 @@ public void commandWithEmptyPath() throws GitAPIException { @Test public void commandWithNullUri() throws GitAPIException { try { - new SubmoduleAddCommand(db).setPath("sub").call(); + new SubmoduleAddCommand(db).setPath("sub").call().close(); fail("Exception not thrown"); } catch (IllegalArgumentException e) { assertEquals(JGitText.get().uriNotConfigured, e.getMessage()); @@ -108,7 +109,8 @@ public void commandWithNullUri() throws GitAPIException { @Test public void commandWithEmptyUri() throws GitAPIException { try { - new SubmoduleAddCommand(db).setPath("sub").setURI("").call(); + new SubmoduleAddCommand(db).setPath("sub").setURI("").call() + .close(); fail("Exception not thrown"); } catch (IllegalArgumentException e) { assertEquals(JGitText.get().uriNotConfigured, e.getMessage()); @@ -129,6 +131,7 @@ public void addSubmodule() throws Exception { command.setURI(uri); Repository repo = command.call(); assertNotNull(repo); + addRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); @@ -137,7 +140,9 @@ public void addSubmodule() throws Exception { assertEquals(uri, generator.getModulesUrl()); assertEquals(path, generator.getModulesPath()); assertEquals(uri, generator.getConfigUrl()); - assertNotNull(generator.getRepository()); + Repository subModRepo = generator.getRepository(); + addRepoToClose(subModRepo); + assertNotNull(subModRepo); assertEquals(commit, repo.resolve(Constants.HEAD)); Status status = Git.wrap(db).status().call(); @@ -165,7 +170,7 @@ public void apply(DirCacheEntry ent) { command.setPath(path); command.setURI("git://server/repo.git"); try { - command.call(); + command.call().close(); fail("Exception not thrown"); } catch (JGitInternalException e) { assertEquals( @@ -188,6 +193,7 @@ public void addSubmoduleWithRelativeUri() throws Exception { command.setURI(uri); Repository repo = command.call(); assertNotNull(repo); + addRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); @@ -199,11 +205,12 @@ public void addSubmoduleWithRelativeUri() throws Exception { if (File.separatorChar == '\\') fullUri = fullUri.replace('\\', '/'); assertEquals(fullUri, generator.getConfigUrl()); - assertNotNull(generator.getRepository()); + Repository subModRepo = generator.getRepository(); + addRepoToClose(subModRepo); + assertNotNull(subModRepo); assertEquals( fullUri, - generator - .getRepository() + subModRepo .getConfig() .getString(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, @@ -238,7 +245,9 @@ public void addSubmoduleWithExistingSubmoduleDefined() throws Exception { command.setPath(path2); String url2 = db.getDirectory().toURI().toString(); command.setURI(url2); - assertNotNull(command.call()); + Repository r = command.call(); + assertNotNull(r); + addRepoToClose(r); modulesConfig.load(); assertEquals(path1, modulesConfig.getString( diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java index 3f9ad11f1..9191edef8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java @@ -115,6 +115,7 @@ public void apply(DirCacheEntry ent) { .setURI(db.getDirectory().toURI().toString()) .setDirectory(new File(db.getWorkTree(), path)).call() .getRepository(); + addRepoToClose(subRepo); assertNotNull(subRepo); SubmoduleWalk generator = SubmoduleWalk.forIndex(db); @@ -133,7 +134,9 @@ public void apply(DirCacheEntry ent) { generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); assertEquals(url, generator.getConfigUrl()); - StoredConfig submoduleConfig = generator.getRepository().getConfig(); + Repository subModRepository = generator.getRepository(); + addRepoToClose(subModRepository); + StoredConfig submoduleConfig = subModRepository.getConfig(); assertEquals(url, submoduleConfig.getString( ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL)); @@ -181,6 +184,7 @@ public void apply(DirCacheEntry ent) { .setDirectory(new File(db.getWorkTree(), path)).call() .getRepository(); assertNotNull(subRepo); + addRepoToClose(subRepo); SubmoduleWalk generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); @@ -202,7 +206,9 @@ public void apply(DirCacheEntry ent) { generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); assertEquals("git://server/sub.git", generator.getConfigUrl()); - StoredConfig submoduleConfig = generator.getRepository().getConfig(); + Repository subModRepository1 = generator.getRepository(); + addRepoToClose(subModRepository1); + StoredConfig submoduleConfig = subModRepository1.getConfig(); assertEquals("git://server/sub.git", submoduleConfig.getString( ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL)); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java index eb0cf2b0b..306236325 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java @@ -121,6 +121,7 @@ public void apply(DirCacheEntry ent) { SubmoduleWalk generator = SubmoduleWalk.forIndex(db); assertTrue(generator.next()); Repository subRepo = generator.getRepository(); + addRepoToClose(subRepo); assertNotNull(subRepo); assertEquals(commit, subRepo.resolve(Constants.HEAD)); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java index fdb67d266..0669dd199 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java @@ -169,6 +169,7 @@ public void apply(DirCacheEntry ent) { assertNull(gen.getModulesUpdate()); assertNull(gen.getModulesUrl()); Repository subRepo = gen.getRepository(); + addRepoToClose(subRepo); assertNotNull(subRepo); assertEquals(modulesGitDir, subRepo.getDirectory()); assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree()); @@ -217,6 +218,7 @@ public void apply(DirCacheEntry ent) { assertNull(gen.getModulesUpdate()); assertNull(gen.getModulesUrl()); Repository subRepo = gen.getRepository(); + addRepoToClose(subRepo); assertNotNull(subRepo); assertEquals(modulesGitDir, subRepo.getDirectory()); assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java index b335f2d43..2cdaea28d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java @@ -256,7 +256,8 @@ public void apply(DirCacheEntry ent) { editor.commit(); Git.cloneRepository().setURI(db.getDirectory().toURI().toString()) - .setDirectory(new File(db.getWorkTree(), path)).call(); + .setDirectory(new File(db.getWorkTree(), path)).call() + .getRepository().close(); TreeWalk walk = new TreeWalk(db); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); @@ -355,7 +356,8 @@ public void apply(DirCacheEntry ent) { editor.commit(); Git.cloneRepository().setURI(db.getDirectory().toURI().toString()) - .setDirectory(new File(db.getWorkTree(), path)).call(); + .setDirectory(new File(db.getWorkTree(), path)).call() + .getRepository().close(); TreeWalk walk = new TreeWalk(db); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); @@ -388,7 +390,8 @@ public void apply(DirCacheEntry ent) { editor.commit(); Git.cloneRepository().setURI(db.getDirectory().toURI().toString()) - .setDirectory(new File(db.getWorkTree(), path)).call(); + .setDirectory(new File(db.getWorkTree(), path)).call() + .getRepository().close(); TreeWalk walk = new TreeWalk(db); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index 067e92a96..e4c55698e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -245,8 +245,13 @@ private void cloneSubmodules(Repository clonedRepo) throws IOException, SubmoduleWalk walk = SubmoduleWalk.forIndex(clonedRepo); while (walk.next()) { Repository subRepo = walk.getRepository(); - if (subRepo != null) - cloneSubmodules(subRepo); + if (subRepo != null) { + try { + cloneSubmodules(subRepo); + } finally { + subRepo.close(); + } + } } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java index d27f90c12..bbc01adba 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java @@ -130,7 +130,12 @@ private SubmoduleStatus getStatus(SubmoduleWalk generator) return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path, id); - ObjectId headId = subRepo.resolve(Constants.HEAD); + ObjectId headId; + try { + headId = subRepo.resolve(Constants.HEAD); + } finally { + subRepo.close(); + } // Report uninitialized if no HEAD commit in submodule repository if (headId == null) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java index edc54ff4c..11d3c5acc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java @@ -130,21 +130,27 @@ public Map call() throws GitAPIException { if (subRepo == null) continue; - StoredConfig subConfig = subRepo.getConfig(); - // Get name of remote associated with current branch and - // fall back to default remote name as last resort - String branch = getHeadBranch(subRepo); - String remote = null; - if (branch != null) - remote = subConfig.getString( - ConfigConstants.CONFIG_BRANCH_SECTION, branch, - ConfigConstants.CONFIG_KEY_REMOTE); - if (remote == null) - remote = Constants.DEFAULT_REMOTE_NAME; + StoredConfig subConfig; + String branch; + try { + subConfig = subRepo.getConfig(); + // Get name of remote associated with current branch and + // fall back to default remote name as last resort + branch = getHeadBranch(subRepo); + String remote = null; + if (branch != null) + remote = subConfig.getString( + ConfigConstants.CONFIG_BRANCH_SECTION, branch, + ConfigConstants.CONFIG_KEY_REMOTE); + if (remote == null) + remote = Constants.DEFAULT_REMOTE_NAME; - subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION, - remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl); - subConfig.save(); + subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION, + remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl); + subConfig.save(); + } finally { + subRepo.close(); + } } if (!synced.isEmpty()) config.save(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java index caf2cedc4..40f6a9f9a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java @@ -164,29 +164,35 @@ public Collection call() throws InvalidConfigurationException, submoduleRepo = clone.call().getRepository(); } - RevWalk walk = new RevWalk(submoduleRepo); - RevCommit commit = walk.parseCommit(generator.getObjectId()); + try { + RevWalk walk = new RevWalk(submoduleRepo); + RevCommit commit = walk + .parseCommit(generator.getObjectId()); - String update = generator.getConfigUpdate(); - if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) { - MergeCommand merge = new MergeCommand(submoduleRepo); - merge.include(commit); - merge.call(); - } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) { - RebaseCommand rebase = new RebaseCommand(submoduleRepo); - rebase.setUpstream(commit); - rebase.call(); - } else { - // Checkout commit referenced in parent repository's index - // as a detached HEAD - DirCacheCheckout co = new DirCacheCheckout(submoduleRepo, - submoduleRepo.lockDirCache(), commit.getTree()); - co.setFailOnConflict(true); - co.checkout(); - RefUpdate refUpdate = submoduleRepo.updateRef( - Constants.HEAD, true); - refUpdate.setNewObjectId(commit); - refUpdate.forceUpdate(); + String update = generator.getConfigUpdate(); + if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) { + MergeCommand merge = new MergeCommand(submoduleRepo); + merge.include(commit); + merge.call(); + } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) { + RebaseCommand rebase = new RebaseCommand(submoduleRepo); + rebase.setUpstream(commit); + rebase.call(); + } else { + // Checkout commit referenced in parent repository's + // index as a detached HEAD + DirCacheCheckout co = new DirCacheCheckout( + submoduleRepo, submoduleRepo.lockDirCache(), + commit.getTree()); + co.setFailOnConflict(true); + co.checkout(); + RefUpdate refUpdate = submoduleRepo.updateRef( + Constants.HEAD, true); + refUpdate.setNewObjectId(commit); + refUpdate.forceUpdate(); + } + } finally { + submoduleRepo.close(); } updated.add(generator.getPath()); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java index 040ea2687..323965f7c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java @@ -627,7 +627,13 @@ public Repository getRepository() throws IOException { */ public ObjectId getHead() throws IOException { Repository subRepo = getRepository(); - return subRepo != null ? subRepo.resolve(Constants.HEAD) : null; + if (subRepo == null) + return null; + try { + return subRepo.resolve(Constants.HEAD); + } finally { + subRepo.close(); + } } /** @@ -640,8 +646,12 @@ public String getHeadRef() throws IOException { Repository subRepo = getRepository(); if (subRepo == null) return null; - Ref head = subRepo.getRef(Constants.HEAD); - return head != null ? head.getLeaf().getName() : null; + try { + Ref head = subRepo.getRef(Constants.HEAD); + return head != null ? head.getLeaf().getName() : null; + } finally { + subRepo.close(); + } } /**