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 	public Ref call() throws GitAPIException, RefAlreadyExistsException,
124 			RefNotFoundException, InvalidRefNameException {
125 		checkCallable();
126 		processOptions();
127 		try (RevWalk revWalk = new RevWalk(repo)) {
128 			Ref refToCheck = repo.getRef(name);
129 			boolean exists = refToCheck != null
130 					&& refToCheck.getName().startsWith(Constants.R_HEADS);
131 			if (!force && exists)
132 				throw new RefAlreadyExistsException(MessageFormat.format(
133 						JGitText.get().refAlreadyExists1, name));
134 
135 			ObjectId startAt = getStartPointObjectId();
136 			String startPointFullName = null;
137 			if (startPoint != null) {
138 				Ref baseRef = repo.getRef(startPoint);
139 				if (baseRef != null)
140 					startPointFullName = baseRef.getName();
141 			}
142 
143 			// determine whether we are based on a commit,
144 			// a branch, or a tag and compose the reflog message
145 			String refLogMessage;
146 			String baseBranch = ""; //$NON-NLS-1$
147 			if (startPointFullName == null) {
148 				String baseCommit;
149 				if (startCommit != null)
150 					baseCommit = startCommit.getShortMessage();
151 				else {
152 					RevCommit commit = revWalk.parseCommit(repo
153 							.resolve(getStartPointOrHead()));
154 					baseCommit = commit.getShortMessage();
155 				}
156 				if (exists)
157 					refLogMessage = "branch: Reset start-point to commit " //$NON-NLS-1$
158 							+ baseCommit;
159 				else
160 					refLogMessage = "branch: Created from commit " + baseCommit; //$NON-NLS-1$
161 
162 			} else if (startPointFullName.startsWith(Constants.R_HEADS)
163 					|| startPointFullName.startsWith(Constants.R_REMOTES)) {
164 				baseBranch = startPointFullName;
165 				if (exists)
166 					refLogMessage = "branch: Reset start-point to branch " //$NON-NLS-1$
167 							+ startPointFullName; // TODO
168 				else
169 					refLogMessage = "branch: Created from branch " + baseBranch; //$NON-NLS-1$
170 			} else {
171 				startAt = revWalk.peel(revWalk.parseAny(startAt));
172 				if (exists)
173 					refLogMessage = "branch: Reset start-point to tag " //$NON-NLS-1$
174 							+ startPointFullName;
175 				else
176 					refLogMessage = "branch: Created from tag " //$NON-NLS-1$
177 							+ startPointFullName;
178 			}
179 
180 			RefUpdate updateRef = repo.updateRef(Constants.R_HEADS + name);
181 			updateRef.setNewObjectId(startAt);
182 			updateRef.setRefLogMessage(refLogMessage, false);
183 			Result updateResult;
184 			if (exists && force)
185 				updateResult = updateRef.forceUpdate();
186 			else
187 				updateResult = updateRef.update();
188 
189 			setCallable(false);
190 
191 			boolean ok = false;
192 			switch (updateResult) {
193 			case NEW:
194 				ok = !exists;
195 				break;
196 			case NO_CHANGE:
197 			case FAST_FORWARD:
198 			case FORCED:
199 				ok = exists;
200 				break;
201 			default:
202 				break;
203 			}
204 
205 			if (!ok)
206 				throw new JGitInternalException(MessageFormat.format(JGitText
207 						.get().createBranchUnexpectedResult, updateResult
208 						.name()));
209 
210 			Ref result = repo.getRef(name);
211 			if (result == null)
212 				throw new JGitInternalException(
213 						JGitText.get().createBranchFailedUnknownReason);
214 
215 			if (baseBranch.length() == 0) {
216 				return result;
217 			}
218 
219 			// if we are based on another branch, see
220 			// if we need to configure upstream configuration: first check
221 			// whether the setting was done explicitly
222 			boolean doConfigure;
223 			if (upstreamMode == SetupUpstreamMode.SET_UPSTREAM
224 					|| upstreamMode == SetupUpstreamMode.TRACK)
225 				// explicitly set to configure
226 				doConfigure = true;
227 			else if (upstreamMode == SetupUpstreamMode.NOTRACK)
228 				// explicitly set to not configure
229 				doConfigure = false;
230 			else {
231 				// if there was no explicit setting, check the configuration
232 				String autosetupflag = repo.getConfig().getString(
233 						ConfigConstants.CONFIG_BRANCH_SECTION, null,
234 						ConfigConstants.CONFIG_KEY_AUTOSETUPMERGE);
235 				if ("false".equals(autosetupflag)) { //$NON-NLS-1$
236 					doConfigure = false;
237 				} else if ("always".equals(autosetupflag)) { //$NON-NLS-1$
238 					doConfigure = true;
239 				} else {
240 					// in this case, the default is to configure
241 					// only in case the base branch was a remote branch
242 					doConfigure = baseBranch.startsWith(Constants.R_REMOTES);
243 				}
244 			}
245 
246 			if (doConfigure) {
247 				StoredConfig config = repo.getConfig();
248 
249 				String remoteName = repo.getRemoteName(baseBranch);
250 				if (remoteName != null) {
251 					String branchName = repo
252 							.shortenRemoteBranchName(baseBranch);
253 					config
254 							.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
255 									name, ConfigConstants.CONFIG_KEY_REMOTE,
256 									remoteName);
257 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
258 							name, ConfigConstants.CONFIG_KEY_MERGE,
259 							Constants.R_HEADS + branchName);
260 				} else {
261 					// set "." as remote
262 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
263 							name, ConfigConstants.CONFIG_KEY_REMOTE, "."); //$NON-NLS-1$
264 					config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
265 							name, ConfigConstants.CONFIG_KEY_MERGE, baseBranch);
266 				}
267 				config.save();
268 			}
269 			return result;
270 		} catch (IOException ioe) {
271 			throw new JGitInternalException(ioe.getMessage(), ioe);
272 		}
273 	}
274 
275 	private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
276 			RefNotFoundException, IOException {
277 		if (startCommit != null)
278 			return startCommit.getId();
279 		String startPointOrHead = getStartPointOrHead();
280 		ObjectId result = repo.resolve(startPointOrHead);
281 		if (result == null)
282 			throw new RefNotFoundException(MessageFormat.format(
283 					JGitText.get().refNotResolved, startPointOrHead));
284 		return result;
285 	}
286 
287 	private String getStartPointOrHead() {
288 		return startPoint != null ? startPoint : Constants.HEAD;
289 	}
290 
291 	private void processOptions() throws InvalidRefNameException {
292 		if (name == null
293 				|| !Repository.isValidRefName(Constants.R_HEADS + name))
294 			throw new InvalidRefNameException(MessageFormat.format(JGitText
295 					.get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
296 	}
297 
298 	/**
299 	 * @param name
300 	 *            the name of the new branch
301 	 * @return this instance
302 	 */
303 	public CreateBranchCommand setName(String name) {
304 		checkCallable();
305 		this.name = name;
306 		return this;
307 	}
308 
309 	/**
310 	 * @param force
311 	 *            if <code>true</code> and the branch with the given name
312 	 *            already exists, the start-point of an existing branch will be
313 	 *            set to a new start-point; if false, the existing branch will
314 	 *            not be changed
315 	 * @return this instance
316 	 */
317 	public CreateBranchCommand setForce(boolean force) {
318 		checkCallable();
319 		this.force = force;
320 		return this;
321 	}
322 
323 	/**
324 	 * @param startPoint
325 	 *            corresponds to the start-point option; if <code>null</code>,
326 	 *            the current HEAD will be used
327 	 * @return this instance
328 	 */
329 	public CreateBranchCommand setStartPoint(String startPoint) {
330 		checkCallable();
331 		this.startPoint = startPoint;
332 		this.startCommit = null;
333 		return this;
334 	}
335 
336 	/**
337 	 * @param startPoint
338 	 *            corresponds to the start-point option; if <code>null</code>,
339 	 *            the current HEAD will be used
340 	 * @return this instance
341 	 */
342 	public CreateBranchCommand setStartPoint(RevCommit startPoint) {
343 		checkCallable();
344 		this.startCommit = startPoint;
345 		this.startPoint = null;
346 		return this;
347 	}
348 
349 	/**
350 	 * @param mode
351 	 *            corresponds to the --track/--no-track/--set-upstream options;
352 	 *            may be <code>null</code>
353 	 * @return this instance
354 	 */
355 	public CreateBranchCommand setUpstreamMode(SetupUpstreamMode mode) {
356 		checkCallable();
357 		this.upstreamMode = mode;
358 		return this;
359 	}
360 }