Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 239dff07098d3706df0a98496cfe14d1c3901bca (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*******************************************************************************
 * Copyright (c) 2011 SAP AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.dialogs;

import static org.eclipse.jgit.lib.Constants.R_HEADS;
import static org.eclipse.jgit.lib.Constants.R_REMOTES;

import java.io.IOException;
import java.util.Iterator;

import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.CommonUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.branch.BranchOperationUI;
import org.eclipse.egit.ui.internal.repository.CreateBranchWizard;
import org.eclipse.egit.ui.internal.repository.tree.RefNode;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchCommandConstants;

/**
 * Dialog for selecting and editing a branch.
 */
public class BranchSelectionAndEditDialog extends
		AbstractBranchSelectionDialog {

	/**
	 * Id of the New branch button.
	 */
	public static final int NEW = 100;

	private String currentBranch;

	private Button deleteButton;

	private Button renameButton;

	private Button newButton;

	/**
	 * @param parentShell
	 * @param repo
	 * @param refToMark
	 * @param settings
	 */
	public BranchSelectionAndEditDialog(Shell parentShell, Repository repo,
			String refToMark, int settings) {
		super(parentShell, repo, refToMark, settings);
		try {
			currentBranch = repo.getFullBranch();
		} catch (IOException e) {
			currentBranch = null;
		}
	}

	BranchSelectionAndEditDialog(Shell parentShell, Repository repo,
			int settings) {
		// no initial selection
		this(parentShell, repo, null, settings);
	}

	@Override
	protected String getMessageText() {
		return UIText.BranchSelectionAndEditDialog_Message;
	}

	@Override
	protected String getTitle() {
		return UIText.BranchSelectionAndEditDialog_Title;
	}

	@Override
	protected String getWindowTitle() {
		return UIText.BranchSelectionAndEditDialog_WindowTitle;
	}

	@Override
	protected void refNameSelected(String refName) {
		boolean tagSelected = refName != null
				&& refName.startsWith(Constants.R_TAGS);

		boolean branchSelected = refName != null
				&& (refName.startsWith(Constants.R_HEADS) || refName
						.startsWith(Constants.R_REMOTES));

		// handle multiple selection
		if (((TreeSelection) branchTree.getSelection()).size() > 1) {
			TreeSelection selection = (TreeSelection) branchTree
					.getSelection();
			deleteButton.setEnabled(onlyBranchesExcludingCurrentAreSelected(selection));
			renameButton.setEnabled(false);
			if (newButton != null)
				newButton.setEnabled(false);
			setOkButtonEnabled(false);
		} else {
			// we don't support rename on tags
			renameButton.setEnabled(branchSelected && !tagSelected);
			deleteButton.setEnabled(branchSelected && !tagSelected
					&& !isCurrentBranch(refName));
			// new button should be always enabled
			if (newButton != null)
				newButton.setEnabled(true);
			setOkButtonEnabled((branchSelected || tagSelected)
					&& !isCurrentBranch(refName));
		}

		Button okButton = getButton(Window.OK);
		if (okButton != null) {
			if (BranchOperationUI.checkoutWillShowQuestionDialog(refName))
				okButton.setText(UIText.CheckoutDialog_OkCheckoutWithQuestion);
			else
				okButton.setText(UIText.CheckoutDialog_OkCheckout);
		}
	}

	private boolean isCurrentBranch(String refName) {
		if (refName != null)
			return refName.equals(currentBranch);
		return false;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createNewButton(parent);
		createRenameButton(parent);
		createDeleteButton(parent);

		super.createButtonsForButtonBar(parent);

		// can't advance without a selection
		getButton(Window.OK).setEnabled(!branchTree.getSelection().isEmpty());
	}

	/**
	 * Creates button for creating a new branch.
	 *
	 * @param parent
	 * @return button
	 */
	protected Button createNewButton(Composite parent) {
		newButton = createButton(parent, NEW,
				UIText.BranchSelectionAndEditDialog_NewBranch, false);
		setButtonLayoutData(newButton);
		newButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// try to read default source ref from git config
				// in the case that no base is selected in dialog
				String base = refNameFromDialog();
				if (base == null) {
					String sourceRef = repo.getConfig().getString(
							ConfigConstants.CONFIG_WORKFLOW_SECTION, null,
							ConfigConstants.CONFIG_KEY_DEFBRANCHSTARTPOINT);
					try {
						Ref ref = repo.findRef(sourceRef);
						if (ref != null) {
							base = ref.getName();
						}
					} catch (IOException e1) {
						// base = null;
					}
				}
				CreateBranchWizard wiz = new CreateBranchWizard(repo, base);
				if (new WizardDialog(getShell(), wiz).open() == Window.OK) {
					String newRefName = wiz.getNewBranchName();
					try {
						branchTree.refresh();
						markRef(Constants.R_HEADS + newRefName);
						if (newRefName.equals(repo.getBranch()))
							// close branch selection dialog when new branch was
							// already checked out from new branch wizard
							BranchSelectionAndEditDialog.this.okPressed();
					} catch (Throwable e1) {
						reportError(
								e1,
								UIText.BranchSelectionAndEditDialog_ErrorCouldNotCreateNewRef,
								newRefName);
					}
				}
			}
		});
		return newButton;
	}

	/**
	 * Creates button for renaming a branch.
	 *
	 * @param parent
	 * @return button
	 */
	protected Button createRenameButton(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns++;
		renameButton = new Button(parent, SWT.PUSH);
		renameButton.setFont(JFaceResources.getDialogFont());
		renameButton.setText(UIText.BranchSelectionAndEditDialog_Rename);
		setButtonLayoutData(renameButton);
		renameButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Ref selectedRef = refFromDialog();

				String namePrefix = selectedRef.getName()
						.startsWith(Constants.R_REMOTES) ? Constants.R_REMOTES
								: Constants.R_HEADS;

				BranchRenameDialog dialog = new BranchRenameDialog(getShell(),
						repo, selectedRef);
				if (dialog.open() == Window.OK) {
					branchTree.refresh();
					markRef(namePrefix + dialog.getNewName());
				}
			}
		});
		renameButton.setEnabled(false);
		return renameButton;
	}

	/**
	 * Creates button for deleting a branch.
	 *
	 * @param parent
	 * @return button
	 */
	protected Button createDeleteButton(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns++;
		deleteButton = new Button(parent, SWT.PUSH);
		deleteButton.setFont(JFaceResources.getDialogFont());
		deleteButton.setText(UIText.BranchSelectionAndEditDialog_Delete);
		setButtonLayoutData(deleteButton);

		deleteButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent selectionEvent) {
				try {
					CommonUtils.runCommand(
							IWorkbenchCommandConstants.EDIT_DELETE,
							(IStructuredSelection) branchTree.getSelection());
					branchTree.refresh();
				} catch (Throwable e) {
					reportError(
							e,
							UIText.BranchSelectionAndEditDialog_ErrorCouldNotDeleteRef,
							refNameFromDialog());
				}
			}
		});
		deleteButton.setEnabled(false);
		return deleteButton;
	}

	private void reportError(Throwable e, String message, Object... args) {
		String msg = NLS.bind(message, args);
		Activator.handleError(msg, e, true);
	}

	private boolean onlyBranchesExcludingCurrentAreSelected(
			TreeSelection selection) {
		Iterator selIterator = selection.iterator();
		while (selIterator.hasNext()) {
			Object sel = selIterator.next();
			if (sel instanceof RefNode) {
				RefNode node = (RefNode) sel;
				String refName = node.getObject().getName();
				if (!refName.startsWith(R_HEADS)
						&& !refName.startsWith(R_REMOTES))
					return false;
				if (isCurrentBranch(refName))
					return false;
			} else
				return false;
		}
		return true;
	}

}

Back to the top