diff options
author | Thomas Wolf | 2016-05-21 07:19:51 +0000 |
---|---|---|
committer | Thomas Wolf | 2016-05-21 07:19:51 +0000 |
commit | 4d4255306b7ba22ef61d8144179281b8d209c3a0 (patch) | |
tree | 9e5288e46b782e960311963ae83b36ba31d5e06b | |
parent | 63a8fdc2e94e8c1514b04e096f445b296516ddeb (diff) | |
download | egit-4d4255306b7ba22ef61d8144179281b8d209c3a0.tar.gz egit-4d4255306b7ba22ef61d8144179281b8d209c3a0.tar.xz egit-4d4255306b7ba22ef61d8144179281b8d209c3a0.zip |
Ensure Git is closed using try with resource
Eliminates a number of "resource leak" warnings. No change in behavior;
newGit(repo).close() does not close the repository.
Changed remaining cases in egit.core, plus all cases in egit.ui.
Change-Id: I642c546e105e56dca836737a79b00ceeaedbe3e8
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
10 files changed, 86 insertions, 76 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/FetchOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/FetchOperation.java index feaef70de9..db82ffdedf 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/FetchOperation.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/FetchOperation.java @@ -120,23 +120,26 @@ public class FetchOperation { actMonitor = new NullProgressMonitor(); EclipseGitProgressTransformer gitMonitor = new EclipseGitProgressTransformer( actMonitor); - FetchCommand command; - if (rc == null) - command = new Git(repository).fetch().setRemote( - uri.toPrivateString()).setRefSpecs(specs); - else - command = new Git(repository).fetch().setRemote(rc.getName()); - command.setCredentialsProvider(credentialsProvider).setTimeout(timeout) - .setDryRun(dryRun).setProgressMonitor(gitMonitor); - if (tagOpt != null) - command.setTagOpt(tagOpt); - try { - operationResult = command.call(); - } catch (JGitInternalException e) { - throw new InvocationTargetException(e.getCause() != null ? e - .getCause() : e); - } catch (Exception e) { - throw new InvocationTargetException(e); + try (Git git = new Git(repository)) { + FetchCommand command; + if (rc == null) + command = git.fetch().setRemote(uri.toPrivateString()) + .setRefSpecs(specs); + else + command = git.fetch().setRemote(rc.getName()); + command.setCredentialsProvider(credentialsProvider) + .setTimeout(timeout).setDryRun(dryRun) + .setProgressMonitor(gitMonitor); + if (tagOpt != null) + command.setTagOpt(tagOpt); + try { + operationResult = command.call(); + } catch (JGitInternalException e) { + throw new InvocationTargetException( + e.getCause() != null ? e.getCause() : e); + } catch (Exception e) { + throw new InvocationTargetException(e); + } } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/RebaseOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/RebaseOperation.java index 86d5135a65..d0d37e372e 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/RebaseOperation.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/RebaseOperation.java @@ -136,15 +136,15 @@ public class RebaseOperation implements IEGitOperation { @Override public void run(IProgressMonitor actMonitor) throws CoreException { SubMonitor progress = SubMonitor.convert(actMonitor, 2); - RebaseCommand cmd = new Git(repository).rebase() - .setProgressMonitor( - new EclipseGitProgressTransformer(progress.newChild(1))); - MergeStrategy strategy = Activator.getDefault() - .getPreferredMergeStrategy(); - if (strategy != null) { - cmd.setStrategy(strategy); - } - try { + try (Git git = new Git(repository)) { + RebaseCommand cmd = git.rebase().setProgressMonitor( + new EclipseGitProgressTransformer( + progress.newChild(1))); + MergeStrategy strategy = Activator.getDefault() + .getPreferredMergeStrategy(); + if (strategy != null) { + cmd.setStrategy(strategy); + } if (handler != null) cmd.runInteractively(handler, true); if (operation == Operation.BEGIN) { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIRepositoryUtils.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIRepositoryUtils.java index c90f937fbf..0ffa34ac4b 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIRepositoryUtils.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIRepositoryUtils.java @@ -46,8 +46,11 @@ public final class UIRepositoryUtils { */ public static boolean handleUncommittedFiles(Repository repo, Shell shell) throws GitAPIException { - Status status = new Git(repo).status().call(); - if (status.hasUncommittedChanges()) { + Status status = null; + try (Git git = new Git(repo)) { + status = git.status().call(); + } + if (status != null && status.hasUncommittedChanges()) { List<String> files = new ArrayList<>(status.getModified()); Collections.sort(files); String repoName = Activator.getDefault().getRepositoryUtil() diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/CommitDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/CommitDialog.java index 57cc1848d5..cbc674cdf1 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/CommitDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/CommitDialog.java @@ -1266,15 +1266,18 @@ public class CommitDialog extends TitleAreaDialog { return new Action(UIText.CommitDialog_AddFileOnDiskToIndex) { @Override public void run() { - AddCommand addCommand = new Git(repository).add(); - for (Iterator<?> it = selection.iterator(); it.hasNext();) { - CommitItem commitItem = (CommitItem) it.next(); - addCommand.addFilepattern(commitItem.path); - } - try { - addCommand.call(); - } catch (Exception e) { - Activator.logError(UIText.CommitDialog_ErrorAddingFiles, e); + try (Git git = new Git(repository)) { + AddCommand addCommand = git.add(); + for (Iterator<?> it = selection.iterator(); it.hasNext();) { + CommitItem commitItem = (CommitItem) it.next(); + addCommand.addFilepattern(commitItem.path); + } + try { + addCommand.call(); + } catch (Exception e) { + Activator.logError(UIText.CommitDialog_ErrorAddingFiles, + e); + } } for (Iterator<?> it = selection.iterator(); it.hasNext();) { CommitItem commitItem = (CommitItem) it.next(); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/RenameBranchDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/RenameBranchDialog.java index 90ae66df58..1e1e2166ba 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/RenameBranchDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/RenameBranchDialog.java @@ -75,9 +75,9 @@ public class RenameBranchDialog extends AbstractBranchSelectionDialog { refPrefix), refPrefix, branchName); if (labelDialog.open() == Window.OK) { String newRefName = refPrefix + labelDialog.getValue(); - try { - new Git(repo).branchRename().setOldName(refName).setNewName( - labelDialog.getValue()).call(); + try (Git git = new Git(repo)) { + git.branchRename().setOldName(refName) + .setNewName(labelDialog.getValue()).call(); branchTree.refresh(); markRef(newRefName); } catch (Throwable e1) { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java index d868b8832d..040c5e6979 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java @@ -810,8 +810,9 @@ public class FetchGerritChangePage extends WizardPage { bop.execute(monitor); if (doCheckout) { - CheckoutCommand co = new Git(repository).checkout(); - try { + CheckoutCommand co = null; + try (Git git = new Git(repository)) { + co = git.checkout(); co.setName(textForBranch).call(); } catch (CheckoutConflictException e) { final CheckoutResult result = co.getResult(); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/reflog/ReflogViewContentProvider.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/reflog/ReflogViewContentProvider.java index 11ecaae656..501e74e2ff 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/reflog/ReflogViewContentProvider.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/reflog/ReflogViewContentProvider.java @@ -69,9 +69,9 @@ public class ReflogViewContentProvider implements ITreeContentProvider { public Object[] getElements(Object inputElement) { if (inputElement instanceof ReflogInput) { ReflogInput input = (ReflogInput) inputElement; - ReflogCommand command = new Git(input.repository).reflog(); - command.setRef(input.ref); - try { + try (Git git = new Git(input.repository)) { + ReflogCommand command = git.reflog(); + command.setRef(input.ref); return command.call().toArray(); } catch (Exception e) { Activator.logError("Error running reflog command", e); //$NON-NLS-1$ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryStatisticsPage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryStatisticsPage.java index 4403e6a84b..4cfd763987 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryStatisticsPage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryStatisticsPage.java @@ -63,11 +63,11 @@ public class RepositoryStatisticsPage extends PropertyPage { } Repository repo = AdapterUtils.adapt(getElement(), Repository.class); - if (repo == null) + if (repo == null) { return table; - Git git = new Git(repo); - GarbageCollectCommand gc = git.gc(); - try { + } + try (Git git = new Git(repo)) { + GarbageCollectCommand gc = git.gc(); Properties stats = gc.getStatistics(); table.setLinesVisible(true); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddToIndexCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddToIndexCommand.java index cdcb75ea38..194579b790 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddToIndexCommand.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddToIndexCommand.java @@ -39,20 +39,21 @@ public class AddToIndexCommand extends Repository repository = selectedNodes.get(0).getRepository(); IPath workTreePath = new Path(repository.getWorkTree().getAbsolutePath()); - AddCommand addCommand = new Git(repository).add(); + try (Git git = new Git(repository)) { + AddCommand addCommand = git.add(); - Collection<IPath> paths = getSelectedFileAndFolderPaths(event); - for (IPath path : paths) { - String repoRelativepath; - if (path.equals(workTreePath)) - repoRelativepath = "."; //$NON-NLS-1$ - else - repoRelativepath = path.removeFirstSegments( - path.matchingFirstSegments(workTreePath)) - .setDevice(null).toString(); - addCommand.addFilepattern(repoRelativepath); - } - try { + Collection<IPath> paths = getSelectedFileAndFolderPaths(event); + for (IPath path : paths) { + String repoRelativepath; + if (path.equals(workTreePath)) + repoRelativepath = "."; //$NON-NLS-1$ + else + repoRelativepath = path + .removeFirstSegments( + path.matchingFirstSegments(workTreePath)) + .setDevice(null).toString(); + addCommand.addFilepattern(repoRelativepath); + } addCommand.call(); } catch (GitAPIException e) { Activator.logError(UIText.AddToIndexCommand_addingFilesFailed, diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java index f896724a01..b9a77f9349 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java @@ -2238,15 +2238,14 @@ public class StagingView extends ViewPart implements IShowInSource { if (files.isEmpty() || repository == null) { return; } - CheckoutCommand checkoutCommand = new Git(repository) - .checkout(); - if (headRevision) { - checkoutCommand.setStartPoint(Constants.HEAD); - } - for (String path : files) { - checkoutCommand.addPath(path); - } - try { + try (Git git = new Git(repository)) { + CheckoutCommand checkoutCommand = git.checkout(); + if (headRevision) { + checkoutCommand.setStartPoint(Constants.HEAD); + } + for (String path : files) { + checkoutCommand.addPath(path); + } checkoutCommand.call(); if (!inaccessibleFiles.isEmpty()) { IndexDiffCacheEntry indexDiffCacheForRepository = org.eclipse.egit.core.Activator @@ -2505,7 +2504,7 @@ public class StagingView extends ViewPart implements IShowInSource { private void stage(IStructuredSelection selection) { StagingViewContentProvider contentProvider = getContentProvider(unstagedViewer); - final Git git = new Git(currentRepository); + final Repository repository = currentRepository; Iterator iterator = selection.iterator(); final List<String> addPaths = new ArrayList<>(); final List<String> rmPaths = new ArrayList<>(); @@ -2547,7 +2546,7 @@ public class StagingView extends ViewPart implements IShowInSource { Job addJob = new Job(UIText.StagingView_AddJob) { @Override protected IStatus run(IProgressMonitor monitor) { - try { + try (Git git = new Git(repository)) { AddCommand add = git.add(); for (String addPath : addPaths) add.addFilepattern(addPath); @@ -2576,7 +2575,7 @@ public class StagingView extends ViewPart implements IShowInSource { Job removeJob = new Job(UIText.StagingView_RemoveJob) { @Override protected IStatus run(IProgressMonitor monitor) { - try { + try (Git git = new Git(repository)) { RmCommand rm = git.rm().setCached(true); for (String rmPath : rmPaths) rm.addFilepattern(rmPath); @@ -2632,12 +2631,12 @@ public class StagingView extends ViewPart implements IShowInSource { if (paths.isEmpty()) return; - final Git git = new Git(currentRepository); + final Repository repository = currentRepository; Job resetJob = new Job(UIText.StagingView_ResetJob) { @Override protected IStatus run(IProgressMonitor monitor) { - try { + try (Git git = new Git(repository)) { ResetCommand reset = git.reset(); for (String path : paths) reset.addPath(path); |