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
9 * Mathias Kinzler (SAP AG) - initial implementation
10 * Dariusz Luksza (dariusz@luksza.org - set action disabled when HEAD cannot
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
;
29 * Creates a branch using a simple dialog.
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
> {
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(
48 new CreateBranchWizard(node
.getRepository(), baseCommit
.name()));
49 dlg
.setHelpAvailable(false);
51 } catch (IOException e
) {
52 Activator
.handleError(e
.getMessage(), e
, true);
57 if (node
.getObject() instanceof Ref
)
58 base
= ((Ref
) node
.getObject()).getName();
59 new WizardDialog(getShell(event
), new CreateBranchWizard(node
60 .getRepository(), base
)).open();
65 public boolean isEnabled() {
66 return selectedRepositoryHasHead();