Fix RemoteRefUpdate to delete local tracking ref upon successful deletion
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / RemoteConfig.java
blob5bbf664ca320c817e500d0cf75d97993e06063b7
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 * Override existing fetch specifications with new ones.
272 * @param specs
273 * list of fetch specifications to set. List is copied, it can be
274 * modified after this call.
276 public void setFetchRefSpecs(final List<RefSpec> specs) {
277 fetch.clear();
278 fetch.addAll(specs);
282 * Override existing push specifications with new ones.
284 * @param specs
285 * list of push specifications to set. List is copied, it can be
286 * modified after this call.
288 public void setPushRefSpecs(final List<RefSpec> specs) {
289 push.clear();
290 push.addAll(specs);
294 * Remove a fetch RefSpec from this remote.
296 * @param s
297 * the specification to remove.
298 * @return true if the specification existed and was removed.
300 public boolean removeFetchRefSpec(final RefSpec s) {
301 return fetch.remove(s);
305 * Remembered specifications for pushing to a repository.
307 * @return set of specs used by default when pushing.
309 public List<RefSpec> getPushRefSpecs() {
310 return Collections.unmodifiableList(push);
314 * Add a new push RefSpec to this remote.
316 * @param s
317 * the new specification to add.
318 * @return true if the specification was added; false if it already exists.
320 public boolean addPushRefSpec(final RefSpec s) {
321 if (push.contains(s))
322 return false;
323 return push.add(s);
327 * Remove a push RefSpec from this remote.
329 * @param s
330 * the specification to remove.
331 * @return true if the specification existed and was removed.
333 public boolean removePushRefSpec(final RefSpec s) {
334 return push.remove(s);
338 * Override for the location of 'git-upload-pack' on the remote system.
339 * <p>
340 * This value is only useful for an SSH style connection, where Git is
341 * asking the remote system to execute a program that provides the necessary
342 * network protocol.
344 * @return location of 'git-upload-pack' on the remote system. If no
345 * location has been configured the default of 'git-upload-pack' is
346 * returned instead.
348 public String getUploadPack() {
349 return uploadpack;
353 * Override for the location of 'git-receive-pack' on the remote system.
354 * <p>
355 * This value is only useful for an SSH style connection, where Git is
356 * asking the remote system to execute a program that provides the necessary
357 * network protocol.
359 * @return location of 'git-receive-pack' on the remote system. If no
360 * location has been configured the default of 'git-receive-pack' is
361 * returned instead.
363 public String getReceivePack() {
364 return receivepack;
368 * Get the description of how annotated tags should be treated during fetch.
370 * @return option indicating the behavior of annotated tags in fetch.
372 public TagOpt getTagOpt() {
373 return tagopt;
377 * Set the description of how annotated tags should be treated on fetch.
379 * @param option
380 * method to use when handling annotated tags.
382 public void setTagOpt(final TagOpt option) {
383 tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;