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>
This commit is contained in:
parent
c745c93e40
commit
80113c7bb6
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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)
|
||||
if (subRepo != null) {
|
||||
try {
|
||||
cloneSubmodules(subRepo);
|
||||
} finally {
|
||||
subRepo.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -130,10 +130,13 @@ public Map<String, String> call() throws GitAPIException {
|
|||
if (subRepo == null)
|
||||
continue;
|
||||
|
||||
StoredConfig subConfig = subRepo.getConfig();
|
||||
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
|
||||
String branch = getHeadBranch(subRepo);
|
||||
branch = getHeadBranch(subRepo);
|
||||
String remote = null;
|
||||
if (branch != null)
|
||||
remote = subConfig.getString(
|
||||
|
@ -145,6 +148,9 @@ public Map<String, String> call() throws GitAPIException {
|
|||
subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
|
||||
remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl);
|
||||
subConfig.save();
|
||||
} finally {
|
||||
subRepo.close();
|
||||
}
|
||||
}
|
||||
if (!synced.isEmpty())
|
||||
config.save();
|
||||
|
|
|
@ -164,8 +164,10 @@ public Collection<String> call() throws InvalidConfigurationException,
|
|||
submoduleRepo = clone.call().getRepository();
|
||||
}
|
||||
|
||||
try {
|
||||
RevWalk walk = new RevWalk(submoduleRepo);
|
||||
RevCommit commit = walk.parseCommit(generator.getObjectId());
|
||||
RevCommit commit = walk
|
||||
.parseCommit(generator.getObjectId());
|
||||
|
||||
String update = generator.getConfigUpdate();
|
||||
if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
|
||||
|
@ -177,10 +179,11 @@ public Collection<String> call() throws InvalidConfigurationException,
|
|||
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());
|
||||
// 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(
|
||||
|
@ -188,6 +191,9 @@ public Collection<String> call() throws InvalidConfigurationException,
|
|||
refUpdate.setNewObjectId(commit);
|
||||
refUpdate.forceUpdate();
|
||||
}
|
||||
} finally {
|
||||
submoduleRepo.close();
|
||||
}
|
||||
updated.add(generator.getPath());
|
||||
}
|
||||
return updated;
|
||||
|
|
|
@ -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;
|
||||
try {
|
||||
Ref head = subRepo.getRef(Constants.HEAD);
|
||||
return head != null ? head.getLeaf().getName() : null;
|
||||
} finally {
|
||||
subRepo.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue