RemoteRemoveCommand.java

  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. import java.io.IOException;
  12. import java.net.URISyntaxException;

  13. import org.eclipse.jgit.api.errors.GitAPIException;
  14. import org.eclipse.jgit.api.errors.JGitInternalException;
  15. import org.eclipse.jgit.lib.ConfigConstants;
  16. import org.eclipse.jgit.lib.Repository;
  17. import org.eclipse.jgit.lib.StoredConfig;
  18. import org.eclipse.jgit.transport.RemoteConfig;

  19. /**
  20.  * Used to remove an existing remote.
  21.  *
  22.  * This class has setters for all supported options and arguments of this
  23.  * command and a {@link #call()} method to finally execute the command.
  24.  *
  25.  * @see <a href=
  26.  *      "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
  27.  *      documentation about Remote</a>
  28.  * @since 4.2
  29.  */
  30. public class RemoteRemoveCommand extends GitCommand<RemoteConfig> {

  31.     private String remoteName;

  32.     /**
  33.      * <p>
  34.      * Constructor for RemoteRemoveCommand.
  35.      * </p>
  36.      *
  37.      * @param repo
  38.      *            the {@link org.eclipse.jgit.lib.Repository}
  39.      */
  40.     protected RemoteRemoveCommand(Repository repo) {
  41.         super(repo);
  42.     }

  43.     /**
  44.      * The name of the remote to remove.
  45.      *
  46.      * @param name
  47.      *            a remote name
  48.      * @deprecated use {@link #setRemoteName} instead
  49.      */
  50.     @Deprecated
  51.     public void setName(String name) {
  52.         this.remoteName = name;
  53.     }

  54.     /**
  55.      * The name of the remote to remove.
  56.      *
  57.      * @param remoteName
  58.      *            a remote name
  59.      * @return {@code this}
  60.      * @since 5.3
  61.      */
  62.     public RemoteRemoveCommand setRemoteName(String remoteName) {
  63.         this.remoteName = remoteName;
  64.         return this;
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      * <p>
  69.      * Executes the {@code remote} command with all the options and parameters
  70.      * collected by the setter methods of this class.
  71.      */
  72.     @Override
  73.     public RemoteConfig call() throws GitAPIException {
  74.         checkCallable();

  75.         try {
  76.             StoredConfig config = repo.getConfig();
  77.             RemoteConfig remote = new RemoteConfig(config, remoteName);
  78.             config.unsetSection(ConfigConstants.CONFIG_KEY_REMOTE, remoteName);
  79.             config.save();
  80.             return remote;
  81.         } catch (IOException | URISyntaxException e) {
  82.             throw new JGitInternalException(e.getMessage(), e);
  83.         }

  84.     }

  85. }