View Javadoc
1   /*
2    * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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   * Used to change the URL of a remote.
25   *
26   * This class has setters for all supported options and arguments of this
27   * command and a {@link #call()} method to finally execute the command.
28   *
29   * @see <a href=
30   *      "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
31   *      documentation about Remote</a>
32   * @since 4.2
33   */
34  public class RemoteSetUrlCommand extends GitCommand<RemoteConfig> {
35  
36  	/**
37  	 * The available URI types for the remote.
38  	 *
39  	 * @since 5.3
40  	 */
41  	public enum UriType {
42  		/**
43  		 * Fetch URL for the remote.
44  		 */
45  		FETCH,
46  		/**
47  		 * Push URL for the remote.
48  		 */
49  		PUSH
50  	}
51  
52  
53  	private String remoteName;
54  
55  	private URIish remoteUri;
56  
57  	private UriType type;
58  
59  	/**
60  	 * <p>
61  	 * Constructor for RemoteSetUrlCommand.
62  	 * </p>
63  	 *
64  	 * @param repo
65  	 *            the {@link org.eclipse.jgit.lib.Repository}
66  	 */
67  	protected RemoteSetUrlCommand(Repository repo) {
68  		super(repo);
69  	}
70  
71  	/**
72  	 * The name of the remote to change the URL for.
73  	 *
74  	 * @param name
75  	 *            a remote name
76  	 * @deprecated use {@link #setRemoteName} instead
77  	 */
78  	@Deprecated
79  	public void setName(String name) {
80  		this.remoteName = name;
81  	}
82  
83  	/**
84  	 * The name of the remote to change the URL for.
85  	 *
86  	 * @param remoteName
87  	 *            a remote remoteName
88  	 * @return {@code this}
89  	 * @since 5.3
90  	 */
91  	public RemoteSetUrlCommand setRemoteName(String remoteName) {
92  		this.remoteName = remoteName;
93  		return this;
94  	}
95  
96  	/**
97  	 * The new URL for the remote.
98  	 *
99  	 * @param uri
100 	 *            an URL for the remote
101 	 * @deprecated use {@link #setRemoteUri} instead
102 	 */
103 	@Deprecated
104 	public void setUri(URIish uri) {
105 		this.remoteUri = uri;
106 	}
107 
108 	/**
109 	 * The new URL for the remote.
110 	 *
111 	 * @param remoteUri
112 	 *            an URL for the remote
113 	 * @return {@code this}
114 	 * @since 5.3
115 	 */
116 	public RemoteSetUrlCommand setRemoteUri(URIish remoteUri) {
117 		this.remoteUri = remoteUri;
118 		return this;
119 	}
120 
121 	/**
122 	 * Whether to change the push URL of the remote instead of the fetch URL.
123 	 *
124 	 * @param push
125 	 *            <code>true</code> to set the push url, <code>false</code> to
126 	 *            set the fetch url
127 	 * @deprecated use {@link #setUriType} instead
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 	 * Whether to change the push URL of the remote instead of the fetch URL.
140 	 *
141 	 * @param type
142 	 *            the <code>UriType</code> value to set
143 	 * @return {@code this}
144 	 * @since 5.3
145 	 */
146 	public RemoteSetUrlCommand setUriType(UriType type) {
147 		this.type = type;
148 		return this;
149 	}
150 
151 	/**
152 	 * {@inheritDoc}
153 	 * <p>
154 	 * Executes the {@code remote} command with all the options and parameters
155 	 * collected by the setter methods of this class.
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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 }