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.List;
49
50 import org.eclipse.jgit.api.errors.GitAPIException;
51 import org.eclipse.jgit.api.errors.InvalidRemoteException;
52 import org.eclipse.jgit.api.errors.JGitInternalException;
53 import org.eclipse.jgit.errors.NoRemoteRepositoryException;
54 import org.eclipse.jgit.errors.NotSupportedException;
55 import org.eclipse.jgit.errors.TransportException;
56 import org.eclipse.jgit.internal.JGitText;
57 import org.eclipse.jgit.lib.ConfigConstants;
58 import org.eclipse.jgit.lib.Constants;
59 import org.eclipse.jgit.lib.NullProgressMonitor;
60 import org.eclipse.jgit.lib.ProgressMonitor;
61 import org.eclipse.jgit.lib.Repository;
62 import org.eclipse.jgit.lib.StoredConfig;
63 import org.eclipse.jgit.transport.FetchResult;
64 import org.eclipse.jgit.transport.RefSpec;
65 import org.eclipse.jgit.transport.TagOpt;
66 import org.eclipse.jgit.transport.Transport;
67
68
69
70
71
72
73
74
75
76 public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
77
78 private String remote = Constants.DEFAULT_REMOTE_NAME;
79
80 private List<RefSpec> refSpecs;
81
82 private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
83
84 private boolean checkFetchedObjects;
85
86 private Boolean removeDeletedRefs;
87
88 private boolean dryRun;
89
90 private boolean thin = Transport.DEFAULT_FETCH_THIN;
91
92 private TagOpt tagOption;
93
94
95
96
97 protected FetchCommand(Repository repo) {
98 super(repo);
99 refSpecs = new ArrayList<RefSpec>(3);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public FetchResult call() throws GitAPIException, InvalidRemoteException,
116 org.eclipse.jgit.api.errors.TransportException {
117 checkCallable();
118
119 try (Transport transport = Transport.open(repo, remote)) {
120 transport.setCheckFetchedObjects(checkFetchedObjects);
121 transport.setRemoveDeletedRefs(isRemoveDeletedRefs());
122 transport.setDryRun(dryRun);
123 if (tagOption != null)
124 transport.setTagOpt(tagOption);
125 transport.setFetchThin(thin);
126 configure(transport);
127
128 FetchResult result = transport.fetch(monitor, refSpecs);
129 return result;
130 } catch (NoRemoteRepositoryException e) {
131 throw new InvalidRemoteException(MessageFormat.format(
132 JGitText.get().invalidRemote, remote), e);
133 } catch (TransportException e) {
134 throw new org.eclipse.jgit.api.errors.TransportException(
135 e.getMessage(), e);
136 } catch (URISyntaxException e) {
137 throw new InvalidRemoteException(MessageFormat.format(
138 JGitText.get().invalidRemote, remote));
139 } catch (NotSupportedException e) {
140 throw new JGitInternalException(
141 JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand,
142 e);
143 }
144
145 }
146
147
148
149
150
151
152
153
154
155
156 public FetchCommand setRemote(String remote) {
157 checkCallable();
158 this.remote = remote;
159 return this;
160 }
161
162
163
164
165 public String getRemote() {
166 return remote;
167 }
168
169
170
171
172 public int getTimeout() {
173 return timeout;
174 }
175
176
177
178
179 public boolean isCheckFetchedObjects() {
180 return checkFetchedObjects;
181 }
182
183
184
185
186
187
188
189 public FetchCommand setCheckFetchedObjects(boolean checkFetchedObjects) {
190 checkCallable();
191 this.checkFetchedObjects = checkFetchedObjects;
192 return this;
193 }
194
195
196
197
198 public boolean isRemoveDeletedRefs() {
199 if (removeDeletedRefs != null)
200 return removeDeletedRefs.booleanValue();
201 else {
202 boolean result = false;
203 StoredConfig config = repo.getConfig();
204 result = config.getBoolean(ConfigConstants.CONFIG_FETCH_SECTION,
205 null, ConfigConstants.CONFIG_KEY_PRUNE, result);
206 result = config.getBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
207 remote, ConfigConstants.CONFIG_KEY_PRUNE, result);
208 return result;
209 }
210 }
211
212
213
214
215
216
217
218 public FetchCommand setRemoveDeletedRefs(boolean removeDeletedRefs) {
219 checkCallable();
220 this.removeDeletedRefs = Boolean.valueOf(removeDeletedRefs);
221 return this;
222 }
223
224
225
226
227 public ProgressMonitor getProgressMonitor() {
228 return monitor;
229 }
230
231
232
233
234
235
236
237
238
239
240 public FetchCommand setProgressMonitor(ProgressMonitor monitor) {
241 checkCallable();
242 if (monitor == null) {
243 monitor = NullProgressMonitor.INSTANCE;
244 }
245 this.monitor = monitor;
246 return this;
247 }
248
249
250
251
252 public List<RefSpec> getRefSpecs() {
253 return refSpecs;
254 }
255
256
257
258
259
260
261
262 public FetchCommand setRefSpecs(RefSpec... specs) {
263 checkCallable();
264 this.refSpecs.clear();
265 for (RefSpec spec : specs)
266 refSpecs.add(spec);
267 return this;
268 }
269
270
271
272
273
274
275
276 public FetchCommand setRefSpecs(List<RefSpec> specs) {
277 checkCallable();
278 this.refSpecs.clear();
279 this.refSpecs.addAll(specs);
280 return this;
281 }
282
283
284
285
286 public boolean isDryRun() {
287 return dryRun;
288 }
289
290
291
292
293
294
295
296 public FetchCommand setDryRun(boolean dryRun) {
297 checkCallable();
298 this.dryRun = dryRun;
299 return this;
300 }
301
302
303
304
305 public boolean isThin() {
306 return thin;
307 }
308
309
310
311
312
313
314
315
316
317 public FetchCommand setThin(boolean thin) {
318 checkCallable();
319 this.thin = thin;
320 return this;
321 }
322
323
324
325
326
327
328
329 public FetchCommand setTagOpt(TagOpt tagOpt) {
330 checkCallable();
331 this.tagOption = tagOpt;
332 return this;
333 }
334 }