RemoteAddCommand.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.Constants;
  16. import org.eclipse.jgit.lib.Repository;
  17. import org.eclipse.jgit.lib.StoredConfig;
  18. import org.eclipse.jgit.transport.RefSpec;
  19. import org.eclipse.jgit.transport.RemoteConfig;
  20. import org.eclipse.jgit.transport.URIish;

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

  33.     private String name;

  34.     private URIish uri;

  35.     /**
  36.      * Constructor for RemoteAddCommand.
  37.      *
  38.      * @param repo
  39.      *            the {@link org.eclipse.jgit.lib.Repository}
  40.      */
  41.     protected RemoteAddCommand(Repository repo) {
  42.         super(repo);
  43.     }

  44.     /**
  45.      * The name of the remote to add.
  46.      *
  47.      * @param name
  48.      *            a remote name
  49.      * @return this instance
  50.      * @since 5.0
  51.      */
  52.     public RemoteAddCommand setName(String name) {
  53.         this.name = name;
  54.         return this;
  55.     }

  56.     /**
  57.      * The URL of the repository for the new remote.
  58.      *
  59.      * @param uri
  60.      *            an URL for the remote
  61.      * @return this instance
  62.      * @since 5.0
  63.      */
  64.     public RemoteAddCommand setUri(URIish uri) {
  65.         this.uri = uri;
  66.         return this;
  67.     }

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

  77.         try {
  78.             StoredConfig config = repo.getConfig();
  79.             RemoteConfig remote = new RemoteConfig(config, name);

  80.             RefSpec refSpec = new RefSpec();
  81.             refSpec = refSpec.setForceUpdate(true);
  82.             refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", //$NON-NLS-1$
  83.                     Constants.R_REMOTES + name + "/*"); //$NON-NLS-1$
  84.             remote.addFetchRefSpec(refSpec);

  85.             remote.addURI(uri);

  86.             remote.update(config);
  87.             config.save();
  88.             return remote;
  89.         } catch (IOException | URISyntaxException e) {
  90.             throw new JGitInternalException(e.getMessage(), e);
  91.         }

  92.     }

  93. }