1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.List;
57
58 import org.eclipse.jgit.api.errors.GitAPIException;
59 import org.eclipse.jgit.api.errors.JGitInternalException;
60 import org.eclipse.jgit.api.errors.RefNotFoundException;
61 import org.eclipse.jgit.internal.JGitText;
62 import org.eclipse.jgit.lib.ObjectId;
63 import org.eclipse.jgit.lib.Ref;
64 import org.eclipse.jgit.lib.Repository;
65 import org.eclipse.jgit.revwalk.RevCommit;
66 import org.eclipse.jgit.revwalk.RevWalk;
67 import org.eclipse.jgit.revwalk.RevWalkUtils;
68
69
70
71
72
73
74
75
76
77
78
79 public class ListBranchCommand extends GitCommand<List<Ref>> {
80 private ListMode listMode;
81
82 private String containsCommitish;
83
84
85
86
87
88 public enum ListMode {
89
90
91
92 ALL,
93
94
95
96 REMOTE;
97 }
98
99
100
101
102
103
104
105 protected ListBranchCommand(Repository repo) {
106 super(repo);
107 }
108
109
110 @Override
111 public List<Ref> call() throws GitAPIException {
112 checkCallable();
113 List<Ref> resultRefs;
114 try {
115 Collection<Ref> refs = new ArrayList<>();
116
117
118 Ref head = repo.exactRef(HEAD);
119 if (head != null && head.getLeaf().getName().equals(HEAD)) {
120 refs.add(head);
121 }
122
123 if (listMode == null) {
124 refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS));
125 } else if (listMode == ListMode.REMOTE) {
126 refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_REMOTES));
127 } else {
128 refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS,
129 R_REMOTES));
130 }
131 resultRefs = new ArrayList<>(filterRefs(refs));
132 } catch (IOException e) {
133 throw new JGitInternalException(e.getMessage(), e);
134 }
135
136 Collections.sort(resultRefs,
137 (Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) -> o1.getName().compareTo(o2.getName()));
138 setCallable(false);
139 return resultRefs;
140 }
141
142 private Collection<Ref> filterRefs(Collection<Ref> refs)
143 throws RefNotFoundException, IOException {
144 if (containsCommitish == null)
145 return refs;
146
147 try (RevWalkvWalk.html#RevWalk">RevWalk walk = new RevWalk(repo)) {
148 ObjectId resolved = repo.resolve(containsCommitish);
149 if (resolved == null)
150 throw new RefNotFoundException(MessageFormat.format(
151 JGitText.get().refNotResolved, containsCommitish));
152
153 RevCommit containsCommit = walk.parseCommit(resolved);
154 return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
155 refs);
156 }
157 }
158
159
160
161
162
163
164
165
166
167 public ListBranchCommand setListMode(ListMode listMode) {
168 checkCallable();
169 this.listMode = listMode;
170 return this;
171 }
172
173
174
175
176
177
178
179
180
181
182 public ListBranchCommand setContains(String containsCommitish) {
183 checkCallable();
184 this.containsCommitish = containsCommitish;
185 return this;
186 }
187 }