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