1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.transport;
14
15 import java.io.Serializable;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20
21 import org.eclipse.jgit.lib.Config;
22
23
24
25
26
27
28
29
30
31 public class RemoteConfig implements Serializable {
32 private static final long serialVersionUID = 1L;
33
34 private static final String SECTION = "remote";
35
36 private static final String KEY_URL = "url";
37
38 private static final String KEY_PUSHURL = "pushurl";
39
40 private static final String KEY_FETCH = "fetch";
41
42 private static final String KEY_PUSH = "push";
43
44 private static final String KEY_UPLOADPACK = "uploadpack";
45
46 private static final String KEY_RECEIVEPACK = "receivepack";
47
48 private static final String KEY_TAGOPT = "tagopt";
49
50 private static final String KEY_MIRROR = "mirror";
51
52 private static final String KEY_TIMEOUT = "timeout";
53
54 private static final boolean DEFAULT_MIRROR = false;
55
56
57 public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack";
58
59
60 public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack";
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public static List<RemoteConfig> getAllRemoteConfigs(Config rc)
76 throws URISyntaxException {
77 final List<String> names = new ArrayList<>(rc
78 .getSubsections(SECTION));
79 Collections.sort(names);
80
81 final List<RemoteConfig> result = new ArrayList<>(names
82 .size());
83 for (String name : names)
84 result.add(new RemoteConfig(rc, name));
85 return result;
86 }
87
88 private String name;
89
90 private List<URIish> uris;
91
92 private List<URIish> pushURIs;
93
94 private List<RefSpec> fetch;
95
96 private List<RefSpec> push;
97
98 private String uploadpack;
99
100 private String receivepack;
101
102 private TagOpt tagopt;
103
104 private boolean mirror;
105
106 private int timeout;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public RemoteConfig(Config rc, String remoteName)
124 throws URISyntaxException {
125 name = remoteName;
126
127 String[] vlst;
128 String val;
129
130 vlst = rc.getStringList(SECTION, name, KEY_URL);
131 UrlConfig urls = new UrlConfig(rc);
132 uris = new ArrayList<>(vlst.length);
133 for (String s : vlst) {
134 uris.add(new URIish(urls.replace(s)));
135 }
136 String[] plst = rc.getStringList(SECTION, name, KEY_PUSHURL);
137 pushURIs = new ArrayList<>(plst.length);
138 for (String s : plst) {
139 pushURIs.add(new URIish(s));
140 }
141 if (pushURIs.isEmpty()) {
142
143
144 if (urls.hasPushReplacements()) {
145 for (String s : vlst) {
146 String replaced = urls.replacePush(s);
147 if (!s.equals(replaced)) {
148 pushURIs.add(new URIish(replaced));
149 }
150 }
151 }
152 }
153 fetch = rc.getRefSpecs(SECTION, name, KEY_FETCH);
154 push = rc.getRefSpecs(SECTION, name, KEY_PUSH);
155 val = rc.getString(SECTION, name, KEY_UPLOADPACK);
156 if (val == null) {
157 val = DEFAULT_UPLOAD_PACK;
158 }
159 uploadpack = val;
160
161 val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
162 if (val == null) {
163 val = DEFAULT_RECEIVE_PACK;
164 }
165 receivepack = val;
166
167 try {
168 val = rc.getString(SECTION, name, KEY_TAGOPT);
169 tagopt = TagOpt.fromOption(val);
170 } catch (IllegalArgumentException e) {
171
172 tagopt = TagOpt.AUTO_FOLLOW;
173 }
174 mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
175 timeout = rc.getInt(SECTION, name, KEY_TIMEOUT, 0);
176 }
177
178
179
180
181
182
183
184 public void update(Config rc) {
185 final List<String> vlst = new ArrayList<>();
186
187 vlst.clear();
188 for (URIish u : getURIs())
189 vlst.add(u.toPrivateString());
190 rc.setStringList(SECTION, getName(), KEY_URL, vlst);
191
192 vlst.clear();
193 for (URIish u : getPushURIs())
194 vlst.add(u.toPrivateString());
195 rc.setStringList(SECTION, getName(), KEY_PUSHURL, vlst);
196
197 vlst.clear();
198 for (RefSpec u : getFetchRefSpecs())
199 vlst.add(u.toString());
200 rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
201
202 vlst.clear();
203 for (RefSpec u : getPushRefSpecs())
204 vlst.add(u.toString());
205 rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
206
207 set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
208 set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
209 set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
210 set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
211 set(rc, KEY_TIMEOUT, timeout, 0);
212 }
213
214 private void set(final Config rc, final String key,
215 final String currentValue, final String defaultValue) {
216 if (defaultValue.equals(currentValue))
217 unset(rc, key);
218 else
219 rc.setString(SECTION, getName(), key, currentValue);
220 }
221
222 private void set(final Config rc, final String key,
223 final boolean currentValue, final boolean defaultValue) {
224 if (defaultValue == currentValue)
225 unset(rc, key);
226 else
227 rc.setBoolean(SECTION, getName(), key, currentValue);
228 }
229
230 private void set(final Config rc, final String key, final int currentValue,
231 final int defaultValue) {
232 if (defaultValue == currentValue)
233 unset(rc, key);
234 else
235 rc.setInt(SECTION, getName(), key, currentValue);
236 }
237
238 private void unset(Config rc, String key) {
239 rc.unset(SECTION, getName(), key);
240 }
241
242
243
244
245
246
247 public String getName() {
248 return name;
249 }
250
251
252
253
254
255
256 public List<URIish> getURIs() {
257 return Collections.unmodifiableList(uris);
258 }
259
260
261
262
263
264
265
266
267 public boolean addURI(URIish toAdd) {
268 if (uris.contains(toAdd))
269 return false;
270 return uris.add(toAdd);
271 }
272
273
274
275
276
277
278
279
280 public boolean removeURI(URIish toRemove) {
281 return uris.remove(toRemove);
282 }
283
284
285
286
287
288
289 public List<URIish> getPushURIs() {
290 return Collections.unmodifiableList(pushURIs);
291 }
292
293
294
295
296
297
298
299
300 public boolean addPushURI(URIish toAdd) {
301 if (pushURIs.contains(toAdd))
302 return false;
303 return pushURIs.add(toAdd);
304 }
305
306
307
308
309
310
311
312
313 public boolean removePushURI(URIish toRemove) {
314 return pushURIs.remove(toRemove);
315 }
316
317
318
319
320
321
322 public List<RefSpec> getFetchRefSpecs() {
323 return Collections.unmodifiableList(fetch);
324 }
325
326
327
328
329
330
331
332
333 public boolean addFetchRefSpec(RefSpec s) {
334 if (fetch.contains(s))
335 return false;
336 return fetch.add(s);
337 }
338
339
340
341
342
343
344
345
346 public void setFetchRefSpecs(List<RefSpec> specs) {
347 fetch.clear();
348 fetch.addAll(specs);
349 }
350
351
352
353
354
355
356
357
358 public void setPushRefSpecs(List<RefSpec> specs) {
359 push.clear();
360 push.addAll(specs);
361 }
362
363
364
365
366
367
368
369
370 public boolean removeFetchRefSpec(RefSpec s) {
371 return fetch.remove(s);
372 }
373
374
375
376
377
378
379 public List<RefSpec> getPushRefSpecs() {
380 return Collections.unmodifiableList(push);
381 }
382
383
384
385
386
387
388
389
390 public boolean addPushRefSpec(RefSpec s) {
391 if (push.contains(s))
392 return false;
393 return push.add(s);
394 }
395
396
397
398
399
400
401
402
403 public boolean removePushRefSpec(RefSpec s) {
404 return push.remove(s);
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418 public String getUploadPack() {
419 return uploadpack;
420 }
421
422
423
424
425
426
427
428
429
430
431
432
433 public String getReceivePack() {
434 return receivepack;
435 }
436
437
438
439
440
441
442 public TagOpt getTagOpt() {
443 return tagopt;
444 }
445
446
447
448
449
450
451
452 public void setTagOpt(TagOpt option) {
453 tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
454 }
455
456
457
458
459
460
461
462
463 public boolean isMirror() {
464 return mirror;
465 }
466
467
468
469
470
471
472
473 public void setMirror(boolean m) {
474 mirror = m;
475 }
476
477
478
479
480
481
482 public int getTimeout() {
483 return timeout;
484 }
485
486
487
488
489
490
491
492
493
494 public void setTimeout(int seconds) {
495 timeout = seconds;
496 }
497 }