Use try-with-resource to avoid leaks with RevWalk and TreeWalk
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / tree / command / CreateBranchCommand.java
blob171089f1bbe07564cdda3a090c94d7dbbd26364a
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 * Dariusz Luksza (dariusz@luksza.org - set action disabled when HEAD cannot
11 * be resolved
12 *******************************************************************************/
13 package org.eclipse.egit.ui.internal.repository.tree.command;
15 import java.io.IOException;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.egit.ui.Activator;
20 import org.eclipse.egit.ui.internal.repository.CreateBranchWizard;
21 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
22 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNodeType;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.jgit.lib.Ref;
25 import org.eclipse.jgit.revwalk.RevCommit;
26 import org.eclipse.jgit.revwalk.RevWalk;
28 /**
29 * Creates a branch using a simple dialog.
30 * <p>
31 * This is context-sensitive as it suggests the currently selected branch or (if
32 * not started from a branch) the currently checked-out branch as source branch.
33 * The user can override this suggestion.
35 public class CreateBranchCommand extends
36 RepositoriesViewCommandHandler<RepositoryTreeNode> {
37 @Override
38 public Object execute(ExecutionEvent event) throws ExecutionException {
39 final RepositoryTreeNode node = getSelectedNodes(event).get(0);
41 if (node.getType() == RepositoryTreeNodeType.ADDITIONALREF) {
42 Ref ref = (Ref) node.getObject();
43 try (RevWalk rw = new RevWalk(node.getRepository())) {
44 RevCommit baseCommit = rw
45 .parseCommit(ref.getLeaf().getObjectId());
46 WizardDialog dlg = new WizardDialog(
47 getShell(event),
48 new CreateBranchWizard(node.getRepository(), baseCommit.name()));
49 dlg.setHelpAvailable(false);
50 dlg.open();
51 } catch (IOException e) {
52 Activator.handleError(e.getMessage(), e, true);
54 return null;
56 String base = null;
57 if (node.getObject() instanceof Ref)
58 base = ((Ref) node.getObject()).getName();
59 new WizardDialog(getShell(event), new CreateBranchWizard(node
60 .getRepository(), base)).open();
61 return null;
64 @Override
65 public boolean isEnabled() {
66 return selectedRepositoryHasHead();