Browse Source

RepoCommand: Don't leave Git open

When the command is run on a non-bare repository, an instance of
Git is created to execute the commit, and is left open when the
command has finished.

Refactor to not use a class scope Git instance, and make sure it
gets closed before returning.

Change-Id: Ic623ae0fd8b9e264b5dfd434da0de6bb4f910984
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-4.11
David Pursehouse 7 years ago
parent
commit
9bebb1eae7
  1. 86
      org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

86
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

@ -124,7 +124,6 @@ public class RepoCommand extends GitCommand<RevCommit> {
private boolean ignoreRemoteFailures = false;
private List<RepoProject> bareProjects;
private Git git;
private ProgressMonitor monitor;
/**
@ -486,7 +485,6 @@ public class RepoCommand extends GitCommand<RevCommit> {
/** {@inheritDoc} */
@Override
public RevCommit call() throws GitAPIException {
try {
checkCallable();
if (baseUri == null) {
baseUri = ""; //$NON-NLS-1$
@ -503,34 +501,16 @@ public class RepoCommand extends GitCommand<RevCommit> {
}
}
if (repo.isBare()) {
bareProjects = new ArrayList<>();
if (author == null)
author = new PersonIdent(repo);
if (callback == null)
callback = new DefaultRemoteReader();
} else
git = new Git(repo);
ManifestParser parser = new ManifestParser(
includedReader, manifestPath, branch, baseUri, groupsParam, repo);
List<RepoProject> filteredProjects;
try {
ManifestParser parser = new ManifestParser(includedReader,
manifestPath, branch, baseUri, groupsParam, repo);
parser.read(inputStream);
for (RepoProject proj : parser.getFilteredProjects()) {
addSubmodule(proj.getUrl(),
proj.getPath(),
proj.getRevision(),
proj.getCopyFiles(),
proj.getLinkFiles(),
proj.getGroups(),
proj.getRecommendShallow());
}
} catch (GitAPIException | IOException e) {
filteredProjects = parser.getFilteredProjects();
} catch (IOException e) {
throw new ManifestErrorException(e);
}
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
// Just ignore it, it's not important.
@ -538,6 +518,17 @@ public class RepoCommand extends GitCommand<RevCommit> {
}
if (repo.isBare()) {
bareProjects = new ArrayList<>();
if (author == null)
author = new PersonIdent(repo);
if (callback == null)
callback = new DefaultRemoteReader();
for (RepoProject proj : filteredProjects) {
addSubmoduleBare(proj.getUrl(), proj.getPath(),
proj.getRevision(), proj.getCopyFiles(),
proj.getLinkFiles(), proj.getGroups(),
proj.getRecommendShallow());
}
DirCache index = DirCache.newInCore();
DirCacheBuilder builder = index.builder();
ObjectInserter inserter = repo.newObjectInserter();
@ -689,44 +680,42 @@ public class RepoCommand extends GitCommand<RevCommit> {
}
return rw.parseCommit(commitId);
} catch (IOException e) {
} catch (GitAPIException | IOException e) {
throw new ManifestErrorException(e);
}
} else {
return git
.commit()
.setMessage(RepoText.get().repoCommitMessage)
try (Git git = new Git(repo)) {
for (RepoProject proj : filteredProjects) {
addSubmodule(proj.getUrl(), proj.getPath(),
proj.getRevision(), proj.getCopyFiles(),
proj.getLinkFiles(), git);
}
return git.commit().setMessage(RepoText.get().repoCommitMessage)
.call();
} catch (GitAPIException | IOException e) {
throw new ManifestErrorException(e);
}
}
}
private void addSubmodule(String url, String path, String revision,
List<CopyFile> copyfiles, List<LinkFile> linkfiles,
Set<String> groups, String recommendShallow)
List<CopyFile> copyfiles, List<LinkFile> linkfiles, Git git)
throws GitAPIException, IOException {
if (repo.isBare()) {
RepoProject proj = new RepoProject(url, path, revision, null, groups, recommendShallow);
proj.addCopyFiles(copyfiles);
proj.addLinkFiles(linkfiles);
bareProjects.add(proj);
} else {
assert (!repo.isBare());
assert (git != null);
if (!linkfiles.isEmpty()) {
throw new UnsupportedOperationException(
JGitText.get().nonBareLinkFilesNotSupported);
}
SubmoduleAddCommand add = git
.submoduleAdd()
.setPath(path)
.setURI(url);
SubmoduleAddCommand add = git.submoduleAdd().setPath(path).setURI(url);
if (monitor != null)
add.setProgressMonitor(monitor);
Repository subRepo = add.call();
if (revision != null) {
try (Git sub = new Git(subRepo)) {
sub.checkout().setName(findRef(revision, subRepo))
.call();
sub.checkout().setName(findRef(revision, subRepo)).call();
}
subRepo.close();
git.add().addFilepattern(path).call();
@ -736,6 +725,17 @@ public class RepoCommand extends GitCommand<RevCommit> {
git.add().addFilepattern(copyfile.dest).call();
}
}
private void addSubmoduleBare(String url, String path, String revision,
List<CopyFile> copyfiles, List<LinkFile> linkfiles,
Set<String> groups, String recommendShallow) {
assert (repo.isBare());
assert (bareProjects != null);
RepoProject proj = new RepoProject(url, path, revision, null, groups,
recommendShallow);
proj.addCopyFiles(copyfiles);
proj.addLinkFiles(linkfiles);
bareProjects.add(proj);
}
/*

Loading…
Cancel
Save