Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Halstrick2012-06-15 22:19:51 +0000
committerMatthias Sohn2012-06-15 22:19:51 +0000
commit80113c7bb65c4c624250e430b5f2b19b1367d18c (patch)
treee3bc527bf93014d3d4f577c60bd6a7e559374012
parentc745c93e40abdad7afde4e3b48dbb11469916112 (diff)
downloadjgit-80113c7bb65c4c624250e430b5f2b19b1367d18c.tar.gz
jgit-80113c7bb65c4c624250e430b5f2b19b1367d18c.tar.xz
jgit-80113c7bb65c4c624250e430b5f2b19b1367d18c.zip
Fix resource leaks due to unclosed repositories
Whenever a call to JGit returns a Repository the caller should make sure to call close() on it if he doesn't need it anymore. Since instances of Repository contain e.g. open FileOutputStreams (for pack files) forgetting to close the repository can lead to resource leaks. This was the reason why dozens of the JUnit tests failed on Windows with "Can't delete file ...." errors. In LocalDiskRepositoryTestCase.tearDown() we tried to delete the repositories we used during tests which failed because we had open FileOutputStreams. Not only the obvious cases during Clone or Init operations returned Repositories, but also the new SubModule API created repository instances. In some places we even forgot to close submodule repositories in our internal coding. To see the effects of this fix run the JGit JUnit tests under Windows. On other platforms it's harder to see because either the leaking resources don't lead to failing JUnit tests (on Unix you can delete files with open FileOutputStreams) or the java gc runs differently and cleans up the resources earlier. Change-Id: I6d4f637b0d4af20ff4d501db091548696373a58a Signed-off-by: Christian Halstrick <christian.halstrick@sap.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java2
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java14
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java10
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java29
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java10
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java1
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java2
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java9
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java9
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java7
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java34
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java50
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java16
13 files changed, 129 insertions, 64 deletions
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 8d12ce3ad4..f23f3b753d 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 class GitCloneTask extends Task {
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 4441ea9301..7370091d54 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 class CloneCommandTest extends RepositoryTestCase {
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 class CloneCommandTest extends RepositoryTestCase {
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 class CloneCommandTest extends RepositoryTestCase {
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 9b597d32da..e558d61782 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 class CommitCommandTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
+ addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@@ -187,7 +188,9 @@ public class CommitCommandTest extends RepositoryTestCase {
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 class CommitCommandTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
+ addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@@ -232,7 +236,9 @@ public class CommitCommandTest extends RepositoryTestCase {
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 940a78ac4f..211709f499 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 class SubmoduleAddTest extends RepositoryTestCase {
@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 class SubmoduleAddTest extends RepositoryTestCase {
@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 class SubmoduleAddTest extends RepositoryTestCase {
@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 class SubmoduleAddTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
+ addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@@ -137,7 +140,9 @@ public class SubmoduleAddTest extends RepositoryTestCase {
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 class SubmoduleAddTest extends RepositoryTestCase {
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 class SubmoduleAddTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
+ addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@@ -199,11 +205,12 @@ public class SubmoduleAddTest extends RepositoryTestCase {
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 class SubmoduleAddTest extends RepositoryTestCase {
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 3f9ad11f1d..9191edef8b 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 class SubmoduleSyncTest extends RepositoryTestCase {
.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 class SubmoduleSyncTest extends RepositoryTestCase {
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 class SubmoduleSyncTest extends RepositoryTestCase {
.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 class SubmoduleSyncTest extends RepositoryTestCase {
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 eb0cf2b0b6..306236325a 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 class SubmoduleUpdateTest extends RepositoryTestCase {
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 fdb67d266c..0669dd1993 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 class SubmoduleWalkTest extends RepositoryTestCase {
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 class SubmoduleWalkTest extends RepositoryTestCase {
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 b335f2d43a..2cdaea28de 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 class FileTreeIteratorTest extends RepositoryTestCase {
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 class FileTreeIteratorTest extends RepositoryTestCase {
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 class FileTreeIteratorTest extends RepositoryTestCase {
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 067e92a960..e4c55698e0 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 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
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 d27f90c129..bbc01adbae 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 @@ public class SubmoduleStatusCommand extends
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 edc54ff4cc..11d3c5acca 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 class SubmoduleSyncCommand extends GitCommand<Map<String, String>> {
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 caf2cedc4e..40f6a9f9a4 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 class SubmoduleUpdateCommand extends
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 040ea26873..323965f7cc 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 class SubmoduleWalk {
*/
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 class SubmoduleWalk {
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();
+ }
}
/**

Back to the top