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