RemoteSetUrlCommand.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 java.util.List;

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

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

  32.     /**
  33.      * The available URI types for the remote.
  34.      *
  35.      * @since 5.3
  36.      */
  37.     public enum UriType {
  38.         /**
  39.          * Fetch URL for the remote.
  40.          */
  41.         FETCH,
  42.         /**
  43.          * Push URL for the remote.
  44.          */
  45.         PUSH
  46.     }


  47.     private String remoteName;

  48.     private URIish remoteUri;

  49.     private UriType type;

  50.     /**
  51.      * <p>
  52.      * Constructor for RemoteSetUrlCommand.
  53.      * </p>
  54.      *
  55.      * @param repo
  56.      *            the {@link org.eclipse.jgit.lib.Repository}
  57.      */
  58.     protected RemoteSetUrlCommand(Repository repo) {
  59.         super(repo);
  60.     }

  61.     /**
  62.      * The name of the remote to change the URL for.
  63.      *
  64.      * @param name
  65.      *            a remote name
  66.      * @deprecated use {@link #setRemoteName} instead
  67.      */
  68.     @Deprecated
  69.     public void setName(String name) {
  70.         this.remoteName = name;
  71.     }

  72.     /**
  73.      * The name of the remote to change the URL for.
  74.      *
  75.      * @param remoteName
  76.      *            a remote remoteName
  77.      * @return {@code this}
  78.      * @since 5.3
  79.      */
  80.     public RemoteSetUrlCommand setRemoteName(String remoteName) {
  81.         this.remoteName = remoteName;
  82.         return this;
  83.     }

  84.     /**
  85.      * The new URL for the remote.
  86.      *
  87.      * @param uri
  88.      *            an URL for the remote
  89.      * @deprecated use {@link #setRemoteUri} instead
  90.      */
  91.     @Deprecated
  92.     public void setUri(URIish uri) {
  93.         this.remoteUri = uri;
  94.     }

  95.     /**
  96.      * The new URL for the remote.
  97.      *
  98.      * @param remoteUri
  99.      *            an URL for the remote
  100.      * @return {@code this}
  101.      * @since 5.3
  102.      */
  103.     public RemoteSetUrlCommand setRemoteUri(URIish remoteUri) {
  104.         this.remoteUri = remoteUri;
  105.         return this;
  106.     }

  107.     /**
  108.      * Whether to change the push URL of the remote instead of the fetch URL.
  109.      *
  110.      * @param push
  111.      *            <code>true</code> to set the push url, <code>false</code> to
  112.      *            set the fetch url
  113.      * @deprecated use {@link #setUriType} instead
  114.      */
  115.     @Deprecated
  116.     public void setPush(boolean push) {
  117.         if (push) {
  118.             setUriType(UriType.PUSH);
  119.         } else {
  120.             setUriType(UriType.FETCH);
  121.         }
  122.     }

  123.     /**
  124.      * Whether to change the push URL of the remote instead of the fetch URL.
  125.      *
  126.      * @param type
  127.      *            the <code>UriType</code> value to set
  128.      * @return {@code this}
  129.      * @since 5.3
  130.      */
  131.     public RemoteSetUrlCommand setUriType(UriType type) {
  132.         this.type = type;
  133.         return this;
  134.     }

  135.     /**
  136.      * {@inheritDoc}
  137.      * <p>
  138.      * Executes the {@code remote} command with all the options and parameters
  139.      * collected by the setter methods of this class.
  140.      */
  141.     @Override
  142.     public RemoteConfig call() throws GitAPIException {
  143.         checkCallable();

  144.         try {
  145.             StoredConfig config = repo.getConfig();
  146.             RemoteConfig remote = new RemoteConfig(config, remoteName);
  147.             if (type == UriType.PUSH) {
  148.                 List<URIish> uris = remote.getPushURIs();
  149.                 if (uris.size() > 1) {
  150.                     throw new JGitInternalException(
  151.                             "remote.newtest.pushurl has multiple values"); //$NON-NLS-1$
  152.                 } else if (uris.size() == 1) {
  153.                     remote.removePushURI(uris.get(0));
  154.                 }
  155.                 remote.addPushURI(remoteUri);
  156.             } else {
  157.                 List<URIish> uris = remote.getURIs();
  158.                 if (uris.size() > 1) {
  159.                     throw new JGitInternalException(
  160.                             "remote.newtest.url has multiple values"); //$NON-NLS-1$
  161.                 } else if (uris.size() == 1) {
  162.                     remote.removeURI(uris.get(0));
  163.                 }
  164.                 remote.addURI(remoteUri);
  165.             }

  166.             remote.update(config);
  167.             config.save();
  168.             return remote;
  169.         } catch (IOException | URISyntaxException e) {
  170.             throw new JGitInternalException(e.getMessage(), e);
  171.         }
  172.     }

  173. }