aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@gmail.com>2018-02-14 10:30:12 +0900
committerDavid Pursehouse <david.pursehouse@gmail.com>2018-02-15 16:59:42 +0900
commit9bebb1eae78401e1d3289dc3d84006c10d10c0ef (patch)
treec2d0e00dea67353c4c65dedf9ef841a8a5e4e077 /org.eclipse.jgit/src/org/eclipse/jgit/gitrepo
parent282dc592da152a224f58df6b21fd5f594a8778fb (diff)
downloadjgit-9bebb1eae78401e1d3289dc3d84006c10d10c0ef.tar.gz
jgit-9bebb1eae78401e1d3289dc3d84006c10d10c0ef.zip
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>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/gitrepo')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java158
1 files changed, 79 insertions, 79 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
index c7bc1b6aa3..10bd6005b5 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
+++ b/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,58 +485,50 @@ public class RepoCommand extends GitCommand<RevCommit> {
/** {@inheritDoc} */
@Override
public RevCommit call() throws GitAPIException {
- try {
- checkCallable();
- if (baseUri == null) {
- baseUri = ""; //$NON-NLS-1$
- }
- if (inputStream == null) {
- if (manifestPath == null || manifestPath.length() == 0)
- throw new IllegalArgumentException(
- JGitText.get().pathNotConfigured);
- try {
- inputStream = new FileInputStream(manifestPath);
- } catch (IOException e) {
- throw new IllegalArgumentException(
- JGitText.get().pathNotConfigured);
- }
- }
-
- 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);
+ checkCallable();
+ if (baseUri == null) {
+ baseUri = ""; //$NON-NLS-1$
+ }
+ if (inputStream == null) {
+ if (manifestPath == null || manifestPath.length() == 0)
+ throw new IllegalArgumentException(
+ JGitText.get().pathNotConfigured);
try {
- 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) {
- throw new ManifestErrorException(e);
+ inputStream = new FileInputStream(manifestPath);
+ } catch (IOException e) {
+ throw new IllegalArgumentException(
+ JGitText.get().pathNotConfigured);
}
+ }
+
+ List<RepoProject> filteredProjects;
+ try {
+ ManifestParser parser = new ManifestParser(includedReader,
+ manifestPath, branch, baseUri, groupsParam, repo);
+ parser.read(inputStream);
+ filteredProjects = parser.getFilteredProjects();
+ } catch (IOException e) {
+ throw new ManifestErrorException(e);
} finally {
try {
- if (inputStream != null)
- inputStream.close();
+ inputStream.close();
} catch (IOException e) {
// Just ignore it, it's not important.
}
}
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,53 +680,62 @@ 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)
- .call();
+ 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 {
- if (!linkfiles.isEmpty()) {
- throw new UnsupportedOperationException(
- JGitText.get().nonBareLinkFilesNotSupported);
- }
+ assert (!repo.isBare());
+ assert (git != null);
+ if (!linkfiles.isEmpty()) {
+ throw new UnsupportedOperationException(
+ JGitText.get().nonBareLinkFilesNotSupported);
+ }
- 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();
- }
- subRepo.close();
- git.add().addFilepattern(path).call();
- }
- for (CopyFile copyfile : copyfiles) {
- copyfile.copy();
- git.add().addFilepattern(copyfile.dest).call();
+ 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();
}
+ subRepo.close();
+ git.add().addFilepattern(path).call();
}
+ for (CopyFile copyfile : copyfiles) {
+ copyfile.copy();
+ 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);
}
/*