blob: 94aecfa736453eb22b0ce41cc657737bad1cd53d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*******************************************************************************
* Copyright (C) 2012, Mathias Kinzler <mathias.kinzler@sap.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.egit.ui.internal.history.command;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.dialogs.BranchRenameDialog;
import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog;
import org.eclipse.egit.ui.internal.history.GitHistoryPage;
import org.eclipse.egit.ui.internal.history.HistoryPageInput;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revplot.PlotCommit;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
/**
* Rename a branch pointing to a commit.
*/
public class RenameBranchOnCommitHandler extends AbstractHistoryCommandHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
GitHistoryPage page = getPage();
final Repository repository = getRepository(page);
if (repository == null)
return null;
List<Ref> branchesOfCommit = getBranchesOfCommit(page);
// this should have been checked by isEnabled()
if (branchesOfCommit.isEmpty())
return null;
final Shell shell = getPart(event).getSite().getShell();
final Ref branchToRename;
if (branchesOfCommit.size() > 1) {
BranchSelectionDialog<Ref> dlg = new BranchSelectionDialog<Ref>(
shell,
branchesOfCommit,
UIText.RenameBranchOnCommitHandler_SelectBranchDialogTitle,
UIText.RenameBranchOnCommitHandler_SelectBranchDialogMessage,
SWT.SINGLE);
if (dlg.open() != Window.OK)
return null;
branchToRename = dlg.getSelectedNode();
} else
branchToRename = branchesOfCommit.get(0);
new BranchRenameDialog(shell, repository, branchToRename).open();
return null;
}
private List<Ref> getBranchesOfCommit(GitHistoryPage page) {
final List<Ref> branchesOfCommit = new ArrayList<Ref>();
IStructuredSelection selection = getSelection(page);
if (selection.isEmpty())
return branchesOfCommit;
PlotCommit commit = (PlotCommit) selection.getFirstElement();
int refCount = commit.getRefCount();
for (int i = 0; i < refCount; i++) {
Ref ref = commit.getRef(i);
String refName = ref.getName();
if (refName.startsWith(Constants.R_HEADS)
|| refName.startsWith(Constants.R_REMOTES))
branchesOfCommit.add(ref);
}
return branchesOfCommit;
}
private Repository getRepository(GitHistoryPage page) {
if (page == null)
return null;
HistoryPageInput input = page.getInputInternal();
if (input == null)
return null;
return input.getRepository();
}
@Override
public boolean isEnabled() {
GitHistoryPage page = getPage();
return getRepository(page) != null
&& !(getBranchesOfCommit(page).isEmpty());
}
}
|