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
15 import org.eclipse.jgit.api.errors.GitAPIException;
16 import org.eclipse.jgit.api.errors.JGitInternalException;
17 import org.eclipse.jgit.lib.Constants;
18 import org.eclipse.jgit.lib.Repository;
19 import org.eclipse.jgit.lib.StoredConfig;
20 import org.eclipse.jgit.transport.RefSpec;
21 import org.eclipse.jgit.transport.RemoteConfig;
22 import org.eclipse.jgit.transport.URIish;
23
24
25
26
27
28
29
30
31
32
33
34
35 public class RemoteAddCommand extends GitCommand<RemoteConfig> {
36
37 private String name;
38
39 private URIish uri;
40
41
42
43
44
45
46
47 protected RemoteAddCommand(Repository repo) {
48 super(repo);
49 }
50
51
52
53
54
55
56
57
58
59 public RemoteAddCommand setName(String name) {
60 this.name = name;
61 return this;
62 }
63
64
65
66
67
68
69
70
71
72 public RemoteAddCommand setUri(URIish uri) {
73 this.uri = uri;
74 return this;
75 }
76
77
78
79
80
81
82
83 @Override
84 public RemoteConfig call() throws GitAPIException {
85 checkCallable();
86
87 try {
88 StoredConfig config = repo.getConfig();
89 RemoteConfig remote = new RemoteConfig(config, name);
90
91 RefSpec refSpec = new RefSpec();
92 refSpec = refSpec.setForceUpdate(true);
93 refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*",
94 Constants.R_REMOTES + name + "/*");
95 remote.addFetchRefSpec(refSpec);
96
97 remote.addURI(uri);
98
99 remote.update(config);
100 config.save();
101 return remote;
102 } catch (IOException | URISyntaxException e) {
103 throw new JGitInternalException(e.getMessage(), e);
104 }
105
106 }
107
108 }