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  
15  import org.eclipse.jgit.api.errors.GitAPIException;
16  import org.eclipse.jgit.api.errors.JGitInternalException;
17  import org.eclipse.jgit.lib.ConfigConstants;
18  import org.eclipse.jgit.lib.Repository;
19  import org.eclipse.jgit.lib.StoredConfig;
20  import org.eclipse.jgit.transport.RemoteConfig;
21  
22  /**
23   * Used to remove an existing remote.
24   *
25   * This class has setters for all supported options and arguments of this
26   * command and a {@link #call()} method to finally execute the command.
27   *
28   * @see <a href=
29   *      "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
30   *      documentation about Remote</a>
31   * @since 4.2
32   */
33  public class RemoteRemoveCommand extends GitCommand<RemoteConfig> {
34  
35  	private String remoteName;
36  
37  	/**
38  	 * <p>
39  	 * Constructor for RemoteRemoveCommand.
40  	 * </p>
41  	 *
42  	 * @param repo
43  	 *            the {@link org.eclipse.jgit.lib.Repository}
44  	 */
45  	protected RemoteRemoveCommand(Repository repo) {
46  		super(repo);
47  	}
48  
49  	/**
50  	 * The name of the remote to remove.
51  	 *
52  	 * @param name
53  	 *            a remote name
54  	 * @deprecated use {@link #setRemoteName} instead
55  	 */
56  	@Deprecated
57  	public void setName(String name) {
58  		this.remoteName = name;
59  	}
60  
61  	/**
62  	 * The name of the remote to remove.
63  	 *
64  	 * @param remoteName
65  	 *            a remote name
66  	 * @return {@code this}
67  	 * @since 5.3
68  	 */
69  	public RemoteRemoveCommand setRemoteName(String remoteName) {
70  		this.remoteName = remoteName;
71  		return this;
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 * <p>
77  	 * Executes the {@code remote} command with all the options and parameters
78  	 * collected by the setter methods of this class.
79  	 */
80  	@Override
81  	public RemoteConfig call() throws GitAPIException {
82  		checkCallable();
83  
84  		try {
85  			StoredConfig config = repo.getConfig();
86  			RemoteConfig remote = new RemoteConfig(config, remoteName);
87  			config.unsetSection(ConfigConstants.CONFIG_KEY_REMOTE, remoteName);
88  			config.save();
89  			return remote;
90  		} catch (IOException | URISyntaxException e) {
91  			throw new JGitInternalException(e.getMessage(), e);
92  		}
93  
94  	}
95  
96  }