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 {
120 Transport transport = Transport.open(repo, remote);
121 try {
122 transport.setCheckFetchedObjects(checkFetchedObjects);
123 transport.setRemoveDeletedRefs(isRemoveDeletedRefs());
124 transport.setDryRun(dryRun);
125 if (tagOption != null)
126 transport.setTagOpt(tagOption);
127 transport.setFetchThin(thin);
128 configure(transport);
129
130 FetchResult result = transport.fetch(monitor, refSpecs);
131 return result;
132 } finally {
133 transport.close();
134 }
135 } catch (NoRemoteRepositoryException e) {
136 throw new InvalidRemoteException(MessageFormat.format(
137 JGitText.get().invalidRemote, remote), e);
138 } catch (TransportException e) {
139 throw new org.eclipse.jgit.api.errors.TransportException(
140 e.getMessage(), e);
141 } catch (URISyntaxException e) {
142 throw new InvalidRemoteException(MessageFormat.format(
143 JGitText.get().invalidRemote, remote));
144 } catch (NotSupportedException e) {
145 throw new JGitInternalException(
146 JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand,
147 e);
148 }
149
150 }
151
152
153
154
155
156
157
158
159
160
161 public FetchCommand setRemote(String remote) {
162 checkCallable();
163 this.remote = remote;
164 return this;
165 }
166
167
168
169
170 public String getRemote() {
171 return remote;
172 }
173
174
175
176
177 public int getTimeout() {
178 return timeout;
179 }
180
181
182
183
184 public boolean isCheckFetchedObjects() {
185 return checkFetchedObjects;
186 }
187
188
189
190
191
192
193
194 public FetchCommand setCheckFetchedObjects(boolean checkFetchedObjects) {
195 checkCallable();
196 this.checkFetchedObjects = checkFetchedObjects;
197 return this;
198 }
199
200
201
202
203 public boolean isRemoveDeletedRefs() {
204 if (removeDeletedRefs != null)
205 return removeDeletedRefs.booleanValue();
206 else {
207 boolean result = false;
208 StoredConfig config = repo.getConfig();
209 result = config.getBoolean(ConfigConstants.CONFIG_FETCH_SECTION,
210 null, ConfigConstants.CONFIG_KEY_PRUNE, result);
211 result = config.getBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
212 remote, ConfigConstants.CONFIG_KEY_PRUNE, result);
213 return result;
214 }
215 }
216
217
218
219
220
221
222
223 public FetchCommand setRemoveDeletedRefs(boolean removeDeletedRefs) {
224 checkCallable();
225 this.removeDeletedRefs = Boolean.valueOf(removeDeletedRefs);
226 return this;
227 }
228
229
230
231
232 public ProgressMonitor getProgressMonitor() {
233 return monitor;
234 }
235
236
237
238
239
240
241
242
243
244
245 public FetchCommand setProgressMonitor(ProgressMonitor monitor) {
246 checkCallable();
247 if (monitor == null) {
248 monitor = NullProgressMonitor.INSTANCE;
249 }
250 this.monitor = monitor;
251 return this;
252 }
253
254
255
256
257 public List<RefSpec> getRefSpecs() {
258 return refSpecs;
259 }
260
261
262
263
264
265
266
267 public FetchCommand setRefSpecs(RefSpec... specs) {
268 checkCallable();
269 this.refSpecs.clear();
270 for (RefSpec spec : specs)
271 refSpecs.add(spec);
272 return this;
273 }
274
275
276
277
278
279
280
281 public FetchCommand setRefSpecs(List<RefSpec> specs) {
282 checkCallable();
283 this.refSpecs.clear();
284 this.refSpecs.addAll(specs);
285 return this;
286 }
287
288
289
290
291 public boolean isDryRun() {
292 return dryRun;
293 }
294
295
296
297
298
299
300
301 public FetchCommand setDryRun(boolean dryRun) {
302 checkCallable();
303 this.dryRun = dryRun;
304 return this;
305 }
306
307
308
309
310 public boolean isThin() {
311 return thin;
312 }
313
314
315
316
317
318
319
320
321
322 public FetchCommand setThin(boolean thin) {
323 checkCallable();
324 this.thin = thin;
325 return this;
326 }
327
328
329
330
331
332
333
334 public FetchCommand setTagOpt(TagOpt tagOpt) {
335 checkCallable();
336 this.tagOption = tagOpt;
337 return this;
338 }
339 }