1 package com.proalpha.pds.gitutils.cherrypick;
3 import java.io.IOException;
4 import java.text.DateFormat;
5 import java.util.ArrayList;
6 import java.util.Arrays;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.SubMonitor;
12 import org.eclipse.egit.core.internal.util.ProjectUtil;
13 import org.eclipse.egit.ui.internal.push.PushBranchWizard;
14 import org.eclipse.egit.ui.internal.push.PushWizardDialog;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
22 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.LabelProvider;
25 import org.eclipse.jface.viewers.StyledString;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.viewers.TreeViewerColumn;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.jgit.api.CherryPickResult;
30 import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus;
31 import org.eclipse.jgit.lib.Constants;
32 import org.eclipse.jgit.lib.ObjectId;
33 import org.eclipse.jgit.lib.Ref;
34 import org.eclipse.jgit.lib.Repository;
35 import org.eclipse.jgit.revwalk.RevCommit;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Group;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.swt.widgets.Tree;
50 import org.eclipse.swt.widgets.TreeColumn;
51 import org.eclipse.ui.PlatformUI;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.proalpha.git.PaGit;
56 import com.proalpha.git.model.PaCherryPickResult;
57 import com.proalpha.pds.gitutils.external.DbAndBinaryOperation;
58 import com.proalpha.pds.gitutils.mylyn.CheckoutAction;
59 import com.proalpha.pds.gitutils.mylyn.ReconcileActionSettings;
60 import com.proalpha.pds.paconnector.Activator;
66 private final PaCherryPickResult result;
67 private final List<RevCommit> successfulPicks;
68 private final List<RevCommit> openPicks;
69 private Button btnPush;
70 private Label lblHint;
72 private IProgressMonitor monitor;
75 protected Control createDialogArea(Composite parent) {
77 Composite main =
new Composite(parent, SWT.NONE);
78 GridLayoutFactory.swtDefaults().applyTo(main);
79 GridDataFactory.fillDefaults().indent(0, 0).grab(
true,
true).applyTo(main);
80 main.setLayout(
new GridLayout(1,
false));
82 Composite composite =
new Composite(main, SWT.NONE);
83 composite.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
false,
false, 1, 1));
84 composite.setLayout(
new GridLayout(2,
false));
85 Label lblNewLabel =
new Label(composite, SWT.NONE);
86 lblNewLabel.setLayoutData(
new GridData(SWT.RIGHT, SWT.CENTER,
false,
false, 1, 1));
87 lblNewLabel.setText(
"Result Status:");
89 Text text =
new Text(composite, SWT.READ_ONLY);
90 text.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
true,
false, 1, 1));
91 text.setText(result.getStatus().toString());
92 text.setFont(JFaceResources.getFont(
"Courier New"));
94 Group grpPerformedcommits =
new Group(main, SWT.NONE);
95 grpPerformedcommits.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
false,
false, 1, 1));
96 grpPerformedcommits.setText(
"Performed Commits");
98 TreeViewer succViewer =
new TreeViewer(grpPerformedcommits);
99 Tree succTree = succViewer.getTree();
100 succTree.setBounds(0, 22, 460, 130);
101 succViewer.setAutoExpandLevel(2);
102 fillTreeView(succViewer, this.successfulPicks);
104 Group grpOpenCommits =
new Group(main, SWT.NONE);
105 grpOpenCommits.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
false,
false, 1, 1));
106 grpOpenCommits.setText(
"Open Commits");
108 TreeViewer openViewer =
new TreeViewer(grpOpenCommits);
109 Tree openTree = openViewer.getTree();
110 openTree.setBounds(10, 22, 460, 90);
111 openViewer.setAutoExpandLevel(2);
112 fillTreeView(openViewer, this.openPicks);
114 btnPush =
new Button(main, SWT.CHECK);
115 btnPush.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
false,
false, 1, 1));
116 btnPush.addSelectionListener(
new SelectionListener() {
119 public void widgetSelected(SelectionEvent event) {
120 if (!btnPush.getSelection()) {
121 lblHint.setText(
"Hint: Dont't forget to push your picked branch to the remote Repository!");
129 public void widgetDefaultSelected(SelectionEvent event) {
130 widgetSelected(event);
133 btnPush.setText(
"Push automatically to server");
134 btnPush.setSelection(
true);
138 Group grpCheckoutSettings =
new Group(main, SWT.NONE);
139 grpCheckoutSettings.setText(
"Reconcile settings");
140 grpCheckoutSettings.setLayoutData(
new GridData(SWT.FILL, SWT.FILL,
false,
false, 1, 1));
141 grpCheckoutSettings.setLayout(
new GridLayout(1,
false));
142 Combo cmbCheckoutAction =
new Combo(grpCheckoutSettings, SWT.CHECK);
143 cmbCheckoutAction.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
true,
false, 1, 1));
144 cmbCheckoutAction.addSelectionListener(
new SelectionListener() {
145 public void widgetSelected(SelectionEvent selectedItem) {
146 currentAction =
null;
147 if (selectedItem !=
null) {
153 public void widgetDefaultSelected(SelectionEvent e) {
157 cmbCheckoutAction.setBounds(76, 22, 300, 16);
159 cmbCheckoutAction.setItems(checkoutActions);
161 int selectionIndex = determineSelectionIndex(checkoutActions,
CheckoutAction.LOAD_XML.getValue());
162 cmbCheckoutAction.select(selectionIndex);
164 lblHint =
new Label(main, SWT.NONE);
165 lblHint.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER,
false,
false, 1, 1));
167 if (result.getStatus() == CherryPickResult.CherryPickStatus.FAILED) {
169 "Something wicked happended!\nThe cherrypick terribly failed :-/\nGo to history view to reset your branch.");
170 text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
172 if (result.getStatus() == CherryPickResult.CherryPickStatus.OK) {
173 text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
175 if (result.getStatus() == CherryPickResult.CherryPickStatus.CONFLICTING) {
177 "There is a conflict in your pick.\nPlease resolve the conflict in Git Staging view,\ncommit your change and resume the cherrypick!");
178 text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
184 private int determineSelectionIndex(String[] checkoutActions, String searchValue) {
185 int selectionIndex = 0;
186 for (String action : checkoutActions) {
188 if (action.equals(searchValue)) {
193 return selectionIndex;
203 this.monitor = monitor;
204 this.result = cpResult;
205 this.successfulPicks = cpResult.getSuccessfulPicks();
206 this.openPicks = cpResult.getOpenPicks();
210 protected void okPressed() {
211 CherryPickStatus status = this.result.getStatus();
213 if (status == CherryPickStatus.OK && btnPush.getSelection()) {
215 final Repository repository = PaGit.getInstance().getGit().getRepository();
216 PushBranchWizard wizard =
null;
218 String fullBranch = repository.getFullBranch();
220 if (fullBranch !=
null && fullBranch.startsWith(Constants.R_HEADS))
221 ref = repository.exactRef(fullBranch);
224 wizard =
new PushBranchWizard(repository, ref);
226 ObjectId
id = repository.resolve(repository.getFullBranch());
227 wizard =
new PushBranchWizard(repository,
id);
230 new PushWizardDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(), wizard).open();
233 if (currentAction != CheckoutAction.NONE) {
234 performReconcileJob(repository, currentAction, monitor);
237 }
catch (IOException ex) {
238 PlatformUI.getWorkbench().getDisplay().asyncExec(
239 () -> MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
240 ex.getMessage(), ex.getLocalizedMessage()));
246 private void performReconcileJob(Repository repository, CheckoutAction checkoutAction, IProgressMonitor monitor) {
248 IProject[] projects = ProjectUtil.getProjects(repository);
249 ArrayList<IProject> affectedProjects =
new ArrayList<>(Arrays.asList(projects));
250 SubMonitor subMonitorArtifacts = SubMonitor.convert(monitor,
"database reconcile and artifact update", 10);
252 if (checkoutAction.equals(CheckoutAction.FULL_RECONCILE))
253 Activator.getDefault().getProjectManager().closeProjects(affectedProjects, subMonitorArtifacts);
255 executeDbBinaryOperation(repository, checkoutAction, subMonitorArtifacts);
257 if (checkoutAction.equals(CheckoutAction.FULL_RECONCILE))
258 Activator.getDefault().getProjectManager().openProjects(affectedProjects, subMonitorArtifacts);
261 public void executeDbBinaryOperation(Repository repository, CheckoutAction checkoutAction,
262 SubMonitor subMonitorArtifacts) {
263 DbAndBinaryOperation updateDbBinaryOp =
new DbAndBinaryOperation(repository,
264 new ReconcileActionSettings(checkoutAction));
265 updateDbBinaryOp.setProgressMonitor(subMonitorArtifacts);
266 updateDbBinaryOp.execute();
270 protected void createButtonsForButtonBar(Composite parent) {
271 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL,
true);
275 protected void configureShell(Shell newShell) {
276 super.configureShell(newShell);
277 newShell.setText(
"CherryPick Result: " + result.getStatus());
280 private void fillTreeView(TreeViewer treeViewer, List<RevCommit> commits) {
282 TreeViewerColumn treeViewerColumn_2 =
new TreeViewerColumn(treeViewer, SWT.NONE);
283 TreeColumn trclmnNewColumn_2 = treeViewerColumn_2.getColumn();
284 trclmnNewColumn_2.setWidth(80);
285 trclmnNewColumn_2.setText(
"CommitID");
286 treeViewerColumn_2.setLabelProvider(
new DelegatingStyledCellLabelProvider(
new NameLabelProvider()));
288 TreeViewerColumn treeViewerColumn_1 =
new TreeViewerColumn(treeViewer, SWT.NONE);
289 TreeColumn trclmnNewColumn_1 = treeViewerColumn_1.getColumn();
290 trclmnNewColumn_1.setWidth(200);
291 trclmnNewColumn_1.setText(
"Message");
292 trclmnNewColumn_1.setAlignment(SWT.LEFT);
293 treeViewerColumn_1.setLabelProvider(
new DelegatingStyledCellLabelProvider(
new MessageLabelProvider()));
295 TreeViewerColumn treeViewerColumn =
new TreeViewerColumn(treeViewer, SWT.NONE);
296 TreeColumn trclmnNewColumn = treeViewerColumn.getColumn();
297 trclmnNewColumn.setWidth(100);
298 trclmnNewColumn.setText(
"Author");
299 trclmnNewColumn.setAlignment(SWT.LEFT);
300 treeViewerColumn.setLabelProvider(
new DelegatingStyledCellLabelProvider(
new AuthorLabelProvider()));
302 TreeViewerColumn dateColumn =
new TreeViewerColumn(treeViewer, SWT.NONE);
303 TreeColumn trclmnDateColumn = dateColumn.getColumn();
304 trclmnDateColumn.setWidth(70);
305 trclmnDateColumn.setText(
"Date");
306 trclmnDateColumn.setAlignment(SWT.LEFT);
307 dateColumn.setLabelProvider(
new DelegatingStyledCellLabelProvider(
new DateLabelProvider()));
309 treeViewer.setContentProvider(
new CommitProvider());
310 treeViewer.setInput(commits);
312 treeViewer.getTree().setHeaderVisible(
true);
313 treeViewer.getTree().setLinesVisible(
true);
318 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
322 public void dispose() {
326 public Object[] getElements(Object inputElement) {
327 if (inputElement instanceof List) {
328 return ((List<?>) inputElement).toArray();
330 return new Object[0];
334 public Object[] getChildren(Object parentElement) {
339 public Object getParent(Object element) {
344 public boolean hasChildren(Object element) {
350 class NameLabelProvider
extends LabelProvider implements IStyledLabelProvider {
353 public StyledString getStyledText(Object element) {
354 if (element instanceof RevCommit) {
355 RevCommit commit = (RevCommit) element;
356 return new StyledString(commit.getName().substring(0, 8));
363 class MessageLabelProvider
extends LabelProvider implements IStyledLabelProvider {
366 public StyledString getStyledText(Object element) {
367 if (element instanceof RevCommit) {
368 RevCommit commit = (RevCommit) element;
369 return new StyledString(commit.getFullMessage().replace(
"\n",
" "));
376 class AuthorLabelProvider
extends LabelProvider implements IStyledLabelProvider {
379 public StyledString getStyledText(Object element) {
380 if (element instanceof RevCommit) {
381 RevCommit commit = (RevCommit) element;
382 return new StyledString(commit.getAuthorIdent().getName());
389 class DateLabelProvider
extends LabelProvider implements IStyledLabelProvider {
391 private DateFormat dateLabelFormat;
393 public DateLabelProvider() {
394 dateLabelFormat = DateFormat.getDateInstance();
398 public StyledString getStyledText(Object element) {
399 if (element instanceof RevCommit) {
400 RevCommit commit = (RevCommit) element;
401 return new StyledString(dateLabelFormat.format(commit.getCommitterIdent().getWhen()));
CherryPickResultDialog(Shell parentShell, IProgressMonitor monitor, PaCherryPickResult cpResult)
static String[] getActions()
static CheckoutAction getAction(String value)