View Javadoc
1   /*
2    * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
3    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  package org.eclipse.jgit.api;
45  
46  import static org.eclipse.jgit.lib.Constants.HEAD;
47  import static org.eclipse.jgit.lib.Constants.R_HEADS;
48  
49  import java.io.IOException;
50  import java.text.MessageFormat;
51  
52  import org.eclipse.jgit.api.errors.GitAPIException;
53  import org.eclipse.jgit.api.errors.InvalidRefNameException;
54  import org.eclipse.jgit.api.errors.JGitInternalException;
55  import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
56  import org.eclipse.jgit.api.errors.RefNotFoundException;
57  import org.eclipse.jgit.errors.AmbiguousObjectException;
58  import org.eclipse.jgit.internal.JGitText;
59  import org.eclipse.jgit.lib.ConfigConstants;
60  import org.eclipse.jgit.lib.Constants;
61  import org.eclipse.jgit.lib.ObjectId;
62  import org.eclipse.jgit.lib.Ref;
63  import org.eclipse.jgit.lib.RefUpdate;
64  import org.eclipse.jgit.lib.RefUpdate.Result;
65  import org.eclipse.jgit.lib.Repository;
66  import org.eclipse.jgit.lib.StoredConfig;
67  import org.eclipse.jgit.revwalk.RevCommit;
68  import org.eclipse.jgit.revwalk.RevWalk;
69  
70  /**
71   * Used to create a local branch.
72   *
73   * @see <a
74   *      href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
75   *      >Git documentation about Branch</a>
76   */
77  public class CreateBranchCommand extends GitCommand<Ref> {
78  	private String name;
79  
80  	private boolean force = false;
81  
82  	private SetupUpstreamMode upstreamMode;
83  
84  	private String startPoint = HEAD;
85  
86  	private RevCommit startCommit;
87  
88  	/**
89  	 * The modes available for setting up the upstream configuration
90  	 * (corresponding to the --set-upstream, --track, --no-track options
91  	 *
92  	 */
93  	public enum SetupUpstreamMode {
94  		/**
95  		 * Corresponds to the --track option
96  		 */
97  		TRACK,
98  		/**
99  		 * Corresponds to the --no-track option
100 		 */
101 		NOTRACK,
102 		/**
103 		 * Corresponds to the --set-upstream option
104 		 */
105 		SET_UPSTREAM;
106 	}
107 
108 	/**
109 	 * Constructor for CreateBranchCommand
110 	 *
111 	 * @param repo
112 	 *            the {@link org.eclipse.jgit.lib.Repository}
113 	 */
114 	protected CreateBranchCommand(Repository repo) {
115 		super(repo);
116 	}
117 
118 	/** {@inheritDoc} */
119 	@Override
120 	public Ref call() throws GitAPIException, RefAlreadyExistsException,
121 			RefNotFoundException, InvalidRefNameException {
122 		checkCallable();
123 		processOptions();
124 		try (RevWalklk.html#RevWalk">RevWalk revWalk = new RevWalk(repo)) {
125 			Ref refToCheck = repo.findRef(name);
126 			boolean exists = refToCheck != null
127 					&& refToCheck.getName().startsWith(R_HEADS);
128 			if (!force && exists)
129 				throw new RefAlreadyExistsException(MessageFormat.format(
130 						JGitText.get().refAlreadyExists1, name));
131 
132 			ObjectId startAt = getStartPointObjectId();
133 			String startPointFullName = null;
134 			if (startPoint != null) {
135 				Ref baseRef = repo.findRef(startPoint);
136 				if (baseRef != null)
137 					startPointFullName = baseRef.getName();
138 			}
139 
140 			// determine whether we are based on a commit,
141 			// a branch, or a tag and compose the reflog message
142 			String refLogMessage;
143 			String baseBranch = ""; //$NON-NLS-1$
144 			if (startPointFullName == null) {
145 				String baseCommit;
146 				if (startCommit != null)
147 					baseCommit = startCommit.getShortMessage();
148 				else {
149 					RevCommit commit = revWalk.parseCommit(repo
150 							.resolve(getStartPointOrHead()));
151 					baseCommit = commit.getShortMessage();
152 				}
153 				if (exists)
154 					refLogMessage = "branch: Reset start-point to commit " //$NON-NLS-1$
155 							+ baseCommit;
156 				else
157 					refLogMessage = "branch: Created from commit " + baseCommit; //$NON-NLS-1$
158 
159 			} else if (startPointFullName.startsWith(R_HEADS)
160 					|| startPointFullName.startsWith(Constants.R_REMOTES)) {
161 				baseBranch = startPointFullName;
162 				if (exists)
163 					refLogMessage = "branch: Reset start-point to branch " //$NON-NLS-1$
164 							+ startPointFullName; // TODO
165 				else
166 					refLogMessage = "branch: Created from branch " + baseBranch; //$NON-NLS-1$
167 			} else {
168 				startAt = revWalk.peel(revWalk.parseAny(startAt));
169 				if (exists)
170 					refLogMessage = "branch: Reset start-point to tag " //$NON-NLS-1$
171 							+ startPointFullName;
172 				else
173 					refLogMessage = "branch: Created from tag " //$NON-NLS-1$
174 							+ startPointFullName;
175 			}
176 
177 			RefUpdate updateRef = repo.updateRef(R_HEADS + name);
178 			updateRef.setNewObjectId(startAt);
179 			updateRef.setRefLogMessage(refLogMessage, false);
180 			Result updateResult;
181 			if (exists && force)
182 				updateResult = updateRef.forceUpdate();
183 			else
184 				updateResult = updateRef.update();
185 
186 			setCallable(false);
187 
188 			boolean ok = false;
189 			switch (updateResult) {
190 			case NEW:
191 				ok = !exists;
192 				break;
193 			case NO_CHANGE:
194 			case FAST_FORWARD:
195 			case FORCED:
196 				ok = exists;
197 				break;
198 			default:
199 				break;
200 			}
201 
202 			if (!ok)
203 				throw new JGitInternalException(MessageFormat.format(JGitText
204 						.get().createBranchUnexpectedResult, updateResult
205 						.name()));
206 
207 			Ref result = repo.findRef(name);
208 			if (result == null)
209 				throw new JGitInternalException(
210 						JGitText.get().createBranchFailedUnknownReason);
211 
212 			if (baseBranch.length() == 0) {
213 				return result;
214 			}
215 
216 			// if we are based on another branch, see
217 			// if we need to configure upstream configuration: first check
218 			// whether the setting was done explicitly
219 			boolean doConfigure;
220 			if (upstreamMode == SetupUpstreamMode.SET_UPSTREAM
221 					|| upstreamMode == SetupUpstreamMode.TRACK)
222 				// explicitly set to configure
223 				doConfigure = true;
224 			else if (upstreamMode == SetupUpstreamMode.NOTRACK)
225 				// explicitly set to not configure
226 				doConfigure = false;
227 			else {
228 				// if there was no explicit setting, check the configuration
229 				String autosetupflag = repo.getConfig().getString(
230 						ConfigConstants.CONFIG_BRANCH_SECTION, null,
231 						ConfigConstants.CONFIG_KEY_AUTOSETUPMERGE);
232 				if ("false".equals(autosetupflag)) { //$NON-NLS-1$
233 					doConfigure = false;
234 				} else if ("always".equals(autosetupflag)) { //$NON-NLS-1$
235 					doConfigure = true;
236 				} else {
237 					// in this case, the default is to configure
238 					// only in case the base branch was a remote branch
239 					doConfigure = baseBranch.startsWith(Constants.R_REMOTES);
240 				}
241 			}
242 
243 			if (doConfigure) {
244 				StoredConfig config = repo.getConfig();
245 
246 				String remoteName = repo.getRemoteName(baseBranch);
247 				if (remoteName != null) {
248 					String branchName = repo
249 							.shortenRemoteBranchName(baseBranch);
250 					config
251 							.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
252 									name, ConfigConstants.CONFIG_KEY_REMOTE,
253 									remoteName);
254 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
255 							name, ConfigConstants.CONFIG_KEY_MERGE,
256 							Constants.R_HEADS + branchName);
257 				} else {
258 					// set "." as remote
259 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
260 							name, ConfigConstants.CONFIG_KEY_REMOTE, "."); //$NON-NLS-1$
261 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
262 							name, ConfigConstants.CONFIG_KEY_MERGE, baseBranch);
263 				}
264 				config.save();
265 			}
266 			return result;
267 		} catch (IOException ioe) {
268 			throw new JGitInternalException(ioe.getMessage(), ioe);
269 		}
270 	}
271 
272 	private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
273 			RefNotFoundException, IOException {
274 		if (startCommit != null)
275 			return startCommit.getId();
276 		String startPointOrHead = getStartPointOrHead();
277 		ObjectId result = repo.resolve(startPointOrHead);
278 		if (result == null)
279 			throw new RefNotFoundException(MessageFormat.format(
280 					JGitText.get().refNotResolved, startPointOrHead));
281 		return result;
282 	}
283 
284 	private String getStartPointOrHead() {
285 		return startPoint != null ? startPoint : HEAD;
286 	}
287 
288 	private void processOptions() throws InvalidRefNameException {
289 		if (name == null
290 				|| !Repository.isValidRefName(R_HEADS + name)
291 				|| !isValidBranchName(name))
292 			throw new InvalidRefNameException(MessageFormat.format(JGitText
293 					.get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
294 	}
295 
296 	/**
297 	 * Check if the given branch name is valid
298 	 *
299 	 * @param branchName
300 	 *            branch name to check
301 	 * @return {@code true} if the branch name is valid
302 	 *
303 	 * @since 5.0
304 	 */
305 	public static boolean isValidBranchName(String branchName) {
306 		if (HEAD.equals(branchName)) {
307 			return false;
308 		}
309 		return !branchName.startsWith("-"); //$NON-NLS-1$
310 	}
311 
312 	/**
313 	 * Set the name of the new branch
314 	 *
315 	 * @param name
316 	 *            the name of the new branch
317 	 * @return this instance
318 	 */
319 	public CreateBranchCommand setName(String name) {
320 		checkCallable();
321 		this.name = name;
322 		return this;
323 	}
324 
325 	/**
326 	 * Set whether to create the branch forcefully
327 	 *
328 	 * @param force
329 	 *            if <code>true</code> and the branch with the given name
330 	 *            already exists, the start-point of an existing branch will be
331 	 *            set to a new start-point; if false, the existing branch will
332 	 *            not be changed
333 	 * @return this instance
334 	 */
335 	public CreateBranchCommand setForce(boolean force) {
336 		checkCallable();
337 		this.force = force;
338 		return this;
339 	}
340 
341 	/**
342 	 * Set the start point
343 	 *
344 	 * @param startPoint
345 	 *            corresponds to the start-point option; if <code>null</code>,
346 	 *            the current HEAD will be used
347 	 * @return this instance
348 	 */
349 	public CreateBranchCommand setStartPoint(String startPoint) {
350 		checkCallable();
351 		this.startPoint = startPoint;
352 		this.startCommit = null;
353 		return this;
354 	}
355 
356 	/**
357 	 * Set the start point
358 	 *
359 	 * @param startPoint
360 	 *            corresponds to the start-point option; if <code>null</code>,
361 	 *            the current HEAD will be used
362 	 * @return this instance
363 	 */
364 	public CreateBranchCommand setStartPoint(RevCommit startPoint) {
365 		checkCallable();
366 		this.startCommit = startPoint;
367 		this.startPoint = null;
368 		return this;
369 	}
370 
371 	/**
372 	 * Set the upstream mode
373 	 *
374 	 * @param mode
375 	 *            corresponds to the --track/--no-track/--set-upstream options;
376 	 *            may be <code>null</code>
377 	 * @return this instance
378 	 */
379 	public CreateBranchCommand setUpstreamMode(SetupUpstreamMode mode) {
380 		checkCallable();
381 		this.upstreamMode = mode;
382 		return this;
383 	}
384 }