Git Repositories View: Bare Repositoy Support
[egit/spearce.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / CreateBranchPage.java
blobd932e6fa9c2691f564782371f11ec13225107c6c
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
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.repository;
13 import java.io.IOException;
14 import java.util.Map.Entry;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.egit.core.op.BranchOperation;
19 import org.eclipse.egit.ui.UIText;
20 import org.eclipse.jface.layout.GridDataFactory;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.jgit.lib.Constants;
23 import org.eclipse.jgit.lib.ObjectId;
24 import org.eclipse.jgit.lib.Ref;
25 import org.eclipse.jgit.lib.RefUpdate;
26 import org.eclipse.jgit.lib.Repository;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
40 /**
41 * Allows to create a branch based on another branch or with a selection of
42 * another branch
44 public class CreateBranchPage extends WizardPage {
46 private final Repository myRepository;
48 private final Ref myBaseBranch;
50 private final boolean remoteMode;
52 private Text nameText;
54 private Button checkout;
56 private Combo branchCombo;
58 /**
59 * Create a branch
61 * @param repo
62 * the repository
63 * @param baseBranch
64 * the branch to base the new branch on, may be null
65 * @param remote
66 * true if remote branch is to be created
68 public CreateBranchPage(Repository repo, Ref baseBranch, boolean remote) {
69 super(CreateBranchPage.class.getName());
70 this.myRepository = repo;
71 this.myBaseBranch = baseBranch;
72 this.remoteMode = remote;
73 if (this.remoteMode)
74 if (baseBranch != null)
75 setTitle(NLS.bind(
76 UIText.CreateBranchPage_CreateRemoteBaseOnTitle,
77 myBaseBranch.getName()));
78 else
79 setTitle(UIText.CreateBranchPage_CreateRemoteTitle);
80 else if (baseBranch != null)
81 setTitle(NLS.bind(UIText.CreateBranchPage_CreateLocalBasedTitle,
82 myBaseBranch.getName()));
83 else
84 setTitle(UIText.CreateBranchPage_CreateLocalTitle);
87 public void createControl(Composite parent) {
89 Composite main = new Composite(parent, SWT.NONE);
90 main.setLayout(new GridLayout(2, false));
92 Label sourceLabel = new Label(main, SWT.NONE);
93 sourceLabel.setText(UIText.CreateBranchPage_SourceBranchLabel);
94 sourceLabel.setToolTipText(UIText.CreateBranchPage_SourceBranchTooltip);
95 this.branchCombo = new Combo(main, SWT.READ_ONLY | SWT.DROP_DOWN);
97 GridDataFactory.fillDefaults().grab(true, false).applyTo(
98 this.branchCombo);
100 try {
101 for (Entry<String, Ref> ref : myRepository.getRefDatabase()
102 .getRefs(Constants.R_HEADS).entrySet()) {
103 if (!ref.getValue().isSymbolic())
104 this.branchCombo.add(ref.getValue().getName());
106 for (Entry<String, Ref> ref : myRepository.getRefDatabase()
107 .getRefs(Constants.R_REMOTES).entrySet()) {
108 if (!ref.getValue().isSymbolic())
109 this.branchCombo.add(ref.getValue().getName());
112 } catch (IOException e1) {
113 // ignore here
116 this.branchCombo.addSelectionListener(new SelectionAdapter() {
118 @Override
119 public void widgetSelected(SelectionEvent e) {
120 checkPage();
124 if (myBaseBranch != null) {
125 this.branchCombo.setText(myBaseBranch.getName());
126 this.branchCombo.setEnabled(false);
127 } else {
128 String fullBranch;
129 try {
130 fullBranch = myRepository.getFullBranch();
131 this.branchCombo.setText(fullBranch);
132 } catch (IOException e1) {
133 // ignore
137 Label nameLabel = new Label(main, SWT.NONE);
138 nameLabel.setText(UIText.CreateBranchPage_BranchNameLabel);
139 if (remoteMode)
140 nameLabel.setToolTipText(NLS.bind(
141 UIText.CreateBranchPage_BranchNameTooltip,
142 Constants.R_REMOTES));
143 else
144 nameLabel.setToolTipText(NLS.bind(
145 UIText.CreateBranchPage_BranchNameTooltip,
146 Constants.R_HEADS));
148 nameText = new Text(main, SWT.BORDER);
149 GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);
150 nameText.addModifyListener(new ModifyListener() {
152 public void modifyText(ModifyEvent e) {
153 checkPage();
157 boolean isBare = myRepository.getConfig().getBoolean(
158 "core", "bare", false); //$NON-NLS-1$ //$NON-NLS-2$
159 checkout = new Button(main, SWT.CHECK);
160 checkout.setText(UIText.CreateBranchPage_CheckoutButton);
161 // most of the time, we probably will check this out
162 // unless we have a bare repository which doesn't allow
163 // check out at all
164 checkout.setSelection(!isBare);
165 checkout.setEnabled(!isBare);
166 checkout.setVisible(!isBare);
167 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(
168 checkout);
169 checkout.addSelectionListener(new SelectionAdapter() {
171 @Override
172 public void widgetSelected(SelectionEvent e) {
173 checkPage();
178 setControl(main);
179 nameText.setFocus();
181 // in any case, we will have to enter the name
182 setPageComplete(false);
183 if (this.myBaseBranch != null)
184 setMessage(UIText.CreateBranchPage_ChosseNameMessage);
185 else
186 setMessage(UIText.CreateBranchPage_ChooseBranchAndNameMessage);
189 private void checkPage() {
190 setErrorMessage(null);
192 try {
194 if (branchCombo.getText().length() == 0) {
195 setErrorMessage(UIText.CreateBranchPage_MissingSourceMessage);
196 return;
198 if (nameText.getText().length() == 0) {
199 setErrorMessage(UIText.CreateBranchPage_MissingNameMessage);
200 return;
203 String fullName = getBranchName();
204 try {
205 if (myRepository.getRef(fullName) != null)
206 setErrorMessage(NLS.bind(
207 UIText.CreateBranchPage_BranchAlreadyExistsMessage,
208 fullName));
209 return;
210 } catch (IOException e) {
211 // ignore here
213 } finally {
214 setPageComplete(getErrorMessage() == null);
218 private String getBranchName() {
219 if (remoteMode)
220 return Constants.R_REMOTES + nameText.getText();
221 else
222 return Constants.R_HEADS + nameText.getText();
225 private String getSourceBranchName() {
226 if (myBaseBranch != null)
227 return myBaseBranch.getName();
228 else if (this.branchCombo != null)
229 return this.branchCombo.getText();
230 else
231 return null;
235 * @param monitor
236 * @throws CoreException
237 * @throws IOException
239 public void createBranch(IProgressMonitor monitor) throws CoreException,
240 IOException {
242 String newRefName = getBranchName();
243 RefUpdate updateRef;
245 monitor.beginTask(UIText.CreateBranchPage_CreatingBranchMessage,
246 IProgressMonitor.UNKNOWN);
247 updateRef = myRepository.updateRef(newRefName);
249 Ref sourceBranch;
250 if (myBaseBranch != null) {
251 sourceBranch = myBaseBranch;
252 } else {
253 sourceBranch = myRepository.getRef(getSourceBranchName());
255 ObjectId startAt = sourceBranch.getObjectId();
256 String startBranch = myRepository
257 .shortenRefName(sourceBranch.getName());
258 updateRef.setNewObjectId(startAt);
259 updateRef
260 .setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$
261 updateRef.update();
262 if (checkout.getSelection()) {
263 if (monitor.isCanceled())
264 return;
265 monitor.beginTask(UIText.CreateBranchPage_CheckingOutMessage,
266 IProgressMonitor.UNKNOWN);
267 new BranchOperation(myRepository, getBranchName()).execute(monitor);