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 import java.util.List;
15
16 import org.eclipse.jgit.api.errors.GitAPIException;
17 import org.eclipse.jgit.api.errors.JGitInternalException;
18 import org.eclipse.jgit.lib.Repository;
19 import org.eclipse.jgit.lib.StoredConfig;
20 import org.eclipse.jgit.transport.RemoteConfig;
21 import org.eclipse.jgit.transport.URIish;
22
23
24
25
26
27
28
29
30
31
32
33
34 public class RemoteSetUrlCommand extends GitCommand<RemoteConfig> {
35
36
37
38
39
40
41 public enum UriType {
42
43
44
45 FETCH,
46
47
48
49 PUSH
50 }
51
52
53 private String remoteName;
54
55 private URIish remoteUri;
56
57 private UriType type;
58
59
60
61
62
63
64
65
66
67 protected RemoteSetUrlCommand(Repository repo) {
68 super(repo);
69 }
70
71
72
73
74
75
76
77
78 @Deprecated
79 public void setName(String name) {
80 this.remoteName = name;
81 }
82
83
84
85
86
87
88
89
90
91 public RemoteSetUrlCommand setRemoteName(String remoteName) {
92 this.remoteName = remoteName;
93 return this;
94 }
95
96
97
98
99
100
101
102
103 @Deprecated
104 public void setUri(URIish uri) {
105 this.remoteUri = uri;
106 }
107
108
109
110
111
112
113
114
115
116 public RemoteSetUrlCommand setRemoteUri(URIish remoteUri) {
117 this.remoteUri = remoteUri;
118 return this;
119 }
120
121
122
123
124
125
126
127
128
129 @Deprecated
130 public void setPush(boolean push) {
131 if (push) {
132 setUriType(UriType.PUSH);
133 } else {
134 setUriType(UriType.FETCH);
135 }
136 }
137
138
139
140
141
142
143
144
145
146 public RemoteSetUrlCommand setUriType(UriType type) {
147 this.type = type;
148 return this;
149 }
150
151
152
153
154
155
156
157 @Override
158 public RemoteConfig call() throws GitAPIException {
159 checkCallable();
160
161 try {
162 StoredConfig config = repo.getConfig();
163 RemoteConfig remote = new RemoteConfig(config, remoteName);
164 if (type == UriType.PUSH) {
165 List<URIish> uris = remote.getPushURIs();
166 if (uris.size() > 1) {
167 throw new JGitInternalException(
168 "remote.newtest.pushurl has multiple values");
169 } else if (uris.size() == 1) {
170 remote.removePushURI(uris.get(0));
171 }
172 remote.addPushURI(remoteUri);
173 } else {
174 List<URIish> uris = remote.getURIs();
175 if (uris.size() > 1) {
176 throw new JGitInternalException(
177 "remote.newtest.url has multiple values");
178 } else if (uris.size() == 1) {
179 remote.removeURI(uris.get(0));
180 }
181 remote.addURI(remoteUri);
182 }
183
184 remote.update(config);
185 config.save();
186 return remote;
187 } catch (IOException | URISyntaxException e) {
188 throw new JGitInternalException(e.getMessage(), e);
189 }
190 }
191
192 }