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.Constants;
18  import org.eclipse.jgit.lib.Repository;
19  import org.eclipse.jgit.lib.StoredConfig;
20  import org.eclipse.jgit.transport.RefSpec;
21  import org.eclipse.jgit.transport.RemoteConfig;
22  import org.eclipse.jgit.transport.URIish;
23  
24  /**
25   * Used to add a new remote.
26   *
27   * This class has setters for all supported options and arguments of this
28   * command and a {@link #call()} method to finally execute the command.
29   *
30   * @see <a href=
31   *      "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
32   *      documentation about Remote</a>
33   * @since 4.2
34   */
35  public class RemoteAddCommand extends GitCommand<RemoteConfig> {
36  
37  	private String name;
38  
39  	private URIish uri;
40  
41  	/**
42  	 * Constructor for RemoteAddCommand.
43  	 *
44  	 * @param repo
45  	 *            the {@link org.eclipse.jgit.lib.Repository}
46  	 */
47  	protected RemoteAddCommand(Repository repo) {
48  		super(repo);
49  	}
50  
51  	/**
52  	 * The name of the remote to add.
53  	 *
54  	 * @param name
55  	 *            a remote name
56  	 * @return this instance
57  	 * @since 5.0
58  	 */
59  	public RemoteAddCommand setName(String name) {
60  		this.name = name;
61  		return this;
62  	}
63  
64  	/**
65  	 * The URL of the repository for the new remote.
66  	 *
67  	 * @param uri
68  	 *            an URL for the remote
69  	 * @return this instance
70  	 * @since 5.0
71  	 */
72  	public RemoteAddCommand setUri(URIish uri) {
73  		this.uri = uri;
74  		return this;
75  	}
76  
77  	/**
78  	 * {@inheritDoc}
79  	 * <p>
80  	 * Executes the {@code remote add} command with all the options and
81  	 * parameters collected by the setter methods of this class.
82  	 */
83  	@Override
84  	public RemoteConfig call() throws GitAPIException {
85  		checkCallable();
86  
87  		try {
88  			StoredConfig config = repo.getConfig();
89  			RemoteConfig remote = new RemoteConfig(config, name);
90  
91  			RefSpec refSpec = new RefSpec();
92  			refSpec = refSpec.setForceUpdate(true);
93  			refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", //$NON-NLS-1$
94  					Constants.R_REMOTES + name + "/*"); //$NON-NLS-1$
95  			remote.addFetchRefSpec(refSpec);
96  
97  			remote.addURI(uri);
98  
99  			remote.update(config);
100 			config.save();
101 			return remote;
102 		} catch (IOException | URISyntaxException e) {
103 			throw new JGitInternalException(e.getMessage(), e);
104 		}
105 
106 	}
107 
108 }