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 package org.eclipse.jgit.api;
44
45 import java.net.URISyntaxException;
46 import java.text.MessageFormat;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.Map;
52
53 import org.eclipse.jgit.api.errors.GitAPIException;
54 import org.eclipse.jgit.api.errors.InvalidRemoteException;
55 import org.eclipse.jgit.api.errors.JGitInternalException;
56 import org.eclipse.jgit.errors.NotSupportedException;
57 import org.eclipse.jgit.errors.TransportException;
58 import org.eclipse.jgit.internal.JGitText;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.lib.Ref;
61 import org.eclipse.jgit.lib.Repository;
62 import org.eclipse.jgit.transport.FetchConnection;
63 import org.eclipse.jgit.transport.RefSpec;
64 import org.eclipse.jgit.transport.Transport;
65 import org.eclipse.jgit.transport.URIish;
66
67
68
69
70
71
72
73
74 public class LsRemoteCommand extends
75 TransportCommand<LsRemoteCommand, Collection<Ref>> {
76
77 private String remote = Constants.DEFAULT_REMOTE_NAME;
78
79 private boolean heads;
80
81 private boolean tags;
82
83 private String uploadPack;
84
85
86
87
88
89
90 public LsRemoteCommand(Repository repo) {
91 super(repo);
92 }
93
94
95
96
97
98
99
100
101
102
103 public LsRemoteCommand setRemote(String remote) {
104 checkCallable();
105 this.remote = remote;
106 return this;
107 }
108
109
110
111
112
113
114
115 public LsRemoteCommand setHeads(boolean heads) {
116 this.heads = heads;
117 return this;
118 }
119
120
121
122
123
124
125
126 public LsRemoteCommand setTags(boolean tags) {
127 this.tags = tags;
128 return this;
129 }
130
131
132
133
134
135
136
137 public LsRemoteCommand setUploadPack(String uploadPack) {
138 this.uploadPack = uploadPack;
139 return this;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public Collection<Ref> call() throws GitAPIException,
157 InvalidRemoteException,
158 org.eclipse.jgit.api.errors.TransportException {
159 return execute().values();
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174 public Map<String, Ref> callAsMap() throws GitAPIException,
175 InvalidRemoteException,
176 org.eclipse.jgit.api.errors.TransportException {
177 return Collections.unmodifiableMap(execute());
178 }
179
180 private Map<String, Ref> execute() throws GitAPIException,
181 InvalidRemoteException,
182 org.eclipse.jgit.api.errors.TransportException {
183 checkCallable();
184
185 Transport transport = null;
186 FetchConnection fc = null;
187 try {
188 if (repo != null)
189 transport = Transport.open(repo, remote);
190 else
191 transport = Transport.open(new URIish(remote));
192 transport.setOptionUploadPack(uploadPack);
193 configure(transport);
194 Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
195 if (tags)
196 refSpecs.add(new RefSpec(
197 "refs/tags/*:refs/remotes/origin/tags/*"));
198 if (heads)
199 refSpecs.add(new RefSpec("refs/heads/*:refs/remotes/origin/*"));
200 Collection<Ref> refs;
201 Map<String, Ref> refmap = new HashMap<String, Ref>();
202 fc = transport.openFetch();
203 refs = fc.getRefs();
204 if (refSpecs.isEmpty())
205 for (Ref r : refs)
206 refmap.put(r.getName(), r);
207 else
208 for (Ref r : refs)
209 for (RefSpec rs : refSpecs)
210 if (rs.matchSource(r)) {
211 refmap.put(r.getName(), r);
212 break;
213 }
214 return refmap;
215 } catch (URISyntaxException e) {
216 throw new InvalidRemoteException(MessageFormat.format(
217 JGitText.get().invalidRemote, remote));
218 } catch (NotSupportedException e) {
219 throw new JGitInternalException(
220 JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
221 e);
222 } catch (TransportException e) {
223 throw new org.eclipse.jgit.api.errors.TransportException(
224 e.getMessage(),
225 e);
226 } finally {
227 if (fc != null)
228 fc.close();
229 if (transport != null)
230 transport.close();
231 }
232 }
233
234 }