1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import java.net.URISyntaxException;
13 import java.text.MessageFormat;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jgit.api.errors.GitAPIException;
21 import org.eclipse.jgit.api.errors.InvalidRemoteException;
22 import org.eclipse.jgit.api.errors.JGitInternalException;
23 import org.eclipse.jgit.errors.NotSupportedException;
24 import org.eclipse.jgit.errors.TransportException;
25 import org.eclipse.jgit.internal.JGitText;
26 import org.eclipse.jgit.lib.Constants;
27 import org.eclipse.jgit.lib.Ref;
28 import org.eclipse.jgit.lib.Repository;
29 import org.eclipse.jgit.transport.FetchConnection;
30 import org.eclipse.jgit.transport.RefSpec;
31 import org.eclipse.jgit.transport.Transport;
32 import org.eclipse.jgit.transport.URIish;
33
34
35
36
37
38
39
40
41 public class LsRemoteCommand extends
42 TransportCommand<LsRemoteCommand, Collection<Ref>> {
43
44 private String remote = Constants.DEFAULT_REMOTE_NAME;
45
46 private boolean heads;
47
48 private boolean tags;
49
50 private String uploadPack;
51
52
53
54
55
56
57
58
59 public LsRemoteCommand(Repository repo) {
60 super(repo);
61 }
62
63
64
65
66
67
68
69
70
71
72
73 public LsRemoteCommand setRemote(String remote) {
74 checkCallable();
75 this.remote = remote;
76 return this;
77 }
78
79
80
81
82
83
84
85
86 public LsRemoteCommand setHeads(boolean heads) {
87 this.heads = heads;
88 return this;
89 }
90
91
92
93
94
95
96
97
98 public LsRemoteCommand setTags(boolean tags) {
99 this.tags = tags;
100 return this;
101 }
102
103
104
105
106
107
108
109
110
111 public LsRemoteCommand setUploadPack(String uploadPack) {
112 this.uploadPack = uploadPack;
113 return this;
114 }
115
116
117
118
119
120
121
122
123
124 @Override
125 public Collection<Ref> call() throws GitAPIException,
126 InvalidRemoteException,
127 org.eclipse.jgit.api.errors.TransportException {
128 return execute().values();
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public Map<String, Ref> callAsMap() throws GitAPIException,
144 InvalidRemoteException,
145 org.eclipse.jgit.api.errors.TransportException {
146 return Collections.unmodifiableMap(execute());
147 }
148
149 private Map<String, Ref> execute() throws GitAPIException,
150 InvalidRemoteException,
151 org.eclipse.jgit.api.errors.TransportException {
152 checkCallable();
153
154 try (Transport transport = repo != null
155 ? Transport.open(repo, remote)
156 : Transport.open(new URIish(remote))) {
157 transport.setOptionUploadPack(uploadPack);
158 configure(transport);
159 Collection<RefSpec> refSpecs = new ArrayList<>(1);
160 if (tags)
161 refSpecs.add(new RefSpec(
162 "refs/tags/*:refs/remotes/origin/tags/*"));
163 if (heads)
164 refSpecs.add(new RefSpec("refs/heads/*:refs/remotes/origin/*"));
165 Collection<Ref> refs;
166 Map<String, Ref> refmap = new HashMap<>();
167 try (FetchConnection fc = transport.openFetch(refSpecs)) {
168 refs = fc.getRefs();
169 if (refSpecs.isEmpty())
170 for (Ref r : refs)
171 refmap.put(r.getName(), r);
172 else
173 for (Ref r : refs)
174 for (RefSpec rs : refSpecs)
175 if (rs.matchSource(r)) {
176 refmap.put(r.getName(), r);
177 break;
178 }
179 return refmap;
180 }
181 } catch (URISyntaxException e) {
182 throw new InvalidRemoteException(MessageFormat.format(
183 JGitText.get().invalidRemote, remote), e);
184 } catch (NotSupportedException e) {
185 throw new JGitInternalException(
186 JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
187 e);
188 } catch (TransportException e) {
189 throw new org.eclipse.jgit.api.errors.TransportException(
190 e.getMessage(),
191 e);
192 }
193 }
194
195 }