Add tagopt parsing support to RemoteConfig
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / RemoteConfig.java
blobfe663a9fda1d9fc7905ac8715928ec6bed7749b4
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.transport;
19 import java.net.URISyntaxException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
24 import org.spearce.jgit.lib.RepositoryConfig;
26 /**
27 * A remembered remote repository, including URLs and RefSpecs.
28 * <p>
29 * A remote configuration remembers one or more URLs for a frequently accessed
30 * remote repository as well as zero or more fetch and push specifications
31 * describing how refs should be transferred between this repository and the
32 * remote repository.
34 public class RemoteConfig {
35 private static final String SECTION = "remote";
37 private static final String KEY_URL = "url";
39 private static final String KEY_FETCH = "fetch";
41 private static final String KEY_PUSH = "push";
43 private static final String KEY_UPLOADPACK = "uploadpack";
45 private static final String KEY_RECEIVEPACK = "receivepack";
47 private static final String KEY_TAGOPT = "tagopt";
49 /** Default value for {@link #getUploadPack()} if not specified. */
50 public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack";
52 /** Default value for {@link #getReceivePack()} if not specified. */
53 public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack";
55 private String name;
57 private List<URIish> uris;
59 private List<RefSpec> fetch;
61 private List<RefSpec> push;
63 private String uploadpack;
65 private String receivepack;
67 private TagOpt tagopt;
69 /**
70 * Parse a remote block from an existing configuration file.
71 * <p>
72 * This constructor succeeds even if the requested remote is not defined
73 * within the supplied configuration file. If that occurs then there will be
74 * no URIs and no ref specifications known to the new instance.
76 * @param rc
77 * the existing configuration to get the remote settings from.
78 * The configuration must already be loaded into memory.
79 * @param remoteName
80 * subsection key indicating the name of this remote.
81 * @throws URISyntaxException
82 * one of the URIs within the remote's configuration is invalid.
84 public RemoteConfig(final RepositoryConfig rc, final String remoteName)
85 throws URISyntaxException {
86 name = remoteName;
88 String[] vlst;
89 String val;
91 vlst = rc.getStringList(SECTION, name, KEY_URL);
92 uris = new ArrayList<URIish>(vlst.length);
93 for (final String s : vlst)
94 uris.add(new URIish(s));
96 vlst = rc.getStringList(SECTION, name, KEY_FETCH);
97 fetch = new ArrayList<RefSpec>(vlst.length);
98 for (final String s : vlst)
99 fetch.add(new RefSpec(s));
101 vlst = rc.getStringList(SECTION, name, KEY_PUSH);
102 push = new ArrayList<RefSpec>(vlst.length);
103 for (final String s : vlst)
104 push.add(new RefSpec(s));
106 val = rc.getString(SECTION, name, KEY_UPLOADPACK);
107 if (val == null)
108 val = DEFAULT_UPLOAD_PACK;
109 uploadpack = val;
111 val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
112 if (val == null)
113 val = DEFAULT_RECEIVE_PACK;
114 receivepack = val;
116 val = rc.getString(SECTION, name, KEY_TAGOPT);
117 tagopt = TagOpt.fromOption(val);
121 * Update this remote's definition within the configuration.
123 * @param rc
124 * the configuration file to store ourselves into.
126 public void update(final RepositoryConfig rc) {
127 final List<String> vlst = new ArrayList<String>();
129 vlst.clear();
130 for (final URIish u : getURIs())
131 vlst.add(u.toString());
132 rc.setStringList(SECTION, getName(), KEY_URL, vlst);
134 vlst.clear();
135 for (final RefSpec u : getFetchRefSpecs())
136 vlst.add(u.toString());
137 rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
139 vlst.clear();
140 for (final RefSpec u : getPushRefSpecs())
141 vlst.add(u.toString());
142 rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
144 set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
145 set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
146 set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
149 private void set(final RepositoryConfig rc, final String key,
150 final String currentValue, final String defaultValue) {
151 if (defaultValue.equals(currentValue))
152 rc.unsetString(SECTION, getName(), key);
153 else
154 rc.setString(SECTION, getName(), key, currentValue);
158 * Get the local name this remote configuration is recognized as.
160 * @return name assigned by the user to this configuration block.
162 public String getName() {
163 return name;
167 * Get all configured URIs under this remote.
169 * @return the set of URIs known to this remote.
171 public List<URIish> getURIs() {
172 return Collections.unmodifiableList(uris);
176 * Add a new URI to the end of the list of URIs.
178 * @param toAdd
179 * the new URI to add to this remote.
180 * @return true if the URI was added; false if it already exists.
182 public boolean addURI(final URIish toAdd) {
183 if (uris.contains(toAdd))
184 return false;
185 return uris.add(toAdd);
189 * Remove a URI from the list of URIs.
191 * @param toRemove
192 * the URI to remove from this remote.
193 * @return true if the URI was added; false if it already exists.
195 public boolean removeURI(final URIish toRemove) {
196 return uris.remove(toRemove);
200 * Remembered specifications for fetching from a repository.
202 * @return set of specs used by default when fetching.
204 public List<RefSpec> getFetchRefSpecs() {
205 return Collections.unmodifiableList(fetch);
209 * Add a new fetch RefSpec to this remote.
211 * @param s
212 * the new specification to add.
213 * @return true if the specification was added; false if it already exists.
215 public boolean addFetchRefSpec(final RefSpec s) {
216 if (fetch.contains(s))
217 return false;
218 return fetch.add(s);
222 * Remove a fetch RefSpec from this remote.
224 * @param s
225 * the specification to remove.
226 * @return true if the specification existed and was removed.
228 public boolean removeFetchRefSpec(final RefSpec s) {
229 return fetch.remove(s);
233 * Remembered specifications for pushing to a repository.
235 * @return set of specs used by default when pushing.
237 public List<RefSpec> getPushRefSpecs() {
238 return Collections.unmodifiableList(push);
242 * Add a new push RefSpec to this remote.
244 * @param s
245 * the new specification to add.
246 * @return true if the specification was added; false if it already exists.
248 public boolean addPushRefSpec(final RefSpec s) {
249 if (push.contains(s))
250 return false;
251 return push.add(s);
255 * Remove a push RefSpec from this remote.
257 * @param s
258 * the specification to remove.
259 * @return true if the specification existed and was removed.
261 public boolean removePushRefSpec(final RefSpec s) {
262 return push.remove(s);
266 * Override for the location of 'git-upload-pack' on the remote system.
267 * <p>
268 * This value is only useful for an SSH style connection, where Git is
269 * asking the remote system to execute a program that provides the necessary
270 * network protocol.
272 * @return location of 'git-upload-pack' on the remote system. If no
273 * location has been configured the default of 'git-upload-pack' is
274 * returned instead.
276 public String getUploadPack() {
277 return uploadpack;
281 * Override for the location of 'git-receive-pack' on the remote system.
282 * <p>
283 * This value is only useful for an SSH style connection, where Git is
284 * asking the remote system to execute a program that provides the necessary
285 * network protocol.
287 * @return location of 'git-receive-pack' on the remote system. If no
288 * location has been configured the default of 'git-receive-pack' is
289 * returned instead.
291 public String getReceivePack() {
292 return receivepack;
296 * Get the description of how annotated tags should be treated during fetch.
298 * @return option indicating the behavior of annotated tags in fetch.
300 public TagOpt getTagOpt() {
301 return tagopt;
305 * Set the description of how annotated tags should be treated on fetch.
307 * @param option
308 * method to use when handling annotated tags.
310 public void setTagOpt(final TagOpt option) {
311 tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;