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 java.io.IOException;
47  import java.text.MessageFormat;
48  
49  import org.eclipse.jgit.api.errors.GitAPIException;
50  import org.eclipse.jgit.api.errors.InvalidRefNameException;
51  import org.eclipse.jgit.api.errors.JGitInternalException;
52  import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
53  import org.eclipse.jgit.api.errors.RefNotFoundException;
54  import org.eclipse.jgit.errors.AmbiguousObjectException;
55  import org.eclipse.jgit.internal.JGitText;
56  import org.eclipse.jgit.lib.ConfigConstants;
57  import org.eclipse.jgit.lib.Constants;
58  import org.eclipse.jgit.lib.ObjectId;
59  import org.eclipse.jgit.lib.Ref;
60  import org.eclipse.jgit.lib.RefUpdate;
61  import org.eclipse.jgit.lib.RefUpdate.Result;
62  import org.eclipse.jgit.lib.Repository;
63  import org.eclipse.jgit.lib.StoredConfig;
64  import org.eclipse.jgit.revwalk.RevCommit;
65  import org.eclipse.jgit.revwalk.RevWalk;
66  
67  /**
68   * Used to create a local branch.
69   *
70   * @see <a
71   *      href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
72   *      >Git documentation about Branch</a>
73   */
74  public class CreateBranchCommand extends GitCommand<Ref> {
75  	private String name;
76  
77  	private boolean force = false;
78  
79  	private SetupUpstreamMode upstreamMode;
80  
81  	private String startPoint = Constants.HEAD;
82  
83  	private RevCommit startCommit;
84  
85  	/**
86  	 * The modes available for setting up the upstream configuration
87  	 * (corresponding to the --set-upstream, --track, --no-track options
88  	 *
89  	 */
90  	public enum SetupUpstreamMode {
91  		/**
92  		 * Corresponds to the --track option
93  		 */
94  		TRACK,
95  		/**
96  		 * Corresponds to the --no-track option
97  		 */
98  		NOTRACK,
99  		/**
100 		 * Corresponds to the --set-upstream option
101 		 */
102 		SET_UPSTREAM;
103 	}
104 
105 	/**
106 	 * @param repo
107 	 */
108 	protected CreateBranchCommand(Repository repo) {
109 		super(repo);
110 	}
111 
112 	/**
113 	 * @throws RefAlreadyExistsException
114 	 *             when trying to create (without force) a branch with a name
115 	 *             that already exists
116 	 * @throws RefNotFoundException
117 	 *             if the start point can not be found
118 	 * @throws InvalidRefNameException
119 	 *             if the provided name is <code>null</code> or otherwise
120 	 *             invalid
121 	 * @return the newly created branch
122 	 */
123 	@Override
124 	public Ref call() throws GitAPIException, RefAlreadyExistsException,
125 			RefNotFoundException, InvalidRefNameException {
126 		checkCallable();
127 		processOptions();
128 		try (RevWalk revWalk = new RevWalk(repo)) {
129 			Ref refToCheck = repo.findRef(name);
130 			boolean exists = refToCheck != null
131 					&& refToCheck.getName().startsWith(Constants.R_HEADS);
132 			if (!force && exists)
133 				throw new RefAlreadyExistsException(MessageFormat.format(
134 						JGitText.get().refAlreadyExists1, name));
135 
136 			ObjectId startAt = getStartPointObjectId();
137 			String startPointFullName = null;
138 			if (startPoint != null) {
139 				Ref baseRef = repo.findRef(startPoint);
140 				if (baseRef != null)
141 					startPointFullName = baseRef.getName();
142 			}
143 
144 			// determine whether we are based on a commit,
145 			// a branch, or a tag and compose the reflog message
146 			String refLogMessage;
147 			String baseBranch = ""; //$NON-NLS-1$
148 			if (startPointFullName == null) {
149 				String baseCommit;
150 				if (startCommit != null)
151 					baseCommit = startCommit.getShortMessage();
152 				else {
153 					RevCommit commit = revWalk.parseCommit(repo
154 							.resolve(getStartPointOrHead()));
155 					baseCommit = commit.getShortMessage();
156 				}
157 				if (exists)
158 					refLogMessage = "branch: Reset start-point to commit " //$NON-NLS-1$
159 							+ baseCommit;
160 				else
161 					refLogMessage = "branch: Created from commit " + baseCommit; //$NON-NLS-1$
162 
163 			} else if (startPointFullName.startsWith(Constants.R_HEADS)
164 					|| startPointFullName.startsWith(Constants.R_REMOTES)) {
165 				baseBranch = startPointFullName;
166 				if (exists)
167 					refLogMessage = "branch: Reset start-point to branch " //$NON-NLS-1$
168 							+ startPointFullName; // TODO
169 				else
170 					refLogMessage = "branch: Created from branch " + baseBranch; //$NON-NLS-1$
171 			} else {
172 				startAt = revWalk.peel(revWalk.parseAny(startAt));
173 				if (exists)
174 					refLogMessage = "branch: Reset start-point to tag " //$NON-NLS-1$
175 							+ startPointFullName;
176 				else
177 					refLogMessage = "branch: Created from tag " //$NON-NLS-1$
178 							+ startPointFullName;
179 			}
180 
181 			RefUpdate updateRef = repo.updateRef(Constants.R_HEADS + name);
182 			updateRef.setNewObjectId(startAt);
183 			updateRef.setRefLogMessage(refLogMessage, false);
184 			Result updateResult;
185 			if (exists && force)
186 				updateResult = updateRef.forceUpdate();
187 			else
188 				updateResult = updateRef.update();
189 
190 			setCallable(false);
191 
192 			boolean ok = false;
193 			switch (updateResult) {
194 			case NEW:
195 				ok = !exists;
196 				break;
197 			case NO_CHANGE:
198 			case FAST_FORWARD:
199 			case FORCED:
200 				ok = exists;
201 				break;
202 			default:
203 				break;
204 			}
205 
206 			if (!ok)
207 				throw new JGitInternalException(MessageFormat.format(JGitText
208 						.get().createBranchUnexpectedResult, updateResult
209 						.name()));
210 
211 			Ref result = repo.findRef(name);
212 			if (result == null)
213 				throw new JGitInternalException(
214 						JGitText.get().createBranchFailedUnknownReason);
215 
216 			if (baseBranch.length() == 0) {
217 				return result;
218 			}
219 
220 			// if we are based on another branch, see
221 			// if we need to configure upstream configuration: first check
222 			// whether the setting was done explicitly
223 			boolean doConfigure;
224 			if (upstreamMode == SetupUpstreamMode.SET_UPSTREAM
225 					|| upstreamMode == SetupUpstreamMode.TRACK)
226 				// explicitly set to configure
227 				doConfigure = true;
228 			else if (upstreamMode == SetupUpstreamMode.NOTRACK)
229 				// explicitly set to not configure
230 				doConfigure = false;
231 			else {
232 				// if there was no explicit setting, check the configuration
233 				String autosetupflag = repo.getConfig().getString(
234 						ConfigConstants.CONFIG_BRANCH_SECTION, null,
235 						ConfigConstants.CONFIG_KEY_AUTOSETUPMERGE);
236 				if ("false".equals(autosetupflag)) { //$NON-NLS-1$
237 					doConfigure = false;
238 				} else if ("always".equals(autosetupflag)) { //$NON-NLS-1$
239 					doConfigure = true;
240 				} else {
241 					// in this case, the default is to configure
242 					// only in case the base branch was a remote branch
243 					doConfigure = baseBranch.startsWith(Constants.R_REMOTES);
244 				}
245 			}
246 
247 			if (doConfigure) {
248 				StoredConfig config = repo.getConfig();
249 
250 				String remoteName = repo.getRemoteName(baseBranch);
251 				if (remoteName != null) {
252 					String branchName = repo
253 							.shortenRemoteBranchName(baseBranch);
254 					config
255 							.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
256 									name, ConfigConstants.CONFIG_KEY_REMOTE,
257 									remoteName);
258 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
259 							name, ConfigConstants.CONFIG_KEY_MERGE,
260 							Constants.R_HEADS + branchName);
261 				} else {
262 					// set "." as remote
263 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
264 							name, ConfigConstants.CONFIG_KEY_REMOTE, "."); //$NON-NLS-1$
265 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
266 							name, ConfigConstants.CONFIG_KEY_MERGE, baseBranch);
267 				}
268 				config.save();
269 			}
270 			return result;
271 		} catch (IOException ioe) {
272 			throw new JGitInternalException(ioe.getMessage(), ioe);
273 		}
274 	}
275 
276 	private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
277 			RefNotFoundException, IOException {
278 		if (startCommit != null)
279 			return startCommit.getId();
280 		String startPointOrHead = getStartPointOrHead();
281 		ObjectId result = repo.resolve(startPointOrHead);
282 		if (result == null)
283 			throw new RefNotFoundException(MessageFormat.format(
284 					JGitText.get().refNotResolved, startPointOrHead));
285 		return result;
286 	}
287 
288 	private String getStartPointOrHead() {
289 		return startPoint != null ? startPoint : Constants.HEAD;
290 	}
291 
292 	private void processOptions() throws InvalidRefNameException {
293 		if (name == null
294 				|| !Repository.isValidRefName(Constants.R_HEADS + name))
295 			throw new InvalidRefNameException(MessageFormat.format(JGitText
296 					.get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
297 	}
298 
299 	/**
300 	 * @param name
301 	 *            the name of the new branch
302 	 * @return this instance
303 	 */
304 	public CreateBranchCommand setName(String name) {
305 		checkCallable();
306 		this.name = name;
307 		return this;
308 	}
309 
310 	/**
311 	 * @param force
312 	 *            if <code>true</code> and the branch with the given name
313 	 *            already exists, the start-point of an existing branch will be
314 	 *            set to a new start-point; if false, the existing branch will
315 	 *            not be changed
316 	 * @return this instance
317 	 */
318 	public CreateBranchCommand setForce(boolean force) {
319 		checkCallable();
320 		this.force = force;
321 		return this;
322 	}
323 
324 	/**
325 	 * @param startPoint
326 	 *            corresponds to the start-point option; if <code>null</code>,
327 	 *            the current HEAD will be used
328 	 * @return this instance
329 	 */
330 	public CreateBranchCommand setStartPoint(String startPoint) {
331 		checkCallable();
332 		this.startPoint = startPoint;
333 		this.startCommit = null;
334 		return this;
335 	}
336 
337 	/**
338 	 * @param startPoint
339 	 *            corresponds to the start-point option; if <code>null</code>,
340 	 *            the current HEAD will be used
341 	 * @return this instance
342 	 */
343 	public CreateBranchCommand setStartPoint(RevCommit startPoint) {
344 		checkCallable();
345 		this.startCommit = startPoint;
346 		this.startPoint = null;
347 		return this;
348 	}
349 
350 	/**
351 	 * @param mode
352 	 *            corresponds to the --track/--no-track/--set-upstream options;
353 	 *            may be <code>null</code>
354 	 * @return this instance
355 	 */
356 	public CreateBranchCommand setUpstreamMode(SetupUpstreamMode mode) {
357 		checkCallable();
358 		this.upstreamMode = mode;
359 		return this;
360 	}
361 }