Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4df5bcdd6ff8bf2baaebc547361f52a49a86c99f (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
/*******************************************************************************
 * Copyright (c) 2010 SAP AG.
 * 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
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository;

import java.io.IOException;
import java.util.Map.Entry;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.core.op.BranchOperation;
import org.eclipse.egit.ui.UIText;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Allows to create a new local branch based on another branch. The source
 * branch can be selected using a drop down.
 */
public class CreateBranchPage extends WizardPage {

	private final Repository myRepository;

	private final Ref myBaseBranch;

	private Text nameText;

	private Button checkout;

	private Combo branchCombo;

	/**
	 * Constructs this page.
	 * <p>
	 * If a base branch is provided, the drop down will be selected accordingly
	 *
	 * @param repo
	 *            the repository
	 * @param baseBranch
	 *            the branch to base the new branch on, may be null
	 */
	public CreateBranchPage(Repository repo, Ref baseBranch) {
		super(CreateBranchPage.class.getName());
		this.myRepository = repo;
		this.myBaseBranch = baseBranch;
		setTitle(UIText.CreateBranchPage_Title);
	}

	public void createControl(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout(3, false));

		Label sourceLabel = new Label(main, SWT.NONE);
		sourceLabel.setText(UIText.CreateBranchPage_SourceBranchLabel);
		sourceLabel.setToolTipText(UIText.CreateBranchPage_SourceBranchTooltip);
		this.branchCombo = new Combo(main, SWT.READ_ONLY | SWT.DROP_DOWN);

		GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(
				this.branchCombo);

		try {
			for (Entry<String, Ref> ref : myRepository.getRefDatabase()
					.getRefs(Constants.R_HEADS).entrySet()) {
				if (!ref.getValue().isSymbolic())
					this.branchCombo.add(ref.getValue().getName());
			}
			for (Entry<String, Ref> ref : myRepository.getRefDatabase()
					.getRefs(Constants.R_REMOTES).entrySet()) {
				if (!ref.getValue().isSymbolic())
					this.branchCombo.add(ref.getValue().getName());
			}
			for (Entry<String, Ref> ref : myRepository.getRefDatabase()
					.getRefs(Constants.R_TAGS).entrySet()) {
				if (!ref.getValue().isSymbolic())
					this.branchCombo.add(ref.getValue().getName());
			}

		} catch (IOException e1) {
			// ignore here
		}

		this.branchCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				checkPage();
			}
		});
		// select the current branch in the drop down
		if (myBaseBranch != null) {
			this.branchCombo.setText(myBaseBranch.getName());
		}

		Label nameLabel = new Label(main, SWT.NONE);
		nameLabel.setText(UIText.CreateBranchPage_BranchNameLabel);

		// we visualize the prefix here
		Text prefix = new Text(main, SWT.NONE);
		prefix.setText(Constants.R_HEADS);
		prefix.setEnabled(false);

		nameText = new Text(main, SWT.BORDER);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);

		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				checkPage();
			}
		});

		boolean isBare = myRepository.getConfig().getBoolean(
				"core", "bare", false); //$NON-NLS-1$ //$NON-NLS-2$
		checkout = new Button(main, SWT.CHECK);
		checkout.setText(UIText.CreateBranchPage_CheckoutButton);
		// most of the time, we probably will check this out
		// unless we have a bare repository which doesn't allow
		// check out at all
		checkout.setSelection(!isBare);
		checkout.setEnabled(!isBare);
		checkout.setVisible(!isBare);
		GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(
				checkout);
		checkout.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				checkPage();
			}

		});

		Dialog.applyDialogFont(main);
		setControl(main);
		nameText.setFocus();
		if (myBaseBranch != null
				&& (myBaseBranch.getName().startsWith(Constants.R_REMOTES) || myBaseBranch
						.getName().startsWith(Constants.R_TAGS))) {
			// additional convenience: the last part of the name is suggested
			// as name for the local branch
			nameText.setText(myBaseBranch.getName().substring(
					myBaseBranch.getName().lastIndexOf('/') + 1));
			checkPage();
		} else {
			// in any case, we will have to enter the name
			setPageComplete(false);
		}

		if (this.myBaseBranch != null && this.nameText.getText().length() == 0)
			setMessage(UIText.CreateBranchPage_ChooseNameMessage);
		else
			setMessage(UIText.CreateBranchPage_ChooseBranchAndNameMessage);
	}

	private void checkPage() {
		setErrorMessage(null);

		try {

			if (branchCombo.getText().length() == 0) {
				setErrorMessage(UIText.CreateBranchPage_MissingSourceMessage);
				return;
			}
			if (nameText.getText().length() == 0) {
				setErrorMessage(UIText.CreateBranchPage_MissingNameMessage);
				return;
			}

			String fullName = getBranchName();
			try {
				if (myRepository.getRef(fullName) != null)
					setErrorMessage(NLS.bind(
							UIText.CreateBranchPage_BranchAlreadyExistsMessage,
							fullName));
				return;
			} catch (IOException e) {
				// ignore here
			}
		} finally {
			setPageComplete(getErrorMessage() == null);
		}
	}

	private String getBranchName() {
		return Constants.R_HEADS + nameText.getText();
	}

	private String getSourceBranchName() {
		if (myBaseBranch != null)
			return myBaseBranch.getName();
		else if (this.branchCombo != null)
			return this.branchCombo.getText();
		else
			return null;
	}

	/**
	 * @param monitor
	 * @throws CoreException
	 * @throws IOException
	 */
	public void createBranch(IProgressMonitor monitor) throws CoreException,
			IOException {

		monitor.beginTask(UIText.CreateBranchPage_CreatingBranchMessage,
				IProgressMonitor.UNKNOWN);

		String newRefName = getBranchName();

		RefUpdate updateRef = myRepository.updateRef(newRefName);
		ObjectId startAt = new RevWalk(myRepository).parseCommit(myRepository
				.resolve(getSourceBranchName()));

		updateRef.setNewObjectId(startAt);
		updateRef.setRefLogMessage(
				"branch: Created from " + getSourceBranchName(), false); //$NON-NLS-1$
		updateRef.update();

		if (checkout.getSelection()) {
			if (monitor.isCanceled())
				return;
			monitor.beginTask(UIText.CreateBranchPage_CheckingOutMessage,
					IProgressMonitor.UNKNOWN);
			new BranchOperation(myRepository, getBranchName()).execute(monitor);
		}
	}
}

Back to the top