Add getAllRemoteConfigs() to RemoteConfig
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / RemoteConfig.java
blobcde5d4369ac26484aefa7a0191abe21573ce605e
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.transport;
41 import java.net.URISyntaxException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
46 import org.spearce.jgit.lib.RepositoryConfig;
48 /**
49 * A remembered remote repository, including URLs and RefSpecs.
50 * <p>
51 * A remote configuration remembers one or more URLs for a frequently accessed
52 * remote repository as well as zero or more fetch and push specifications
53 * describing how refs should be transferred between this repository and the
54 * remote repository.
56 public class RemoteConfig {
57 private static final String SECTION = "remote";
59 private static final String KEY_URL = "url";
61 private static final String KEY_FETCH = "fetch";
63 private static final String KEY_PUSH = "push";
65 private static final String KEY_UPLOADPACK = "uploadpack";
67 private static final String KEY_RECEIVEPACK = "receivepack";
69 private static final String KEY_TAGOPT = "tagopt";
71 /** Default value for {@link #getUploadPack()} if not specified. */
72 public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack";
74 /** Default value for {@link #getReceivePack()} if not specified. */
75 public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack";
77 /**
78 * Parse all remote blocks in an existing configuration file, looking for
79 * remotes configuration.
81 * @param rc
82 * the existing configuration to get the remote settings from.
83 * The configuration must already be loaded into memory.
84 * @return all remotes configurations existing in provided repository
85 * configuration. Returned configurations are ordered
86 * lexicographically by names.
87 * @throws URISyntaxException
88 * one of the URIs within the remote's configuration is invalid.
90 public static List<RemoteConfig> getAllRemoteConfigs(
91 final RepositoryConfig rc) throws URISyntaxException {
92 final List<String> names = new ArrayList<String>(rc
93 .getSubsections(SECTION));
94 Collections.sort(names);
96 final List<RemoteConfig> result = new ArrayList<RemoteConfig>(names
97 .size());
98 for (final String name : names)
99 result.add(new RemoteConfig(rc, name));
100 return result;
103 private String name;
105 private List<URIish> uris;
107 private List<RefSpec> fetch;
109 private List<RefSpec> push;
111 private String uploadpack;
113 private String receivepack;
115 private TagOpt tagopt;
118 * Parse a remote block from an existing configuration file.
119 * <p>
120 * This constructor succeeds even if the requested remote is not defined
121 * within the supplied configuration file. If that occurs then there will be
122 * no URIs and no ref specifications known to the new instance.
124 * @param rc
125 * the existing configuration to get the remote settings from.
126 * The configuration must already be loaded into memory.
127 * @param remoteName
128 * subsection key indicating the name of this remote.
129 * @throws URISyntaxException
130 * one of the URIs within the remote's configuration is invalid.
132 public RemoteConfig(final RepositoryConfig rc, final String remoteName)
133 throws URISyntaxException {
134 name = remoteName;
136 String[] vlst;
137 String val;
139 vlst = rc.getStringList(SECTION, name, KEY_URL);
140 uris = new ArrayList<URIish>(vlst.length);
141 for (final String s : vlst)
142 uris.add(new URIish(s));
144 vlst = rc.getStringList(SECTION, name, KEY_FETCH);
145 fetch = new ArrayList<RefSpec>(vlst.length);
146 for (final String s : vlst)
147 fetch.add(new RefSpec(s));
149 vlst = rc.getStringList(SECTION, name, KEY_PUSH);
150 push = new ArrayList<RefSpec>(vlst.length);
151 for (final String s : vlst)
152 push.add(new RefSpec(s));
154 val = rc.getString(SECTION, name, KEY_UPLOADPACK);
155 if (val == null)
156 val = DEFAULT_UPLOAD_PACK;
157 uploadpack = val;
159 val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
160 if (val == null)
161 val = DEFAULT_RECEIVE_PACK;
162 receivepack = val;
164 val = rc.getString(SECTION, name, KEY_TAGOPT);
165 tagopt = TagOpt.fromOption(val);
169 * Update this remote's definition within the configuration.
171 * @param rc
172 * the configuration file to store ourselves into.
174 public void update(final RepositoryConfig rc) {
175 final List<String> vlst = new ArrayList<String>();
177 vlst.clear();
178 for (final URIish u : getURIs())
179 vlst.add(u.toPrivateString());
180 rc.setStringList(SECTION, getName(), KEY_URL, vlst);
182 vlst.clear();
183 for (final RefSpec u : getFetchRefSpecs())
184 vlst.add(u.toString());
185 rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
187 vlst.clear();
188 for (final RefSpec u : getPushRefSpecs())
189 vlst.add(u.toString());
190 rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
192 set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
193 set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
194 set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
197 private void set(final RepositoryConfig rc, final String key,
198 final String currentValue, final String defaultValue) {
199 if (defaultValue.equals(currentValue))
200 rc.unsetString(SECTION, getName(), key);
201 else
202 rc.setString(SECTION, getName(), key, currentValue);
206 * Get the local name this remote configuration is recognized as.
208 * @return name assigned by the user to this configuration block.
210 public String getName() {
211 return name;
215 * Get all configured URIs under this remote.
217 * @return the set of URIs known to this remote.
219 public List<URIish> getURIs() {
220 return Collections.unmodifiableList(uris);
224 * Add a new URI to the end of the list of URIs.
226 * @param toAdd
227 * the new URI to add to this remote.
228 * @return true if the URI was added; false if it already exists.
230 public boolean addURI(final URIish toAdd) {
231 if (uris.contains(toAdd))
232 return false;
233 return uris.add(toAdd);
237 * Remove a URI from the list of URIs.
239 * @param toRemove
240 * the URI to remove from this remote.
241 * @return true if the URI was added; false if it already exists.
243 public boolean removeURI(final URIish toRemove) {
244 return uris.remove(toRemove);
248 * Remembered specifications for fetching from a repository.
250 * @return set of specs used by default when fetching.
252 public List<RefSpec> getFetchRefSpecs() {
253 return Collections.unmodifiableList(fetch);
257 * Add a new fetch RefSpec to this remote.
259 * @param s
260 * the new specification to add.
261 * @return true if the specification was added; false if it already exists.
263 public boolean addFetchRefSpec(final RefSpec s) {
264 if (fetch.contains(s))
265 return false;
266 return fetch.add(s);
270 * Remove a fetch RefSpec from this remote.
272 * @param s
273 * the specification to remove.
274 * @return true if the specification existed and was removed.
276 public boolean removeFetchRefSpec(final RefSpec s) {
277 return fetch.remove(s);
281 * Remembered specifications for pushing to a repository.
283 * @return set of specs used by default when pushing.
285 public List<RefSpec> getPushRefSpecs() {
286 return Collections.unmodifiableList(push);
290 * Add a new push RefSpec to this remote.
292 * @param s
293 * the new specification to add.
294 * @return true if the specification was added; false if it already exists.
296 public boolean addPushRefSpec(final RefSpec s) {
297 if (push.contains(s))
298 return false;
299 return push.add(s);
303 * Remove a push RefSpec from this remote.
305 * @param s
306 * the specification to remove.
307 * @return true if the specification existed and was removed.
309 public boolean removePushRefSpec(final RefSpec s) {
310 return push.remove(s);
314 * Override for the location of 'git-upload-pack' on the remote system.
315 * <p>
316 * This value is only useful for an SSH style connection, where Git is
317 * asking the remote system to execute a program that provides the necessary
318 * network protocol.
320 * @return location of 'git-upload-pack' on the remote system. If no
321 * location has been configured the default of 'git-upload-pack' is
322 * returned instead.
324 public String getUploadPack() {
325 return uploadpack;
329 * Override for the location of 'git-receive-pack' on the remote system.
330 * <p>
331 * This value is only useful for an SSH style connection, where Git is
332 * asking the remote system to execute a program that provides the necessary
333 * network protocol.
335 * @return location of 'git-receive-pack' on the remote system. If no
336 * location has been configured the default of 'git-receive-pack' is
337 * returned instead.
339 public String getReceivePack() {
340 return receivepack;
344 * Get the description of how annotated tags should be treated during fetch.
346 * @return option indicating the behavior of annotated tags in fetch.
348 public TagOpt getTagOpt() {
349 return tagopt;
353 * Set the description of how annotated tags should be treated on fetch.
355 * @param option
356 * method to use when handling annotated tags.
358 public void setTagOpt(final TagOpt option) {
359 tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;