1 /*
2 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
3 * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
4 * Copyright (C) 2014, Robin Stocker <robin@nibor.org>
5 * and other copyright owners as documented in the project's IP log.
6 *
7 * This program and the accompanying materials are made available
8 * under the terms of the Eclipse Distribution License v1.0 which
9 * accompanies this distribution, is reproduced below, and is
10 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * - Neither the name of the Eclipse Foundation, Inc. nor the
27 * names of its contributors may be used to endorse or promote
28 * products derived from this software without specific prior
29 * written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45 package org.eclipse.jgit.api;
46
47 import java.io.IOException;
48 import java.text.MessageFormat;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.Comparator;
53 import java.util.List;
54
55 import org.eclipse.jgit.api.errors.GitAPIException;
56 import org.eclipse.jgit.api.errors.JGitInternalException;
57 import org.eclipse.jgit.api.errors.RefNotFoundException;
58 import org.eclipse.jgit.internal.JGitText;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.lib.ObjectId;
61 import org.eclipse.jgit.lib.Ref;
62 import org.eclipse.jgit.lib.Repository;
63 import org.eclipse.jgit.revwalk.RevCommit;
64 import org.eclipse.jgit.revwalk.RevWalk;
65 import org.eclipse.jgit.revwalk.RevWalkUtils;
66
67 /**
68 * Used to obtain a list of branches.
69 * <p>
70 * In case HEAD is detached (it points directly to a commit), it is also
71 * returned in the results.
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 ListBranchCommand extends GitCommand<List<Ref>> {
78 private ListMode listMode;
79
80 private String containsCommitish;
81
82 /**
83 * The modes available for listing branches (corresponding to the -r and -a
84 * options)
85 */
86 public enum ListMode {
87 /**
88 * Corresponds to the -a option (all branches)
89 */
90 ALL,
91 /**
92 * Corresponds to the -r option (remote branches only)
93 */
94 REMOTE;
95 }
96
97 /**
98 * Constructor for ListBranchCommand.
99 *
100 * @param repo
101 * a {@link org.eclipse.jgit.lib.Repository} object.
102 */
103 protected ListBranchCommand(Repository repo) {
104 super(repo);
105 }
106
107 /** {@inheritDoc} */
108 @Override
109 public List<Ref> call() throws GitAPIException {
110 checkCallable();
111 List<Ref> resultRefs;
112 try {
113 Collection<Ref> refs = new ArrayList<>();
114
115 // Also return HEAD if it's detached
116 Ref head = repo.exactRef(Constants.HEAD);
117 if (head != null && head.getLeaf().getName().equals(Constants.HEAD))
118 refs.add(head);
119
120 if (listMode == null) {
121 refs.addAll(getRefs(Constants.R_HEADS));
122 } else if (listMode == ListMode.REMOTE) {
123 refs.addAll(getRefs(Constants.R_REMOTES));
124 } else {
125 refs.addAll(getRefs(Constants.R_HEADS));
126 refs.addAll(getRefs(Constants.R_REMOTES));
127 }
128 resultRefs = new ArrayList<>(filterRefs(refs));
129 } catch (IOException e) {
130 throw new JGitInternalException(e.getMessage(), e);
131 }
132
133 Collections.sort(resultRefs, new Comparator<Ref>() {
134 @Override
135 public int compare(Ref o1, Ref o2) {
136 return o1.getName().compareTo(o2.getName());
137 }
138 });
139 setCallable(false);
140 return resultRefs;
141 }
142
143 private Collection<Ref> filterRefs(Collection<Ref> refs)
144 throws RefNotFoundException, IOException {
145 if (containsCommitish == null)
146 return refs;
147
148 try (RevWalk walk = new RevWalk(repo)) {
149 ObjectId resolved = repo.resolve(containsCommitish);
150 if (resolved == null)
151 throw new RefNotFoundException(MessageFormat.format(
152 JGitText.get().refNotResolved, containsCommitish));
153
154 RevCommit containsCommit = walk.parseCommit(resolved);
155 return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
156 refs);
157 }
158 }
159
160 /**
161 * Set the list mode
162 *
163 * @param listMode
164 * optional: corresponds to the -r/-a options; by default, only
165 * local branches will be listed
166 * @return this instance
167 */
168 public ListBranchCommand setListMode(ListMode listMode) {
169 checkCallable();
170 this.listMode = listMode;
171 return this;
172 }
173
174 /**
175 * If this is set, only the branches that contain the specified commit-ish
176 * as an ancestor are returned.
177 *
178 * @param containsCommitish
179 * a commit ID or ref name
180 * @return this instance
181 * @since 3.4
182 */
183 public ListBranchCommand setContains(String containsCommitish) {
184 checkCallable();
185 this.containsCommitish = containsCommitish;
186 return this;
187 }
188
189 private Collection<Ref> getRefs(String prefix) throws IOException {
190 return repo.getRefDatabase().getRefsByPrefix(prefix);
191 }
192 }