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 static org.eclipse.jgit.lib.Constants.HEAD;
48 import static org.eclipse.jgit.lib.Constants.R_HEADS;
49 import static org.eclipse.jgit.lib.Constants.R_REMOTES;
50
51 import java.io.IOException;
52 import java.text.MessageFormat;
53 import java.util.ArrayList;
54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.Comparator;
57 import java.util.List;
58
59 import org.eclipse.jgit.api.errors.GitAPIException;
60 import org.eclipse.jgit.api.errors.JGitInternalException;
61 import org.eclipse.jgit.api.errors.RefNotFoundException;
62 import org.eclipse.jgit.internal.JGitText;
63 import org.eclipse.jgit.lib.ObjectId;
64 import org.eclipse.jgit.lib.Ref;
65 import org.eclipse.jgit.lib.Repository;
66 import org.eclipse.jgit.revwalk.RevCommit;
67 import org.eclipse.jgit.revwalk.RevWalk;
68 import org.eclipse.jgit.revwalk.RevWalkUtils;
69
70 /**
71 * Used to obtain a list of branches.
72 * <p>
73 * In case HEAD is detached (it points directly to a commit), it is also
74 * returned in the results.
75 *
76 * @see <a
77 * href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
78 * >Git documentation about Branch</a>
79 */
80 public class ListBranchCommand extends GitCommand<List<Ref>> {
81 private ListMode listMode;
82
83 private String containsCommitish;
84
85 /**
86 * The modes available for listing branches (corresponding to the -r and -a
87 * options)
88 */
89 public enum ListMode {
90 /**
91 * Corresponds to the -a option (all branches)
92 */
93 ALL,
94 /**
95 * Corresponds to the -r option (remote branches only)
96 */
97 REMOTE;
98 }
99
100 /**
101 * Constructor for ListBranchCommand.
102 *
103 * @param repo
104 * a {@link org.eclipse.jgit.lib.Repository} object.
105 */
106 protected ListBranchCommand(Repository repo) {
107 super(repo);
108 }
109
110 /** {@inheritDoc} */
111 @Override
112 public List<Ref> call() throws GitAPIException {
113 checkCallable();
114 List<Ref> resultRefs;
115 try {
116 Collection<Ref> refs = new ArrayList<>();
117
118 // Also return HEAD if it's detached
119 Ref head = repo.exactRef(HEAD);
120 if (head != null && head.getLeaf().getName().equals(HEAD)) {
121 refs.add(head);
122 }
123
124 if (listMode == null) {
125 refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS));
126 } else if (listMode == ListMode.REMOTE) {
127 refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_REMOTES));
128 } else {
129 refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS,
130 R_REMOTES));
131 }
132 resultRefs = new ArrayList<>(filterRefs(refs));
133 } catch (IOException e) {
134 throw new JGitInternalException(e.getMessage(), e);
135 }
136
137 Collections.sort(resultRefs, new Comparator<Ref>() {
138 @Override
139 public int compare(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) {
140 return o1.getName().compareTo(o2.getName());
141 }
142 });
143 setCallable(false);
144 return resultRefs;
145 }
146
147 private Collection<Ref> filterRefs(Collection<Ref> refs)
148 throws RefNotFoundException, IOException {
149 if (containsCommitish == null)
150 return refs;
151
152 try (RevWalkvWalk.html#RevWalk">RevWalk walk = new RevWalk(repo)) {
153 ObjectId resolved = repo.resolve(containsCommitish);
154 if (resolved == null)
155 throw new RefNotFoundException(MessageFormat.format(
156 JGitText.get().refNotResolved, containsCommitish));
157
158 RevCommit containsCommit = walk.parseCommit(resolved);
159 return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
160 refs);
161 }
162 }
163
164 /**
165 * Set the list mode
166 *
167 * @param listMode
168 * optional: corresponds to the -r/-a options; by default, only
169 * local branches will be listed
170 * @return this instance
171 */
172 public ListBranchCommand setListMode(ListMode listMode) {
173 checkCallable();
174 this.listMode = listMode;
175 return this;
176 }
177
178 /**
179 * If this is set, only the branches that contain the specified commit-ish
180 * as an ancestor are returned.
181 *
182 * @param containsCommitish
183 * a commit ID or ref name
184 * @return this instance
185 * @since 3.4
186 */
187 public ListBranchCommand setContains(String containsCommitish) {
188 checkCallable();
189 this.containsCommitish = containsCommitish;
190 return this;
191 }
192 }