View Javadoc
1   /*
2    * Copyright (C) 2007-2008, Charles O'Farrell <charleso@charleso.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.pgm;
12  
13  import java.io.IOException;
14  import java.text.MessageFormat;
15  import java.util.Collection;
16  import java.util.LinkedHashMap;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Map.Entry;
20  
21  import org.eclipse.jgit.api.Git;
22  import org.eclipse.jgit.api.ListBranchCommand;
23  import org.eclipse.jgit.api.ListBranchCommand.ListMode;
24  import org.eclipse.jgit.api.errors.GitAPIException;
25  import org.eclipse.jgit.lib.Constants;
26  import org.eclipse.jgit.lib.ObjectId;
27  import org.eclipse.jgit.lib.ObjectReader;
28  import org.eclipse.jgit.lib.Ref;
29  import org.eclipse.jgit.lib.RefComparator;
30  import org.eclipse.jgit.lib.RefRename;
31  import org.eclipse.jgit.lib.RefUpdate;
32  import org.eclipse.jgit.lib.RefUpdate.Result;
33  import org.eclipse.jgit.lib.Repository;
34  import org.eclipse.jgit.pgm.internal.CLIText;
35  import org.eclipse.jgit.pgm.opt.OptionWithValuesListHandler;
36  import org.eclipse.jgit.revwalk.RevWalk;
37  import org.kohsuke.args4j.Argument;
38  import org.kohsuke.args4j.Option;
39  
40  @Command(common = true, usage = "usage_listCreateOrDeleteBranches")
41  class Branch extends TextBuiltin {
42  
43  	private String otherBranch;
44  	private boolean createForce;
45  	private boolean rename;
46  
47  	@Option(name = "--remote", aliases = { "-r" }, usage = "usage_actOnRemoteTrackingBranches")
48  	private boolean remote = false;
49  
50  	@Option(name = "--all", aliases = { "-a" }, usage = "usage_listBothRemoteTrackingAndLocalBranches")
51  	private boolean all = false;
52  
53  	@Option(name = "--contains", metaVar = "metaVar_commitish", usage = "usage_printOnlyBranchesThatContainTheCommit")
54  	private String containsCommitish;
55  
56  	private List<String> delete;
57  
58  	/**
59  	 * Delete branches
60  	 *
61  	 * @param names
62  	 *            a {@link java.util.List} of branch names.
63  	 */
64  	@Option(name = "--delete", aliases = {
65  			"-d" }, metaVar = "metaVar_branchNames", usage = "usage_deleteFullyMergedBranch", handler = OptionWithValuesListHandler.class)
66  	public void delete(List<String> names) {
67  		if (names.isEmpty()) {
68  			throw die(CLIText.get().branchNameRequired);
69  		}
70  		delete = names;
71  	}
72  
73  	private List<String> deleteForce;
74  
75  	/**
76  	 * Forcefully delete branches
77  	 *
78  	 * @param names
79  	 *            a {@link java.util.List} of branch names.
80  	 */
81  	@Option(name = "--delete-force", aliases = {
82  			"-D" }, metaVar = "metaVar_branchNames", usage = "usage_deleteBranchEvenIfNotMerged", handler = OptionWithValuesListHandler.class)
83  	public void deleteForce(List<String> names) {
84  		if (names.isEmpty()) {
85  			throw die(CLIText.get().branchNameRequired);
86  		}
87  		deleteForce = names;
88  	}
89  
90  	/**
91  	 * Forcefully create a list of branches
92  	 *
93  	 * @param branchAndStartPoint
94  	 *            a branch name and a start point
95  	 */
96  	@Option(name = "--create-force", aliases = {
97  			"-f" }, metaVar = "metaVar_branchAndStartPoint", usage = "usage_forceCreateBranchEvenExists", handler = OptionWithValuesListHandler.class)
98  	public void createForce(List<String> branchAndStartPoint) {
99  		createForce = true;
100 		if (branchAndStartPoint.isEmpty()) {
101 			throw die(CLIText.get().branchNameRequired);
102 		}
103 		if (branchAndStartPoint.size() > 2) {
104 			throw die(CLIText.get().tooManyRefsGiven);
105 		}
106 		if (branchAndStartPoint.size() == 1) {
107 			branch = branchAndStartPoint.get(0);
108 		} else {
109 			branch = branchAndStartPoint.get(0);
110 			otherBranch = branchAndStartPoint.get(1);
111 		}
112 	}
113 
114 	/**
115 	 * Move or rename a branch
116 	 *
117 	 * @param currentAndNew
118 	 *            the current and the new branch name
119 	 */
120 	@Option(name = "--move", aliases = {
121 			"-m" }, metaVar = "metaVar_oldNewBranchNames", usage = "usage_moveRenameABranch", handler = OptionWithValuesListHandler.class)
122 	public void moveRename(List<String> currentAndNew) {
123 		rename = true;
124 		if (currentAndNew.isEmpty()) {
125 			throw die(CLIText.get().branchNameRequired);
126 		}
127 		if (currentAndNew.size() > 2) {
128 			throw die(CLIText.get().tooManyRefsGiven);
129 		}
130 		if (currentAndNew.size() == 1) {
131 			branch = currentAndNew.get(0);
132 		} else {
133 			branch = currentAndNew.get(0);
134 			otherBranch = currentAndNew.get(1);
135 		}
136 	}
137 
138 	@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
139 	private boolean verbose = false;
140 
141 	@Argument(metaVar = "metaVar_name")
142 	private String branch;
143 
144 	private final Map<String, Ref> printRefs = new LinkedHashMap<>();
145 
146 	/** Only set for verbose branch listing at-the-moment */
147 	private RevWalk rw;
148 
149 	private int maxNameLength;
150 
151 	/** {@inheritDoc} */
152 	@Override
153 	protected void run() {
154 		try {
155 			if (delete != null || deleteForce != null) {
156 				if (delete != null) {
157 					delete(delete, false);
158 				}
159 				if (deleteForce != null) {
160 					delete(deleteForce, true);
161 				}
162 				return;
163 			}
164 			if (rename) {
165 				String src, dst;
166 				if (otherBranch == null) {
167 					final Ref head = db.exactRef(Constants.HEAD);
168 					if (head != null && head.isSymbolic()) {
169 						src = head.getLeaf().getName();
170 					} else {
171 						throw die(CLIText.get().cannotRenameDetachedHEAD);
172 					}
173 					dst = branch;
174 				} else {
175 					src = branch;
176 					final Ref old = db.findRef(src);
177 					if (old == null) {
178 						throw die(MessageFormat.format(CLIText.get().doesNotExist, src));
179 					}
180 					if (!old.getName().startsWith(Constants.R_HEADS)) {
181 						throw die(MessageFormat.format(CLIText.get().notABranch, src));
182 					}
183 					src = old.getName();
184 					dst = otherBranch;
185 				}
186 
187 				if (!dst.startsWith(Constants.R_HEADS)) {
188 					dst = Constants.R_HEADS + dst;
189 				}
190 				if (!Repository.isValidRefName(dst)) {
191 					throw die(MessageFormat.format(CLIText.get().notAValidRefName, dst));
192 				}
193 
194 				RefRename r = db.renameRef(src, dst);
195 				if (r.rename() != Result.RENAMED) {
196 					throw die(MessageFormat.format(CLIText.get().cannotBeRenamed, src));
197 				}
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 		} catch (IOException | GitAPIException e) {
243 			throw die(e.getMessage(), e);
244 		}
245 	}
246 
247 	private void list() throws IOException, GitAPIException {
248 		Ref head = db.exactRef(Constants.HEAD);
249 		// This can happen if HEAD is stillborn
250 		if (head != null) {
251 			String current = head.getLeaf().getName();
252 			try (Gitit.html#Git">Git git = new Git(db)) {
253 				ListBranchCommand command = git.branchList();
254 				if (all)
255 					command.setListMode(ListMode.ALL);
256 				else if (remote)
257 					command.setListMode(ListMode.REMOTE);
258 
259 				if (containsCommitish != null)
260 					command.setContains(containsCommitish);
261 
262 				List<Ref> refs = command.call();
263 				for (Ref ref : refs) {
264 					if (ref.getName().equals(Constants.HEAD))
265 						addRef("(no branch)", head); //$NON-NLS-1$
266 				}
267 
268 				addRefs(refs, Constants.R_HEADS);
269 				addRefs(refs, Constants.R_REMOTES);
270 
271 				try (ObjectReader reader = db.newObjectReader()) {
272 					for (Entry<String, Ref> e : printRefs.entrySet()) {
273 						final Ref ref = e.getValue();
274 						printHead(reader, e.getKey(),
275 								current.equals(ref.getName()), ref);
276 					}
277 				}
278 			}
279 		}
280 	}
281 
282 	private void addRefs(Collection<Ref> refs, String prefix) {
283 		for (Ref ref : RefComparator.sort(refs)) {
284 			final String name = ref.getName();
285 			if (name.startsWith(prefix))
286 				addRef(name.substring(name.indexOf('/', 5) + 1), ref);
287 		}
288 	}
289 
290 	private void addRef(String name, Ref ref) {
291 		printRefs.put(name, ref);
292 		maxNameLength = Math.max(maxNameLength, name.length());
293 	}
294 
295 	private void printHead(final ObjectReader reader, final String ref,
296 			final boolean isCurrent, final Ref refObj) throws IOException {
297 		outw.print(isCurrent ? '*' : ' ');
298 		outw.print(' ');
299 		outw.print(ref);
300 		if (verbose) {
301 			final int spaces = maxNameLength - ref.length() + 1;
302 			outw.format("%" + spaces + "s", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
303 			final ObjectId objectId = refObj.getObjectId();
304 			outw.print(reader.abbreviate(objectId).name());
305 			outw.print(' ');
306 			outw.print(rw.parseCommit(objectId).getShortMessage());
307 		}
308 		outw.println();
309 	}
310 
311 	private void delete(List<String> branches, boolean force)
312 			throws IOException {
313 		String current = db.getBranch();
314 		ObjectId head = db.resolve(Constants.HEAD);
315 		for (String b : branches) {
316 			if (b.equals(current)) {
317 				throw die(MessageFormat.format(CLIText.get().cannotDeleteTheBranchWhichYouAreCurrentlyOn, b));
318 			}
319 			RefUpdate update = db.updateRef((remote ? Constants.R_REMOTES
320 					: Constants.R_HEADS)
321 					+ b);
322 			update.setNewObjectId(head);
323 			update.setForceUpdate(force || remote);
324 			Result result = update.delete();
325 			if (result == Result.REJECTED) {
326 				throw die(MessageFormat.format(CLIText.get().branchIsNotAnAncestorOfYourCurrentHEAD, b));
327 			} else if (result == Result.NEW)
328 				throw die(MessageFormat.format(CLIText.get().branchNotFound, b));
329 			if (remote)
330 				outw.println(MessageFormat.format(CLIText.get().deletedRemoteBranch, b));
331 			else if (verbose)
332 				outw.println(MessageFormat.format(CLIText.get().deletedBranch, b));
333 		}
334 	}
335 }