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.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
72
73
74
75
76
77
78
79
80 public class ListBranchCommand extends GitCommand<List<Ref>> {
81 private ListMode listMode;
82
83 private String containsCommitish;
84
85
86
87
88
89 public enum ListMode {
90
91
92
93 ALL,
94
95
96
97 REMOTE;
98 }
99
100
101
102
103
104
105
106 protected ListBranchCommand(Repository repo) {
107 super(repo);
108 }
109
110
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
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
166
167
168
169
170
171
172 public ListBranchCommand setListMode(ListMode listMode) {
173 checkCallable();
174 this.listMode = listMode;
175 return this;
176 }
177
178
179
180
181
182
183
184
185
186
187 public ListBranchCommand setContains(String containsCommitish) {
188 checkCallable();
189 this.containsCommitish = containsCommitish;
190 return this;
191 }
192 }