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
44
45 package org.eclipse.jgit.transport;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertSame;
51 import static org.junit.Assert.assertTrue;
52
53 import java.util.Arrays;
54 import java.util.Collections;
55 import java.util.List;
56
57 import org.eclipse.jgit.errors.ConfigInvalidException;
58 import org.eclipse.jgit.lib.Config;
59 import org.junit.Before;
60 import org.junit.Test;
61
62 public class RemoteConfigTest {
63 private Config config;
64
65 @Before
66 public void setUp() throws Exception {
67 config = new Config();
68 }
69
70 private void readConfig(String dat) throws ConfigInvalidException {
71 config = new Config();
72 config.fromText(dat);
73 }
74
75 private void checkConfig(String exp) {
76 assertEquals(exp, config.toText());
77 }
78
79 @Test
80 public void testSimple() throws Exception {
81 readConfig("[remote \"spearce\"]\n"
82 + "url = http://www.spearce.org/egit.git\n"
83 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
84
85 final RemoteConfig rc = new RemoteConfig(config, "spearce");
86 final List<URIish> allURIs = rc.getURIs();
87 RefSpec spec;
88
89 assertEquals("spearce", rc.getName());
90 assertNotNull(allURIs);
91 assertNotNull(rc.getFetchRefSpecs());
92 assertNotNull(rc.getPushRefSpecs());
93 assertNotNull(rc.getTagOpt());
94 assertEquals(0, rc.getTimeout());
95 assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
96
97 assertEquals(1, allURIs.size());
98 assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
99 .toString());
100
101 assertEquals(1, rc.getFetchRefSpecs().size());
102 spec = rc.getFetchRefSpecs().get(0);
103 assertTrue(spec.isForceUpdate());
104 assertTrue(spec.isWildcard());
105 assertEquals("refs/heads/*", spec.getSource());
106 assertEquals("refs/remotes/spearce/*", spec.getDestination());
107
108 assertEquals(0, rc.getPushRefSpecs().size());
109 }
110
111 @Test
112 public void testSimpleNoTags() throws Exception {
113 readConfig("[remote \"spearce\"]\n"
114 + "url = http://www.spearce.org/egit.git\n"
115 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
116 + "tagopt = --no-tags\n");
117 final RemoteConfig rc = new RemoteConfig(config, "spearce");
118 assertSame(TagOpt.NO_TAGS, rc.getTagOpt());
119 }
120
121 @Test
122 public void testSimpleAlwaysTags() throws Exception {
123 readConfig("[remote \"spearce\"]\n"
124 + "url = http://www.spearce.org/egit.git\n"
125 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
126 + "tagopt = --tags\n");
127 final RemoteConfig rc = new RemoteConfig(config, "spearce");
128 assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
129 }
130
131 @Test
132 public void testMirror() throws Exception {
133 readConfig("[remote \"spearce\"]\n"
134 + "url = http://www.spearce.org/egit.git\n"
135 + "fetch = +refs/heads/*:refs/heads/*\n"
136 + "fetch = refs/tags/*:refs/tags/*\n");
137
138 final RemoteConfig rc = new RemoteConfig(config, "spearce");
139 final List<URIish> allURIs = rc.getURIs();
140 RefSpec spec;
141
142 assertEquals("spearce", rc.getName());
143 assertNotNull(allURIs);
144 assertNotNull(rc.getFetchRefSpecs());
145 assertNotNull(rc.getPushRefSpecs());
146
147 assertEquals(1, allURIs.size());
148 assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
149 .toString());
150
151 assertEquals(2, rc.getFetchRefSpecs().size());
152
153 spec = rc.getFetchRefSpecs().get(0);
154 assertTrue(spec.isForceUpdate());
155 assertTrue(spec.isWildcard());
156 assertEquals("refs/heads/*", spec.getSource());
157 assertEquals("refs/heads/*", spec.getDestination());
158
159 spec = rc.getFetchRefSpecs().get(1);
160 assertFalse(spec.isForceUpdate());
161 assertTrue(spec.isWildcard());
162 assertEquals("refs/tags/*", spec.getSource());
163 assertEquals("refs/tags/*", spec.getDestination());
164
165 assertEquals(0, rc.getPushRefSpecs().size());
166 }
167
168 @Test
169 public void testBackup() throws Exception {
170 readConfig("[remote \"backup\"]\n"
171 + "url = http://www.spearce.org/egit.git\n"
172 + "url = user@repo.or.cz:/srv/git/egit.git\n"
173 + "push = +refs/heads/*:refs/heads/*\n"
174 + "push = refs/tags/*:refs/tags/*\n");
175
176 final RemoteConfig rc = new RemoteConfig(config, "backup");
177 final List<URIish> allURIs = rc.getURIs();
178 RefSpec spec;
179
180 assertEquals("backup", rc.getName());
181 assertNotNull(allURIs);
182 assertNotNull(rc.getFetchRefSpecs());
183 assertNotNull(rc.getPushRefSpecs());
184
185 assertEquals(2, allURIs.size());
186 assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
187 .toString());
188 assertEquals("user@repo.or.cz:/srv/git/egit.git", allURIs.get(1)
189 .toString());
190
191 assertEquals(0, rc.getFetchRefSpecs().size());
192
193 assertEquals(2, rc.getPushRefSpecs().size());
194 spec = rc.getPushRefSpecs().get(0);
195 assertTrue(spec.isForceUpdate());
196 assertTrue(spec.isWildcard());
197 assertEquals("refs/heads/*", spec.getSource());
198 assertEquals("refs/heads/*", spec.getDestination());
199
200 spec = rc.getPushRefSpecs().get(1);
201 assertFalse(spec.isForceUpdate());
202 assertTrue(spec.isWildcard());
203 assertEquals("refs/tags/*", spec.getSource());
204 assertEquals("refs/tags/*", spec.getDestination());
205 }
206
207 @Test
208 public void testUploadPack() throws Exception {
209 readConfig("[remote \"example\"]\n"
210 + "url = user@example.com:egit.git\n"
211 + "fetch = +refs/heads/*:refs/remotes/example/*\n"
212 + "uploadpack = /path/to/git/git-upload-pack\n"
213 + "receivepack = /path/to/git/git-receive-pack\n");
214
215 final RemoteConfig rc = new RemoteConfig(config, "example");
216 final List<URIish> allURIs = rc.getURIs();
217 RefSpec spec;
218
219 assertEquals("example", rc.getName());
220 assertNotNull(allURIs);
221 assertNotNull(rc.getFetchRefSpecs());
222 assertNotNull(rc.getPushRefSpecs());
223
224 assertEquals(1, allURIs.size());
225 assertEquals("user@example.com:egit.git", allURIs.get(0).toString());
226
227 assertEquals(1, rc.getFetchRefSpecs().size());
228 spec = rc.getFetchRefSpecs().get(0);
229 assertTrue(spec.isForceUpdate());
230 assertTrue(spec.isWildcard());
231 assertEquals("refs/heads/*", spec.getSource());
232 assertEquals("refs/remotes/example/*", spec.getDestination());
233
234 assertEquals(0, rc.getPushRefSpecs().size());
235
236 assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
237 assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
238 }
239
240 @Test
241 public void testUnknown() throws Exception {
242 readConfig("");
243
244 final RemoteConfig rc = new RemoteConfig(config, "backup");
245 assertEquals(0, rc.getURIs().size());
246 assertEquals(0, rc.getFetchRefSpecs().size());
247 assertEquals(0, rc.getPushRefSpecs().size());
248 assertEquals("git-upload-pack", rc.getUploadPack());
249 assertEquals("git-receive-pack", rc.getReceivePack());
250 }
251
252 @Test
253 public void testAddURI() throws Exception {
254 readConfig("");
255
256 final URIish uri = new URIish("/some/dir");
257 final RemoteConfig rc = new RemoteConfig(config, "backup");
258 assertEquals(0, rc.getURIs().size());
259
260 assertTrue(rc.addURI(uri));
261 assertEquals(1, rc.getURIs().size());
262 assertSame(uri, rc.getURIs().get(0));
263
264 assertFalse(rc.addURI(new URIish(uri.toString())));
265 assertEquals(1, rc.getURIs().size());
266 }
267
268 @Test
269 public void testRemoveFirstURI() throws Exception {
270 readConfig("");
271
272 final URIish a = new URIish("/some/dir");
273 final URIish b = new URIish("/another/dir");
274 final URIish c = new URIish("/more/dirs");
275 final RemoteConfig rc = new RemoteConfig(config, "backup");
276 assertTrue(rc.addURI(a));
277 assertTrue(rc.addURI(b));
278 assertTrue(rc.addURI(c));
279
280 assertEquals(3, rc.getURIs().size());
281 assertSame(a, rc.getURIs().get(0));
282 assertSame(b, rc.getURIs().get(1));
283 assertSame(c, rc.getURIs().get(2));
284
285 assertTrue(rc.removeURI(a));
286 assertEquals(2, rc.getURIs().size());
287 assertSame(b, rc.getURIs().get(0));
288 assertSame(c, rc.getURIs().get(1));
289 }
290
291 @Test
292 public void testRemoveMiddleURI() throws Exception {
293 readConfig("");
294
295 final URIish a = new URIish("/some/dir");
296 final URIish b = new URIish("/another/dir");
297 final URIish c = new URIish("/more/dirs");
298 final RemoteConfig rc = new RemoteConfig(config, "backup");
299 assertTrue(rc.addURI(a));
300 assertTrue(rc.addURI(b));
301 assertTrue(rc.addURI(c));
302
303 assertEquals(3, rc.getURIs().size());
304 assertSame(a, rc.getURIs().get(0));
305 assertSame(b, rc.getURIs().get(1));
306 assertSame(c, rc.getURIs().get(2));
307
308 assertTrue(rc.removeURI(b));
309 assertEquals(2, rc.getURIs().size());
310 assertSame(a, rc.getURIs().get(0));
311 assertSame(c, rc.getURIs().get(1));
312 }
313
314 @Test
315 public void testRemoveLastURI() throws Exception {
316 readConfig("");
317
318 final URIish a = new URIish("/some/dir");
319 final URIish b = new URIish("/another/dir");
320 final URIish c = new URIish("/more/dirs");
321 final RemoteConfig rc = new RemoteConfig(config, "backup");
322 assertTrue(rc.addURI(a));
323 assertTrue(rc.addURI(b));
324 assertTrue(rc.addURI(c));
325
326 assertEquals(3, rc.getURIs().size());
327 assertSame(a, rc.getURIs().get(0));
328 assertSame(b, rc.getURIs().get(1));
329 assertSame(c, rc.getURIs().get(2));
330
331 assertTrue(rc.removeURI(c));
332 assertEquals(2, rc.getURIs().size());
333 assertSame(a, rc.getURIs().get(0));
334 assertSame(b, rc.getURIs().get(1));
335 }
336
337 @Test
338 public void testRemoveOnlyURI() throws Exception {
339 readConfig("");
340
341 final URIish a = new URIish("/some/dir");
342 final RemoteConfig rc = new RemoteConfig(config, "backup");
343 assertTrue(rc.addURI(a));
344
345 assertEquals(1, rc.getURIs().size());
346 assertSame(a, rc.getURIs().get(0));
347
348 assertTrue(rc.removeURI(a));
349 assertEquals(0, rc.getURIs().size());
350 }
351
352 @Test
353 public void testCreateOrigin() throws Exception {
354 final RemoteConfig rc = new RemoteConfig(config, "origin");
355 rc.addURI(new URIish("/some/dir"));
356 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
357 + rc.getName() + "/*"));
358 rc.update(config);
359 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
360 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
361 }
362
363 @Test
364 public void testSaveAddURI() throws Exception {
365 readConfig("[remote \"spearce\"]\n"
366 + "url = http://www.spearce.org/egit.git\n"
367 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
368
369 final RemoteConfig rc = new RemoteConfig(config, "spearce");
370 rc.addURI(new URIish("/some/dir"));
371 assertEquals(2, rc.getURIs().size());
372 rc.update(config);
373 checkConfig("[remote \"spearce\"]\n"
374 + "\turl = http://www.spearce.org/egit.git\n"
375 + "\turl = /some/dir\n"
376 + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
377 }
378
379 @Test
380 public void testSaveRemoveLastURI() throws Exception {
381 readConfig("[remote \"spearce\"]\n"
382 + "url = http://www.spearce.org/egit.git\n"
383 + "url = /some/dir\n"
384 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
385
386 final RemoteConfig rc = new RemoteConfig(config, "spearce");
387 assertEquals(2, rc.getURIs().size());
388 rc.removeURI(new URIish("/some/dir"));
389 assertEquals(1, rc.getURIs().size());
390 rc.update(config);
391 checkConfig("[remote \"spearce\"]\n"
392 + "\turl = http://www.spearce.org/egit.git\n"
393 + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
394 }
395
396 @Test
397 public void testSaveRemoveFirstURI() throws Exception {
398 readConfig("[remote \"spearce\"]\n"
399 + "url = http://www.spearce.org/egit.git\n"
400 + "url = /some/dir\n"
401 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
402
403 final RemoteConfig rc = new RemoteConfig(config, "spearce");
404 assertEquals(2, rc.getURIs().size());
405 rc.removeURI(new URIish("http://www.spearce.org/egit.git"));
406 assertEquals(1, rc.getURIs().size());
407 rc.update(config);
408 checkConfig("[remote \"spearce\"]\n" + "\turl = /some/dir\n"
409 + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
410 }
411
412 @Test
413 public void testSaveNoTags() throws Exception {
414 final RemoteConfig rc = new RemoteConfig(config, "origin");
415 rc.addURI(new URIish("/some/dir"));
416 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
417 + rc.getName() + "/*"));
418 rc.setTagOpt(TagOpt.NO_TAGS);
419 rc.update(config);
420 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
421 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
422 + "\ttagopt = --no-tags\n");
423 }
424
425 @Test
426 public void testSaveAllTags() throws Exception {
427 final RemoteConfig rc = new RemoteConfig(config, "origin");
428 rc.addURI(new URIish("/some/dir"));
429 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
430 + rc.getName() + "/*"));
431 rc.setTagOpt(TagOpt.FETCH_TAGS);
432 rc.update(config);
433 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
434 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
435 + "\ttagopt = --tags\n");
436 }
437
438 @Test
439 public void testSimpleTimeout() throws Exception {
440 readConfig("[remote \"spearce\"]\n"
441 + "url = http://www.spearce.org/egit.git\n"
442 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
443 + "timeout = 12\n");
444 final RemoteConfig rc = new RemoteConfig(config, "spearce");
445 assertEquals(12, rc.getTimeout());
446 }
447
448 @Test
449 public void testSaveTimeout() throws Exception {
450 final RemoteConfig rc = new RemoteConfig(config, "origin");
451 rc.addURI(new URIish("/some/dir"));
452 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
453 + rc.getName() + "/*"));
454 rc.setTimeout(60);
455 rc.update(config);
456 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
457 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
458 + "\ttimeout = 60\n");
459 }
460
461 @Test
462 public void noInsteadOf() throws Exception {
463 config.setString("remote", "origin", "url", "short:project.git");
464 config.setString("url", "https://server/repos/", "name", "short:");
465 RemoteConfig rc = new RemoteConfig(config, "origin");
466 assertFalse(rc.getURIs().isEmpty());
467 assertEquals("short:project.git", rc.getURIs().get(0).toASCIIString());
468 }
469
470 @Test
471 public void singleInsteadOf() throws Exception {
472 config.setString("remote", "origin", "url", "short:project.git");
473 config.setString("url", "https://server/repos/", "insteadOf", "short:");
474 RemoteConfig rc = new RemoteConfig(config, "origin");
475 assertFalse(rc.getURIs().isEmpty());
476 assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
477 .toASCIIString());
478 }
479
480 @Test
481 public void multipleInsteadOf() throws Exception {
482 config.setString("remote", "origin", "url", "prefixproject.git");
483 config.setStringList("url", "https://server/repos/", "insteadOf",
484 Arrays.asList("pre", "prefix", "pref", "perf"));
485 RemoteConfig rc = new RemoteConfig(config, "origin");
486 assertFalse(rc.getURIs().isEmpty());
487 assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
488 .toASCIIString());
489 }
490
491 @Test
492 public void noPushInsteadOf() throws Exception {
493 config.setString("remote", "origin", "pushurl", "short:project.git");
494 config.setString("url", "https://server/repos/", "name", "short:");
495 RemoteConfig rc = new RemoteConfig(config, "origin");
496 assertFalse(rc.getPushURIs().isEmpty());
497 assertEquals("short:project.git", rc.getPushURIs().get(0)
498 .toASCIIString());
499 }
500
501 @Test
502 public void pushInsteadOfNotAppliedToPushUri() throws Exception {
503 config.setString("remote", "origin", "pushurl", "short:project.git");
504 config.setString("url", "https://server/repos/", "pushInsteadOf",
505 "short:");
506 RemoteConfig rc = new RemoteConfig(config, "origin");
507 assertFalse(rc.getPushURIs().isEmpty());
508 assertEquals("short:project.git",
509 rc.getPushURIs().get(0).toASCIIString());
510 }
511
512 @Test
513 public void pushInsteadOfAppliedToUri() throws Exception {
514 config.setString("remote", "origin", "url", "short:project.git");
515 config.setString("url", "https://server/repos/", "pushInsteadOf",
516 "short:");
517 RemoteConfig rc = new RemoteConfig(config, "origin");
518 assertFalse(rc.getPushURIs().isEmpty());
519 assertEquals("https://server/repos/project.git",
520 rc.getPushURIs().get(0).toASCIIString());
521 }
522
523 @Test
524 public void multiplePushInsteadOf() throws Exception {
525 config.setString("remote", "origin", "url", "prefixproject.git");
526 config.setStringList("url", "https://server/repos/", "pushInsteadOf",
527 Arrays.asList("pre", "prefix", "pref", "perf"));
528 RemoteConfig rc = new RemoteConfig(config, "origin");
529 assertFalse(rc.getPushURIs().isEmpty());
530 assertEquals("https://server/repos/project.git", rc.getPushURIs()
531 .get(0).toASCIIString());
532 }
533
534 @Test
535 public void pushInsteadOfNoPushUrl() throws Exception {
536 config.setString("remote", "origin", "url",
537 "http://git.eclipse.org/gitroot/jgit/jgit");
538 config.setStringList("url", "ssh://someone@git.eclipse.org:29418/",
539 "pushInsteadOf",
540 Collections.singletonList("http://git.eclipse.org/gitroot/"));
541 RemoteConfig rc = new RemoteConfig(config, "origin");
542 assertFalse(rc.getPushURIs().isEmpty());
543 assertEquals("ssh://someone@git.eclipse.org:29418/jgit/jgit",
544 rc.getPushURIs().get(0).toASCIIString());
545 }
546 }