Remove System.out.println from RevWalkFilterTest
[egit/chris.git] / org.spearce.jgit / src / org / spearce / jgit / transport / RemoteConfig.java
blob519a8a52534c3960a7f3650d09b8aa02f58623a8
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 private static final String KEY_MIRROR = "mirror";
73 private static final boolean DEFAULT_MIRROR = false;
75 /** Default value for {@link #getUploadPack()} if not specified. */
76 public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack";
78 /** Default value for {@link #getReceivePack()} if not specified. */
79 public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack";
81 /**
82 * Parse all remote blocks in an existing configuration file, looking for
83 * remotes configuration.
85 * @param rc
86 * the existing configuration to get the remote settings from.
87 * The configuration must already be loaded into memory.
88 * @return all remotes configurations existing in provided repository
89 * configuration. Returned configurations are ordered
90 * lexicographically by names.
91 * @throws URISyntaxException
92 * one of the URIs within the remote's configuration is invalid.
94 public static List<RemoteConfig> getAllRemoteConfigs(
95 final RepositoryConfig rc) throws URISyntaxException {
96 final List<String> names = new ArrayList<String>(rc
97 .getSubsections(SECTION));
98 Collections.sort(names);
100 final List<RemoteConfig> result = new ArrayList<RemoteConfig>(names
101 .size());
102 for (final String name : names)
103 result.add(new RemoteConfig(rc, name));
104 return result;
107 private String name;
109 private List<URIish> uris;
111 private List<RefSpec> fetch;
113 private List<RefSpec> push;
115 private String uploadpack;
117 private String receivepack;
119 private TagOpt tagopt;
121 private boolean mirror;
124 * Parse a remote block from an existing configuration file.
125 * <p>
126 * This constructor succeeds even if the requested remote is not defined
127 * within the supplied configuration file. If that occurs then there will be
128 * no URIs and no ref specifications known to the new instance.
130 * @param rc
131 * the existing configuration to get the remote settings from.
132 * The configuration must already be loaded into memory.
133 * @param remoteName
134 * subsection key indicating the name of this remote.
135 * @throws URISyntaxException
136 * one of the URIs within the remote's configuration is invalid.
138 public RemoteConfig(final RepositoryConfig rc, final String remoteName)
139 throws URISyntaxException {
140 name = remoteName;
142 String[] vlst;
143 String val;
145 vlst = rc.getStringList(SECTION, name, KEY_URL);
146 uris = new ArrayList<URIish>(vlst.length);
147 for (final String s : vlst)
148 uris.add(new URIish(s));
150 vlst = rc.getStringList(SECTION, name, KEY_FETCH);
151 fetch = new ArrayList<RefSpec>(vlst.length);
152 for (final String s : vlst)
153 fetch.add(new RefSpec(s));
155 vlst = rc.getStringList(SECTION, name, KEY_PUSH);
156 push = new ArrayList<RefSpec>(vlst.length);
157 for (final String s : vlst)
158 push.add(new RefSpec(s));
160 val = rc.getString(SECTION, name, KEY_UPLOADPACK);
161 if (val == null)
162 val = DEFAULT_UPLOAD_PACK;
163 uploadpack = val;
165 val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
166 if (val == null)
167 val = DEFAULT_RECEIVE_PACK;
168 receivepack = val;
170 val = rc.getString(SECTION, name, KEY_TAGOPT);
171 tagopt = TagOpt.fromOption(val);
172 mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
176 * Update this remote's definition within the configuration.
178 * @param rc
179 * the configuration file to store ourselves into.
181 public void update(final RepositoryConfig rc) {
182 final List<String> vlst = new ArrayList<String>();
184 vlst.clear();
185 for (final URIish u : getURIs())
186 vlst.add(u.toPrivateString());
187 rc.setStringList(SECTION, getName(), KEY_URL, vlst);
189 vlst.clear();
190 for (final RefSpec u : getFetchRefSpecs())
191 vlst.add(u.toString());
192 rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
194 vlst.clear();
195 for (final RefSpec u : getPushRefSpecs())
196 vlst.add(u.toString());
197 rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
199 set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
200 set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
201 set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
202 set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
205 private void set(final RepositoryConfig rc, final String key,
206 final String currentValue, final String defaultValue) {
207 if (defaultValue.equals(currentValue))
208 unset(rc, key);
209 else
210 rc.setString(SECTION, getName(), key, currentValue);
213 private void set(final RepositoryConfig rc, final String key,
214 final boolean currentValue, final boolean defaultValue) {
215 if (defaultValue == currentValue)
216 unset(rc, key);
217 else
218 rc.setBoolean(SECTION, getName(), key, currentValue);
221 private void unset(final RepositoryConfig rc, final String key) {
222 rc.unsetString(SECTION, getName(), key);
226 * Get the local name this remote configuration is recognized as.
228 * @return name assigned by the user to this configuration block.
230 public String getName() {
231 return name;
235 * Get all configured URIs under this remote.
237 * @return the set of URIs known to this remote.
239 public List<URIish> getURIs() {
240 return Collections.unmodifiableList(uris);
244 * Add a new URI to the end of the list of URIs.
246 * @param toAdd
247 * the new URI to add to this remote.
248 * @return true if the URI was added; false if it already exists.
250 public boolean addURI(final URIish toAdd) {
251 if (uris.contains(toAdd))
252 return false;
253 return uris.add(toAdd);
257 * Remove a URI from the list of URIs.
259 * @param toRemove
260 * the URI to remove from this remote.
261 * @return true if the URI was added; false if it already exists.
263 public boolean removeURI(final URIish toRemove) {
264 return uris.remove(toRemove);
268 * Remembered specifications for fetching from a repository.
270 * @return set of specs used by default when fetching.
272 public List<RefSpec> getFetchRefSpecs() {
273 return Collections.unmodifiableList(fetch);
277 * Add a new fetch RefSpec to this remote.
279 * @param s
280 * the new specification to add.
281 * @return true if the specification was added; false if it already exists.
283 public boolean addFetchRefSpec(final RefSpec s) {
284 if (fetch.contains(s))
285 return false;
286 return fetch.add(s);
290 * Override existing fetch specifications with new ones.
292 * @param specs
293 * list of fetch specifications to set. List is copied, it can be
294 * modified after this call.
296 public void setFetchRefSpecs(final List<RefSpec> specs) {
297 fetch.clear();
298 fetch.addAll(specs);
302 * Override existing push specifications with new ones.
304 * @param specs
305 * list of push specifications to set. List is copied, it can be
306 * modified after this call.
308 public void setPushRefSpecs(final List<RefSpec> specs) {
309 push.clear();
310 push.addAll(specs);
314 * Remove a fetch RefSpec from this remote.
316 * @param s
317 * the specification to remove.
318 * @return true if the specification existed and was removed.
320 public boolean removeFetchRefSpec(final RefSpec s) {
321 return fetch.remove(s);
325 * Remembered specifications for pushing to a repository.
327 * @return set of specs used by default when pushing.
329 public List<RefSpec> getPushRefSpecs() {
330 return Collections.unmodifiableList(push);
334 * Add a new push RefSpec to this remote.
336 * @param s
337 * the new specification to add.
338 * @return true if the specification was added; false if it already exists.
340 public boolean addPushRefSpec(final RefSpec s) {
341 if (push.contains(s))
342 return false;
343 return push.add(s);
347 * Remove a push RefSpec from this remote.
349 * @param s
350 * the specification to remove.
351 * @return true if the specification existed and was removed.
353 public boolean removePushRefSpec(final RefSpec s) {
354 return push.remove(s);
358 * Override for the location of 'git-upload-pack' on the remote system.
359 * <p>
360 * This value is only useful for an SSH style connection, where Git is
361 * asking the remote system to execute a program that provides the necessary
362 * network protocol.
364 * @return location of 'git-upload-pack' on the remote system. If no
365 * location has been configured the default of 'git-upload-pack' is
366 * returned instead.
368 public String getUploadPack() {
369 return uploadpack;
373 * Override for the location of 'git-receive-pack' on the remote system.
374 * <p>
375 * This value is only useful for an SSH style connection, where Git is
376 * asking the remote system to execute a program that provides the necessary
377 * network protocol.
379 * @return location of 'git-receive-pack' on the remote system. If no
380 * location has been configured the default of 'git-receive-pack' is
381 * returned instead.
383 public String getReceivePack() {
384 return receivepack;
388 * Get the description of how annotated tags should be treated during fetch.
390 * @return option indicating the behavior of annotated tags in fetch.
392 public TagOpt getTagOpt() {
393 return tagopt;
397 * Set the description of how annotated tags should be treated on fetch.
399 * @param option
400 * method to use when handling annotated tags.
402 public void setTagOpt(final TagOpt option) {
403 tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
407 * @return true if pushing to the remote automatically deletes remote refs
408 * which don't exist on the source side.
410 public boolean isMirror() {
411 return mirror;
415 * Set the mirror flag to automatically delete remote refs.
417 * @param m
418 * true to automatically delete remote refs during push.
420 public void setMirror(final boolean m) {
421 mirror = m;