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 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
69
70
71
72
73
74
75
76
77 public class ListBranchCommand extends GitCommand<List<Ref>> {
78 private ListMode listMode;
79
80 private String containsCommitish;
81
82
83
84
85
86 public enum ListMode {
87
88
89
90 ALL,
91
92
93
94 REMOTE;
95 }
96
97
98
99
100 protected ListBranchCommand(Repository repo) {
101 super(repo);
102 }
103
104 @Override
105 public List<Ref> call() throws GitAPIException {
106 checkCallable();
107 List<Ref> resultRefs;
108 try {
109 Collection<Ref> refs = new ArrayList<>();
110
111
112 Ref head = repo.exactRef(Constants.HEAD);
113 if (head != null && head.getLeaf().getName().equals(Constants.HEAD))
114 refs.add(head);
115
116 if (listMode == null) {
117 refs.addAll(getRefs(Constants.R_HEADS));
118 } else if (listMode == ListMode.REMOTE) {
119 refs.addAll(getRefs(Constants.R_REMOTES));
120 } else {
121 refs.addAll(getRefs(Constants.R_HEADS));
122 refs.addAll(getRefs(Constants.R_REMOTES));
123 }
124 resultRefs = new ArrayList<>(filterRefs(refs));
125 } catch (IOException e) {
126 throw new JGitInternalException(e.getMessage(), e);
127 }
128
129 Collections.sort(resultRefs, new Comparator<Ref>() {
130 @Override
131 public int compare(Ref o1, Ref o2) {
132 return o1.getName().compareTo(o2.getName());
133 }
134 });
135 setCallable(false);
136 return resultRefs;
137 }
138
139 private Collection<Ref> filterRefs(Collection<Ref> refs)
140 throws RefNotFoundException, IOException {
141 if (containsCommitish == null)
142 return refs;
143
144 try (RevWalk walk = new RevWalk(repo)) {
145 ObjectId resolved = repo.resolve(containsCommitish);
146 if (resolved == null)
147 throw new RefNotFoundException(MessageFormat.format(
148 JGitText.get().refNotResolved, containsCommitish));
149
150 RevCommit containsCommit = walk.parseCommit(resolved);
151 return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
152 refs);
153 }
154 }
155
156
157
158
159
160
161
162 public ListBranchCommand setListMode(ListMode listMode) {
163 checkCallable();
164 this.listMode = listMode;
165 return this;
166 }
167
168
169
170
171
172
173
174
175
176
177 public ListBranchCommand setContains(String containsCommitish) {
178 checkCallable();
179 this.containsCommitish = containsCommitish;
180 return this;
181 }
182
183 private Collection<Ref> getRefs(String prefix) throws IOException {
184 return repo.getRefDatabase().getRefs(prefix).values();
185 }
186 }