View Javadoc
1   /*
2    * Copyright (C) 2007-2008, Charles O'Farrell <charleso@charleso.org>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.pgm;
45  
46  import java.io.IOException;
47  import java.text.MessageFormat;
48  import java.util.Collection;
49  import java.util.LinkedHashMap;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.Map.Entry;
53  
54  import org.eclipse.jgit.api.Git;
55  import org.eclipse.jgit.api.ListBranchCommand;
56  import org.eclipse.jgit.api.ListBranchCommand.ListMode;
57  import org.eclipse.jgit.lib.Constants;
58  import org.eclipse.jgit.lib.ObjectId;
59  import org.eclipse.jgit.lib.ObjectReader;
60  import org.eclipse.jgit.lib.Ref;
61  import org.eclipse.jgit.lib.RefComparator;
62  import org.eclipse.jgit.lib.RefRename;
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.pgm.internal.CLIText;
67  import org.eclipse.jgit.pgm.opt.OptionWithValuesListHandler;
68  import org.eclipse.jgit.revwalk.RevWalk;
69  import org.kohsuke.args4j.Argument;
70  import org.kohsuke.args4j.Option;
71  
72  @Command(common = true, usage = "usage_listCreateOrDeleteBranches")
73  class Branch extends TextBuiltin {
74  
75  	private String otherBranch;
76  	private boolean createForce;
77  	private boolean rename;
78  
79  	@Option(name = "--remote", aliases = { "-r" }, usage = "usage_actOnRemoteTrackingBranches")
80  	private boolean remote = false;
81  
82  	@Option(name = "--all", aliases = { "-a" }, usage = "usage_listBothRemoteTrackingAndLocalBranches")
83  	private boolean all = false;
84  
85  	@Option(name = "--contains", metaVar = "metaVar_commitish", usage = "usage_printOnlyBranchesThatContainTheCommit")
86  	private String containsCommitish;
87  
88  	private List<String> delete;
89  
90  	@Option(name = "--delete", aliases = {
91  			"-d" }, metaVar = "metaVar_branchNames", usage = "usage_deleteFullyMergedBranch", handler = OptionWithValuesListHandler.class)
92  	public void delete(List<String> names) {
93  		if (names.isEmpty()) {
94  			throw die(CLIText.get().branchNameRequired);
95  		}
96  		delete = names;
97  	}
98  
99  	private List<String> deleteForce;
100 
101 	@Option(name = "--delete-force", aliases = {
102 			"-D" }, metaVar = "metaVar_branchNames", usage = "usage_deleteBranchEvenIfNotMerged", handler = OptionWithValuesListHandler.class)
103 	public void deleteForce(List<String> names) {
104 		if (names.isEmpty()) {
105 			throw die(CLIText.get().branchNameRequired);
106 		}
107 		deleteForce = names;
108 	}
109 
110 	@Option(name = "--create-force", aliases = {
111 			"-f" }, metaVar = "metaVar_branchAndStartPoint", usage = "usage_forceCreateBranchEvenExists", handler = OptionWithValuesListHandler.class)
112 	public void createForce(List<String> branchAndStartPoint) {
113 		createForce = true;
114 		if (branchAndStartPoint.isEmpty()) {
115 			throw die(CLIText.get().branchNameRequired);
116 		}
117 		if (branchAndStartPoint.size() > 2) {
118 			throw die(CLIText.get().tooManyRefsGiven);
119 		}
120 		if (branchAndStartPoint.size() == 1) {
121 			branch = branchAndStartPoint.get(0);
122 		} else {
123 			branch = branchAndStartPoint.get(0);
124 			otherBranch = branchAndStartPoint.get(1);
125 		}
126 	}
127 
128 	@Option(name = "--move", aliases = {
129 			"-m" }, metaVar = "metaVar_oldNewBranchNames", usage = "usage_moveRenameABranch", handler = OptionWithValuesListHandler.class)
130 	public void moveRename(List<String> currentAndNew) {
131 		rename = true;
132 		if (currentAndNew.isEmpty()) {
133 			throw die(CLIText.get().branchNameRequired);
134 		}
135 		if (currentAndNew.size() > 2) {
136 			throw die(CLIText.get().tooManyRefsGiven);
137 		}
138 		if (currentAndNew.size() == 1) {
139 			branch = currentAndNew.get(0);
140 		} else {
141 			branch = currentAndNew.get(0);
142 			otherBranch = currentAndNew.get(1);
143 		}
144 	}
145 
146 	@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
147 	private boolean verbose = false;
148 
149 	@Argument(metaVar = "metaVar_name")
150 	private String branch;
151 
152 	private final Map<String, Ref> printRefs = new LinkedHashMap<String, Ref>();
153 
154 	/** Only set for verbose branch listing at-the-moment */
155 	private RevWalk rw;
156 
157 	private int maxNameLength;
158 
159 	@Override
160 	protected void run() throws Exception {
161 		if (delete != null || deleteForce != null) {
162 			if (delete != null) {
163 				delete(delete, false);
164 			}
165 			if (deleteForce != null) {
166 				delete(deleteForce, true);
167 			}
168 		} else {
169 			if (rename) {
170 				String src, dst;
171 				if (otherBranch == null) {
172 					final Ref head = db.exactRef(Constants.HEAD);
173 					if (head != null && head.isSymbolic()) {
174 						src = head.getLeaf().getName();
175 					} else {
176 						throw die(CLIText.get().cannotRenameDetachedHEAD);
177 					}
178 					dst = branch;
179 				} else {
180 					src = branch;
181 					final Ref old = db.findRef(src);
182 					if (old == null)
183 						throw die(MessageFormat.format(CLIText.get().doesNotExist, src));
184 					if (!old.getName().startsWith(Constants.R_HEADS))
185 						throw die(MessageFormat.format(CLIText.get().notABranch, src));
186 					src = old.getName();
187 					dst = otherBranch;
188 				}
189 
190 				if (!dst.startsWith(Constants.R_HEADS))
191 					dst = Constants.R_HEADS + dst;
192 				if (!Repository.isValidRefName(dst))
193 					throw die(MessageFormat.format(CLIText.get().notAValidRefName, dst));
194 
195 				RefRename r = db.renameRef(src, dst);
196 				if (r.rename() != Result.RENAMED)
197 					throw die(MessageFormat.format(CLIText.get().cannotBeRenamed, src));
198 
199 			} else if (createForce || branch != null) {
200 				String newHead = branch;
201 				String startBranch;
202 				if (createForce) {
203 					startBranch = otherBranch;
204 				} else {
205 					startBranch = Constants.HEAD;
206 				}
207 				Ref startRef = db.findRef(startBranch);
208 				ObjectId startAt = db.resolve(startBranch + "^0"); //$NON-NLS-1$
209 				if (startRef != null) {
210 					startBranch = startRef.getName();
211 				} else if (startAt != null) {
212 					startBranch = startAt.name();
213 				} else {
214 					throw die(MessageFormat.format(
215 							CLIText.get().notAValidCommitName, startBranch));
216 				}
217 				startBranch = Repository.shortenRefName(startBranch);
218 				String newRefName = newHead;
219 				if (!newRefName.startsWith(Constants.R_HEADS)) {
220 					newRefName = Constants.R_HEADS + newRefName;
221 				}
222 				if (!Repository.isValidRefName(newRefName)) {
223 					throw die(MessageFormat.format(CLIText.get().notAValidRefName, newRefName));
224 				}
225 				if (!createForce && db.resolve(newRefName) != null) {
226 					throw die(MessageFormat.format(CLIText.get().branchAlreadyExists, newHead));
227 				}
228 				RefUpdate updateRef = db.updateRef(newRefName);
229 				updateRef.setNewObjectId(startAt);
230 				updateRef.setForceUpdate(createForce);
231 				updateRef.setRefLogMessage(MessageFormat.format(CLIText.get().branchCreatedFrom, startBranch), false);
232 				Result update = updateRef.update();
233 				if (update == Result.REJECTED) {
234 					throw die(MessageFormat.format(CLIText.get().couldNotCreateBranch, newHead, update.toString()));
235 				}
236 			} else {
237 				if (verbose) {
238 					rw = new RevWalk(db);
239 				}
240 				list();
241 			}
242 		}
243 	}
244 
245 	private void list() throws Exception {
246 		Ref head = db.exactRef(Constants.HEAD);
247 		// This can happen if HEAD is stillborn
248 		if (head != null) {
249 			String current = head.getLeaf().getName();
250 			try (Git git = new Git(db)) {
251 				ListBranchCommand command = git.branchList();
252 				if (all)
253 					command.setListMode(ListMode.ALL);
254 				else if (remote)
255 					command.setListMode(ListMode.REMOTE);
256 
257 				if (containsCommitish != null)
258 					command.setContains(containsCommitish);
259 
260 				List<Ref> refs = command.call();
261 				for (Ref ref : refs) {
262 					if (ref.getName().equals(Constants.HEAD))
263 						addRef("(no branch)", head); //$NON-NLS-1$
264 				}
265 
266 				addRefs(refs, Constants.R_HEADS);
267 				addRefs(refs, Constants.R_REMOTES);
268 
269 				try (ObjectReader reader = db.newObjectReader()) {
270 					for (final Entry<String, Ref> e : printRefs.entrySet()) {
271 						final Ref ref = e.getValue();
272 						printHead(reader, e.getKey(),
273 								current.equals(ref.getName()), ref);
274 					}
275 				}
276 			}
277 		}
278 	}
279 
280 	private void addRefs(final Collection<Ref> refs, final String prefix) {
281 		for (final Ref ref : RefComparator.sort(refs)) {
282 			final String name = ref.getName();
283 			if (name.startsWith(prefix))
284 				addRef(name.substring(name.indexOf('/', 5) + 1), ref);
285 		}
286 	}
287 
288 	private void addRef(final String name, final Ref ref) {
289 		printRefs.put(name, ref);
290 		maxNameLength = Math.max(maxNameLength, name.length());
291 	}
292 
293 	private void printHead(final ObjectReader reader, final String ref,
294 			final boolean isCurrent, final Ref refObj) throws Exception {
295 		outw.print(isCurrent ? '*' : ' ');
296 		outw.print(' ');
297 		outw.print(ref);
298 		if (verbose) {
299 			final int spaces = maxNameLength - ref.length() + 1;
300 			outw.format("%" + spaces + "s", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
301 			final ObjectId objectId = refObj.getObjectId();
302 			outw.print(reader.abbreviate(objectId).name());
303 			outw.print(' ');
304 			outw.print(rw.parseCommit(objectId).getShortMessage());
305 		}
306 		outw.println();
307 	}
308 
309 	private void delete(List<String> branches, boolean force)
310 			throws IOException {
311 		String current = db.getBranch();
312 		ObjectId head = db.resolve(Constants.HEAD);
313 		for (String b : branches) {
314 			if (b.equals(current)) {
315 				throw die(MessageFormat.format(CLIText.get().cannotDeleteTheBranchWhichYouAreCurrentlyOn, b));
316 			}
317 			RefUpdate update = db.updateRef((remote ? Constants.R_REMOTES
318 					: Constants.R_HEADS)
319 					+ b);
320 			update.setNewObjectId(head);
321 			update.setForceUpdate(force || remote);
322 			Result result = update.delete();
323 			if (result == Result.REJECTED) {
324 				throw die(MessageFormat.format(CLIText.get().branchIsNotAnAncestorOfYourCurrentHEAD, b));
325 			} else if (result == Result.NEW)
326 				throw die(MessageFormat.format(CLIText.get().branchNotFound, b));
327 			if (remote)
328 				outw.println(MessageFormat.format(CLIText.get().deletedRemoteBranch, b));
329 			else if (verbose)
330 				outw.println(MessageFormat.format(CLIText.get().deletedBranch, b));
331 		}
332 	}
333 }