Browse Source

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>
stable-2.1
Christian Halstrick 13 years ago committed by Matthias Sohn
parent
commit
80113c7bb6
  1. 2
      org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java
  2. 14
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
  3. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
  4. 29
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java
  5. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java
  6. 1
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java
  7. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java
  8. 9
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java
  9. 9
      org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java
  10. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java
  11. 34
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java
  12. 50
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java
  13. 16
      org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

2
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(); CloneCommand clone = Git.cloneRepository();
try { try {
clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare); clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare);
clone.call(); clone.call().getRepository().close();
} catch (Exception e) { } catch (Exception e) {
log("Could not clone repository: " + e, e, Project.MSG_ERR); log("Could not clone repository: " + e, e, Project.MSG_ERR);
throw new BuildException("Could not clone repository: " + e.getMessage(), e); throw new BuildException("Could not clone repository: " + e.getMessage(), e);

14
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java

@ -281,6 +281,7 @@ public class CloneCommandTest extends RepositoryTestCase {
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo);
git.add().addFilepattern(path) git.add().addFilepattern(path)
.addFilepattern(Constants.DOT_GIT_MODULES).call(); .addFilepattern(Constants.DOT_GIT_MODULES).call();
git.commit().setMessage("adding submodule").call(); git.commit().setMessage("adding submodule").call();
@ -342,15 +343,19 @@ public class CloneCommandTest extends RepositoryTestCase {
assertNotNull(sub2Head); assertNotNull(sub2Head);
// Add submodule 2 to submodule 1 // Add submodule 2 to submodule 1
assertNotNull(sub1Git.submoduleAdd().setPath(path) Repository r = sub1Git.submoduleAdd().setPath(path)
.setURI(sub2.getDirectory().toURI().toString()).call()); .setURI(sub2.getDirectory().toURI().toString()).call();
assertNotNull(r);
addRepoToClose(r);
RevCommit sub1Head = sub1Git.commit().setAll(true) RevCommit sub1Head = sub1Git.commit().setAll(true)
.setMessage("Adding submodule").call(); .setMessage("Adding submodule").call();
assertNotNull(sub1Head); assertNotNull(sub1Head);
// Add submodule 1 to default repository // Add submodule 1 to default repository
assertNotNull(git.submoduleAdd().setPath(path) r = git.submoduleAdd().setPath(path)
.setURI(sub1.getDirectory().toURI().toString()).call()); .setURI(sub1.getDirectory().toURI().toString()).call();
assertNotNull(r);
addRepoToClose(r);
assertNotNull(git.commit().setAll(true).setMessage("Adding submodule") assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
.call()); .call());
@ -383,6 +388,7 @@ public class CloneCommandTest extends RepositoryTestCase {
SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository()); SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
assertTrue(walk.next()); assertTrue(walk.next());
Repository clonedSub1 = walk.getRepository(); Repository clonedSub1 = walk.getRepository();
addRepoToClose(clonedSub1);
assertNotNull(clonedSub1); assertNotNull(clonedSub1);
status = new SubmoduleStatusCommand(clonedSub1); status = new SubmoduleStatusCommand(clonedSub1);
statuses = status.call(); statuses = status.call();

10
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

@ -179,6 +179,7 @@ public class CommitCommandTest extends RepositoryTestCase {
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
@ -187,7 +188,9 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(uri, generator.getModulesUrl()); assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath()); assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl()); assertEquals(uri, generator.getConfigUrl());
assertNotNull(generator.getRepository()); Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(commit, repo.resolve(Constants.HEAD)); assertEquals(commit, repo.resolve(Constants.HEAD));
RevCommit submoduleCommit = git.commit().setMessage("submodule add") RevCommit submoduleCommit = git.commit().setMessage("submodule add")
@ -224,6 +227,7 @@ public class CommitCommandTest extends RepositoryTestCase {
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
@ -232,7 +236,9 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(uri, generator.getModulesUrl()); assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath()); assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl()); assertEquals(uri, generator.getConfigUrl());
assertNotNull(generator.getRepository()); Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(commit2, repo.resolve(Constants.HEAD)); assertEquals(commit2, repo.resolve(Constants.HEAD));
RevCommit submoduleAddCommit = git.commit().setMessage("submodule add") RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")

29
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java

@ -78,7 +78,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test @Test
public void commandWithNullPath() throws GitAPIException { public void commandWithNullPath() throws GitAPIException {
try { try {
new SubmoduleAddCommand(db).setURI("uri").call(); new SubmoduleAddCommand(db).setURI("uri").call().close();
fail("Exception not thrown"); fail("Exception not thrown");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertEquals(JGitText.get().pathNotConfigured, e.getMessage()); assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
@ -88,7 +88,8 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test @Test
public void commandWithEmptyPath() throws GitAPIException { public void commandWithEmptyPath() throws GitAPIException {
try { try {
new SubmoduleAddCommand(db).setPath("").setURI("uri").call(); new SubmoduleAddCommand(db).setPath("").setURI("uri").call()
.close();
fail("Exception not thrown"); fail("Exception not thrown");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertEquals(JGitText.get().pathNotConfigured, e.getMessage()); assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
@ -98,7 +99,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test @Test
public void commandWithNullUri() throws GitAPIException { public void commandWithNullUri() throws GitAPIException {
try { try {
new SubmoduleAddCommand(db).setPath("sub").call(); new SubmoduleAddCommand(db).setPath("sub").call().close();
fail("Exception not thrown"); fail("Exception not thrown");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertEquals(JGitText.get().uriNotConfigured, e.getMessage()); assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
@ -108,7 +109,8 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test @Test
public void commandWithEmptyUri() throws GitAPIException { public void commandWithEmptyUri() throws GitAPIException {
try { try {
new SubmoduleAddCommand(db).setPath("sub").setURI("").call(); new SubmoduleAddCommand(db).setPath("sub").setURI("").call()
.close();
fail("Exception not thrown"); fail("Exception not thrown");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertEquals(JGitText.get().uriNotConfigured, e.getMessage()); assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
@ -129,6 +131,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
@ -137,7 +140,9 @@ public class SubmoduleAddTest extends RepositoryTestCase {
assertEquals(uri, generator.getModulesUrl()); assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath()); assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl()); assertEquals(uri, generator.getConfigUrl());
assertNotNull(generator.getRepository()); Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(commit, repo.resolve(Constants.HEAD)); assertEquals(commit, repo.resolve(Constants.HEAD));
Status status = Git.wrap(db).status().call(); Status status = Git.wrap(db).status().call();
@ -165,7 +170,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setPath(path); command.setPath(path);
command.setURI("git://server/repo.git"); command.setURI("git://server/repo.git");
try { try {
command.call(); command.call().close();
fail("Exception not thrown"); fail("Exception not thrown");
} catch (JGitInternalException e) { } catch (JGitInternalException e) {
assertEquals( assertEquals(
@ -188,6 +193,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
@ -199,11 +205,12 @@ public class SubmoduleAddTest extends RepositoryTestCase {
if (File.separatorChar == '\\') if (File.separatorChar == '\\')
fullUri = fullUri.replace('\\', '/'); fullUri = fullUri.replace('\\', '/');
assertEquals(fullUri, generator.getConfigUrl()); assertEquals(fullUri, generator.getConfigUrl());
assertNotNull(generator.getRepository()); Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals( assertEquals(
fullUri, fullUri,
generator subModRepo
.getRepository()
.getConfig() .getConfig()
.getString(ConfigConstants.CONFIG_REMOTE_SECTION, .getString(ConfigConstants.CONFIG_REMOTE_SECTION,
Constants.DEFAULT_REMOTE_NAME, Constants.DEFAULT_REMOTE_NAME,
@ -238,7 +245,9 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setPath(path2); command.setPath(path2);
String url2 = db.getDirectory().toURI().toString(); String url2 = db.getDirectory().toURI().toString();
command.setURI(url2); command.setURI(url2);
assertNotNull(command.call()); Repository r = command.call();
assertNotNull(r);
addRepoToClose(r);
modulesConfig.load(); modulesConfig.load();
assertEquals(path1, modulesConfig.getString( assertEquals(path1, modulesConfig.getString(

10
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()) .setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call() .setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository(); .getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo); assertNotNull(subRepo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
@ -133,7 +134,9 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
generator = SubmoduleWalk.forIndex(db); generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals(url, generator.getConfigUrl()); assertEquals(url, generator.getConfigUrl());
StoredConfig submoduleConfig = generator.getRepository().getConfig(); Repository subModRepository = generator.getRepository();
addRepoToClose(subModRepository);
StoredConfig submoduleConfig = subModRepository.getConfig();
assertEquals(url, submoduleConfig.getString( assertEquals(url, submoduleConfig.getString(
ConfigConstants.CONFIG_REMOTE_SECTION, ConfigConstants.CONFIG_REMOTE_SECTION,
Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL)); Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
@ -181,6 +184,7 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
.setDirectory(new File(db.getWorkTree(), path)).call() .setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository(); .getRepository();
assertNotNull(subRepo); assertNotNull(subRepo);
addRepoToClose(subRepo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
@ -202,7 +206,9 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
generator = SubmoduleWalk.forIndex(db); generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals("git://server/sub.git", generator.getConfigUrl()); 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( assertEquals("git://server/sub.git", submoduleConfig.getString(
ConfigConstants.CONFIG_REMOTE_SECTION, ConfigConstants.CONFIG_REMOTE_SECTION,
Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL)); Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));

1
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); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
Repository subRepo = generator.getRepository(); Repository subRepo = generator.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo); assertNotNull(subRepo);
assertEquals(commit, subRepo.resolve(Constants.HEAD)); assertEquals(commit, subRepo.resolve(Constants.HEAD));
} }

2
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.getModulesUpdate());
assertNull(gen.getModulesUrl()); assertNull(gen.getModulesUrl());
Repository subRepo = gen.getRepository(); Repository subRepo = gen.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo); assertNotNull(subRepo);
assertEquals(modulesGitDir, subRepo.getDirectory()); assertEquals(modulesGitDir, subRepo.getDirectory());
assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree()); assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree());
@ -217,6 +218,7 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
assertNull(gen.getModulesUpdate()); assertNull(gen.getModulesUpdate());
assertNull(gen.getModulesUrl()); assertNull(gen.getModulesUrl());
Repository subRepo = gen.getRepository(); Repository subRepo = gen.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo); assertNotNull(subRepo);
assertEquals(modulesGitDir, subRepo.getDirectory()); assertEquals(modulesGitDir, subRepo.getDirectory());
assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree()); assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree());

9
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

@ -256,7 +256,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
editor.commit(); editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString()) 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); TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
@ -355,7 +356,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
editor.commit(); editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString()) 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); TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
@ -388,7 +390,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
editor.commit(); editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString()) 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); TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());

9
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); SubmoduleWalk walk = SubmoduleWalk.forIndex(clonedRepo);
while (walk.next()) { while (walk.next()) {
Repository subRepo = walk.getRepository(); Repository subRepo = walk.getRepository();
if (subRepo != null) if (subRepo != null) {
cloneSubmodules(subRepo); try {
cloneSubmodules(subRepo);
} finally {
subRepo.close();
}
}
} }
} }
} }

7
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java

@ -130,7 +130,12 @@ public class SubmoduleStatusCommand extends
return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path, return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path,
id); 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 // Report uninitialized if no HEAD commit in submodule repository
if (headId == null) if (headId == null)

34
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) if (subRepo == null)
continue; continue;
StoredConfig subConfig = subRepo.getConfig(); StoredConfig subConfig;
// Get name of remote associated with current branch and String branch;
// fall back to default remote name as last resort try {
String branch = getHeadBranch(subRepo); subConfig = subRepo.getConfig();
String remote = null; // Get name of remote associated with current branch and
if (branch != null) // fall back to default remote name as last resort
remote = subConfig.getString( branch = getHeadBranch(subRepo);
ConfigConstants.CONFIG_BRANCH_SECTION, branch, String remote = null;
ConfigConstants.CONFIG_KEY_REMOTE); if (branch != null)
if (remote == null) remote = subConfig.getString(
remote = Constants.DEFAULT_REMOTE_NAME; ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REMOTE);
if (remote == null)
remote = Constants.DEFAULT_REMOTE_NAME;
subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION, subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl); remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl);
subConfig.save(); subConfig.save();
} finally {
subRepo.close();
}
} }
if (!synced.isEmpty()) if (!synced.isEmpty())
config.save(); config.save();

50
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java

@ -164,29 +164,35 @@ public class SubmoduleUpdateCommand extends
submoduleRepo = clone.call().getRepository(); submoduleRepo = clone.call().getRepository();
} }
RevWalk walk = new RevWalk(submoduleRepo); try {
RevCommit commit = walk.parseCommit(generator.getObjectId()); RevWalk walk = new RevWalk(submoduleRepo);
RevCommit commit = walk
.parseCommit(generator.getObjectId());
String update = generator.getConfigUpdate(); String update = generator.getConfigUpdate();
if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) { if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
MergeCommand merge = new MergeCommand(submoduleRepo); MergeCommand merge = new MergeCommand(submoduleRepo);
merge.include(commit); merge.include(commit);
merge.call(); merge.call();
} else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) { } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) {
RebaseCommand rebase = new RebaseCommand(submoduleRepo); RebaseCommand rebase = new RebaseCommand(submoduleRepo);
rebase.setUpstream(commit); rebase.setUpstream(commit);
rebase.call(); rebase.call();
} else { } else {
// Checkout commit referenced in parent repository's index // Checkout commit referenced in parent repository's
// as a detached HEAD // index as a detached HEAD
DirCacheCheckout co = new DirCacheCheckout(submoduleRepo, DirCacheCheckout co = new DirCacheCheckout(
submoduleRepo.lockDirCache(), commit.getTree()); submoduleRepo, submoduleRepo.lockDirCache(),
co.setFailOnConflict(true); commit.getTree());
co.checkout(); co.setFailOnConflict(true);
RefUpdate refUpdate = submoduleRepo.updateRef( co.checkout();
Constants.HEAD, true); RefUpdate refUpdate = submoduleRepo.updateRef(
refUpdate.setNewObjectId(commit); Constants.HEAD, true);
refUpdate.forceUpdate(); refUpdate.setNewObjectId(commit);
refUpdate.forceUpdate();
}
} finally {
submoduleRepo.close();
} }
updated.add(generator.getPath()); updated.add(generator.getPath());
} }

16
org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

@ -627,7 +627,13 @@ public class SubmoduleWalk {
*/ */
public ObjectId getHead() throws IOException { public ObjectId getHead() throws IOException {
Repository subRepo = getRepository(); 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(); Repository subRepo = getRepository();
if (subRepo == null) if (subRepo == null)
return null; return null;
Ref head = subRepo.getRef(Constants.HEAD); try {
return head != null ? head.getLeaf().getName() : null; Ref head = subRepo.getRef(Constants.HEAD);
return head != null ? head.getLeaf().getName() : null;
} finally {
subRepo.close();
}
} }
/** /**

Loading…
Cancel
Save