Browse Source

Add remote command to JGit CLI

Supported subcommands are:
 - <none> (lists available remotes)
 - add
 - remove
 - set-url
 - update
 
Supported options are:
 --verbose
 --push
 --prune

Bug: 481316
Change-Id: I57c34ed6daabb7d308bc383b17c1ef4af433e714
Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.3
Kaloyan Raev 11 years ago committed by Matthias Sohn
parent
commit
e23521f0d9
  1. 159
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RemoteTest.java
  2. 1
      org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin
  3. 3
      org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
  4. 197
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java
  5. 1
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java
  6. 96
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AbstractRemoteCommandTest.java
  7. 81
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteAddCommandTest.java
  8. 68
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteDeleteCommandTest.java
  9. 68
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteListCommandTest.java
  10. 100
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteSetUrlCommandTest.java
  11. 44
      org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java
  12. 133
      org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteAddCommand.java
  13. 91
      org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteListCommand.java
  14. 110
      org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteRemoveCommand.java
  15. 155
      org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteSetUrlCommand.java
  16. 2
      org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

159
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RemoteTest.java

@ -0,0 +1,159 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.pgm;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.CLIRepositoryTestCase;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.junit.Before;
import org.junit.Test;
public class RemoteTest extends CLIRepositoryTestCase {
private StoredConfig config;
private RemoteConfig remote;
@Before
@Override
public void setUp() throws Exception {
super.setUp();
// create another repository
Repository remoteRepository = createWorkRepository();
// set it up as a remote to this repository
config = db.getConfig();
remote = new RemoteConfig(config, "test");
remote.addFetchRefSpec(
new RefSpec("+refs/heads/*:refs/remotes/test/*"));
URIish uri = new URIish(
remoteRepository.getDirectory().toURI().toURL());
remote.addURI(uri);
remote.update(config);
config.save();
Git.wrap(remoteRepository).commit().setMessage("initial commit").call();
}
@Test
public void testList() throws Exception {
assertArrayEquals(new String[] { remote.getName(), "" },
execute("git remote"));
}
@Test
public void testVerboseList() throws Exception {
assertArrayEquals(
new String[] {
String.format("%s\t%s (fetch)", remote.getName(),
remote.getURIs().get(0)),
String.format("%s\t%s (push)", remote.getName(),
remote.getURIs().get(0)),
"" },
execute("git remote -v"));
}
@Test
public void testAdd() throws Exception {
assertArrayEquals(new String[] { "" },
execute("git remote add second git://test.com/second"));
List<RemoteConfig> remotes = RemoteConfig.getAllRemoteConfigs(config);
assertEquals(2, remotes.size());
assertEquals("second", remotes.get(0).getName());
assertEquals("test", remotes.get(1).getName());
}
@Test
public void testRemove() throws Exception {
assertArrayEquals(new String[] { "" },
execute("git remote remove test"));
assertTrue(RemoteConfig.getAllRemoteConfigs(config).isEmpty());
}
@Test
public void testSetUrl() throws Exception {
assertArrayEquals(new String[] { "" },
execute("git remote set-url test git://test.com/test"));
RemoteConfig result = new RemoteConfig(config, "test");
assertEquals("test", result.getName());
assertArrayEquals(new URIish[] { new URIish("git://test.com/test") },
result.getURIs().toArray());
assertTrue(result.getPushURIs().isEmpty());
}
@Test
public void testSetUrlPush() throws Exception {
assertArrayEquals(new String[] { "" },
execute("git remote set-url --push test git://test.com/test"));
RemoteConfig result = new RemoteConfig(config, "test");
assertEquals("test", result.getName());
assertEquals(remote.getURIs(), result.getURIs());
assertArrayEquals(new URIish[] { new URIish("git://test.com/test") },
result.getPushURIs().toArray());
}
@Test
public void testUpdate() throws Exception {
assertArrayEquals(new String[] {
"From " + remote.getURIs().get(0).toString(),
" * [new branch] master -> test/master", "", "" },
execute("git remote update test"));
}
}

1
org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin

@ -24,6 +24,7 @@ org.eclipse.jgit.pgm.MergeBase
org.eclipse.jgit.pgm.Push
org.eclipse.jgit.pgm.ReceivePack
org.eclipse.jgit.pgm.Reflog
org.eclipse.jgit.pgm.Remote
org.eclipse.jgit.pgm.Repo
org.eclipse.jgit.pgm.Reset
org.eclipse.jgit.pgm.RevList

3
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties

@ -188,6 +188,7 @@ treeIsRequired=argument tree is required
tooManyRefsGiven=Too many refs given
unknownIoErrorStdout=An unknown I/O error occurred on standard output
unknownMergeStrategy=unknown merge strategy {0} specified
unknownSubcommand=Unknown subcommand: {0}
unmergedPaths=Unmerged paths:
unsupportedOperation=Unsupported operation: {0}
untrackedFiles=Untracked files:
@ -222,6 +223,7 @@ usage_MergeBase=Find as good common ancestors as possible for a merge
usage_MergesTwoDevelopmentHistories=Merges two development histories
usage_ReadDirCache= Read the DirCache 100 times
usage_RebuildCommitGraph=Recreate a repository from another one's commit graph
usage_Remote=Manage set of tracked repositories
usage_RepositoryToReadFrom=Repository to read from
usage_RepositoryToReceiveInto=Repository to receive into
usage_RevList=List commit objects in reverse chronological order
@ -329,6 +331,7 @@ usage_performFsckStyleChecksOnReceive=perform fsck style checks on receive
usage_portNumberToListenOn=port number to listen on
usage_printOnlyBranchesThatContainTheCommit=print only branches that contain the commit
usage_pruneStaleTrackingRefs=prune stale tracking refs
usage_pushUrls=push URLs are manipulated
usage_quiet=don't show progress messages
usage_recordChangesToRepository=Record changes to the repository
usage_recurseIntoSubtrees=recurse into subtrees

197
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java

@ -0,0 +1,197 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.pgm;
import java.io.IOException;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RemoteAddCommand;
import org.eclipse.jgit.api.RemoteListCommand;
import org.eclipse.jgit.api.RemoteRemoveCommand;
import org.eclipse.jgit.api.RemoteSetUrlCommand;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.pgm.opt.CmdLineParser;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.io.ThrowingPrintWriter;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@Command(common = false, usage = "usage_Remote")
class Remote extends TextBuiltin {
@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
private boolean verbose = false;
@Option(name = "--prune", aliases = {
"-p" }, usage = "usage_pruneStaleTrackingRefs")
private boolean prune;
@Option(name = "--push", usage = "usage_pushUrls")
private boolean push;
@Argument(index = 0, metaVar = "metaVar_command")
private String command;
@Argument(index = 1, metaVar = "metaVar_remoteName")
private String name;
@Argument(index = 2, metaVar = "metaVar_uriish")
private String uri;
@Override
protected void run() throws Exception {
try (Git git = new Git(db)) {
if (command == null) {
RemoteListCommand cmd = git.remoteList();
List<RemoteConfig> remotes = cmd.call();
print(remotes);
} else if ("add".equals(command)) { //$NON-NLS-1$
RemoteAddCommand cmd = git.remoteAdd();
cmd.setName(name);
cmd.setUri(new URIish(uri));
cmd.call();
} else if ("remove".equals(command) || "rm".equals(command)) { //$NON-NLS-1$ //$NON-NLS-2$
RemoteRemoveCommand cmd = git.remoteRemove();
cmd.setName(name);
cmd.call();
} else if ("set-url".equals(command)) { //$NON-NLS-1$
RemoteSetUrlCommand cmd = git.remoteSetUrl();
cmd.setName(name);
cmd.setUri(new URIish(uri));
cmd.setPush(push);
cmd.call();
} else if ("update".equals(command)) { //$NON-NLS-1$
// reuse fetch command for basic implementation of remote update
Fetch fetch = new Fetch();
fetch.init(db, gitdir);
// redirect the output stream
StringWriter osw = new StringWriter();
fetch.outw = new ThrowingPrintWriter(osw);
// redirect the error stream
StringWriter esw = new StringWriter();
fetch.errw = new ThrowingPrintWriter(esw);
List<String> fetchArgs = new ArrayList<>();
if (verbose) {
fetchArgs.add("--verbose"); //$NON-NLS-1$
}
if (prune) {
fetchArgs.add("--prune"); //$NON-NLS-1$
}
if (name != null) {
fetchArgs.add(name);
}
fetch.execute(fetchArgs.toArray(new String[fetchArgs.size()]));
// flush the streams
fetch.outw.flush();
fetch.errw.flush();
outw.println(osw.toString());
errw.println(esw.toString());
} else {
throw new JGitInternalException(MessageFormat
.format(CLIText.get().unknownSubcommand, command));
}
}
}
@Override
public void printUsageAndExit(final String message, final CmdLineParser clp)
throws IOException {
errw.println(message);
errw.println("jgit remote [--verbose (-v)] [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote add name uri-ish [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote remove name [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote rm name [--help (-h)]"); //$NON-NLS-1$
errw.println(
"jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]"); //$NON-NLS-1$
errw.println();
clp.printUsage(errw, getResourceBundle());
errw.println();
errw.flush();
throw die(true);
}
private void print(List<RemoteConfig> remotes) throws IOException {
for (RemoteConfig remote : remotes) {
String remoteName = remote.getName();
if (verbose) {
List<URIish> fetchURIs = remote.getURIs();
List<URIish> pushURIs = remote.getPushURIs();
String fetchURI = ""; //$NON-NLS-1$
if (!fetchURIs.isEmpty()) {
fetchURI = fetchURIs.get(0).toString();
} else if (!pushURIs.isEmpty()) {
fetchURI = pushURIs.get(0).toString();
}
String pushURI = ""; //$NON-NLS-1$
if (!pushURIs.isEmpty()) {
pushURI = pushURIs.get(0).toString();
} else if (!fetchURIs.isEmpty()) {
pushURI = fetchURIs.get(0).toString();
}
outw.println(
String.format("%s\t%s (fetch)", remoteName, fetchURI)); //$NON-NLS-1$
outw.println(
String.format("%s\t%s (push)", remoteName, pushURI)); //$NON-NLS-1$
} else {
outw.println(remoteName);
}
}
}
}

1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java

@ -247,6 +247,7 @@ public class CLIText extends TranslationBundle {
/***/ public String treeIsRequired;
/***/ public char[] unknownIoErrorStdout;
/***/ public String unknownMergeStrategy;
/***/ public String unknownSubcommand;
/***/ public String unmergedPaths;
/***/ public String unsupportedOperation;
/***/ public String untrackedFiles;

96
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AbstractRemoteCommandTest.java

@ -0,0 +1,96 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URISyntaxException;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
public class AbstractRemoteCommandTest extends RepositoryTestCase {
protected static final String REMOTE_NAME = "test";
protected RemoteConfig setupRemote()
throws IOException, URISyntaxException {
// create another repository
Repository remoteRepository = createWorkRepository();
// set it up as a remote to this repository
final StoredConfig config = db.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, REMOTE_NAME);
RefSpec refSpec = new RefSpec();
refSpec = refSpec.setForceUpdate(true);
refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*",
Constants.R_REMOTES + REMOTE_NAME + "/*");
remoteConfig.addFetchRefSpec(refSpec);
URIish uri = new URIish(
remoteRepository.getDirectory().toURI().toURL());
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
return remoteConfig;
}
protected void assertRemoteConfigEquals(RemoteConfig expected,
RemoteConfig actual) {
assertEquals(expected.getName(), actual.getName());
assertEquals(expected.getURIs(), actual.getURIs());
assertEquals(expected.getPushURIs(), actual.getPushURIs());
assertEquals(expected.getFetchRefSpecs(), actual.getFetchRefSpecs());
assertEquals(expected.getPushRefSpecs(), actual.getPushRefSpecs());
}
}

81
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteAddCommandTest.java

@ -0,0 +1,81 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.junit.Test;
public class RemoteAddCommandTest extends AbstractRemoteCommandTest {
@Test
public void testAdd() throws Exception {
// create another repository
Repository remoteRepository = createWorkRepository();
URIish uri = new URIish(
remoteRepository.getDirectory().toURI().toURL());
// execute the command to add a new remote
RemoteAddCommand cmd = Git.wrap(db).remoteAdd();
cmd.setName(REMOTE_NAME);
cmd.setUri(uri);
RemoteConfig remote = cmd.call();
// assert that the added remote represents the remote repository
assertEquals(REMOTE_NAME, remote.getName());
assertArrayEquals(new URIish[] { uri }, remote.getURIs().toArray());
assertEquals(1, remote.getFetchRefSpecs().size());
assertEquals(
String.format("+refs/heads/*:refs/remotes/%s/*", REMOTE_NAME),
remote.getFetchRefSpecs().get(0).toString());
// assert that the added remote is available in the git configuration
assertRemoteConfigEquals(remote,
new RemoteConfig(db.getConfig(), REMOTE_NAME));
}
}

68
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteDeleteCommandTest.java

@ -0,0 +1,68 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import static org.junit.Assert.assertTrue;
import org.eclipse.jgit.transport.RemoteConfig;
import org.junit.Test;
public class RemoteDeleteCommandTest extends AbstractRemoteCommandTest {
@Test
public void testDelete() throws Exception {
// setup an initial remote
RemoteConfig remoteConfig = setupRemote();
// execute the command to remove the remote
RemoteRemoveCommand cmd = Git.wrap(db).remoteRemove();
cmd.setName(REMOTE_NAME);
RemoteConfig remote = cmd.call();
// assert that the removed remote is the initial remote
assertRemoteConfigEquals(remoteConfig, remote);
// assert that there are no remotes left
assertTrue(RemoteConfig.getAllRemoteConfigs(db.getConfig()).isEmpty());
}
}

68
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteListCommandTest.java

@ -0,0 +1,68 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.eclipse.jgit.transport.RemoteConfig;
import org.junit.Test;
public class RemoteListCommandTest extends AbstractRemoteCommandTest {
@Test
public void testList() throws Exception {
// setup an initial remote
RemoteConfig remoteConfig = setupRemote();
// execute the command to list the remotes
List<RemoteConfig> remotes = Git.wrap(db).remoteList().call();
// assert that there is only one remote
assertEquals(1, remotes.size());
// assert that the available remote is the initial remote
assertRemoteConfigEquals(remoteConfig, remotes.get(0));
}
}

100
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RemoteSetUrlCommandTest.java

@ -0,0 +1,100 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.junit.Test;
public class RemoteSetUrlCommandTest extends AbstractRemoteCommandTest {
@Test
public void testSetUrl() throws Exception {
// setup an initial remote
setupRemote();
// execute the command to change the fetch url
RemoteSetUrlCommand cmd = Git.wrap(db).remoteSetUrl();
cmd.setName(REMOTE_NAME);
URIish newUri = new URIish("git://test.com/test");
cmd.setUri(newUri);
RemoteConfig remote = cmd.call();
// assert that the changed remote has the new fetch url
assertEquals(REMOTE_NAME, remote.getName());
assertArrayEquals(new URIish[] { newUri }, remote.getURIs().toArray());
// assert that the changed remote is available in the git configuration
assertRemoteConfigEquals(remote,
new RemoteConfig(db.getConfig(), REMOTE_NAME));
}
@Test
public void testSetPushUrl() throws Exception {
// setup an initial remote
RemoteConfig remoteConfig = setupRemote();
// execute the command to change the push url
RemoteSetUrlCommand cmd = Git.wrap(db).remoteSetUrl();
cmd.setName(REMOTE_NAME);
URIish newUri = new URIish("git://test.com/test");
cmd.setUri(newUri);
cmd.setPush(true);
RemoteConfig remote = cmd.call();
// assert that the changed remote has the old fetch url and the new push
// url
assertEquals(REMOTE_NAME, remote.getName());
assertEquals(remoteConfig.getURIs(), remote.getURIs());
assertArrayEquals(new URIish[] { newUri },
remote.getPushURIs().toArray());
// assert that the changed remote is available in the git configuration
assertRemoteConfigEquals(remote,
new RemoteConfig(db.getConfig(), REMOTE_NAME));
}
}

44
org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java

@ -713,8 +713,48 @@ public class Git implements AutoCloseable {
}
/**
* @return the git repository this class is interacting with; see {@link
* #close()} for notes on closing this repository.
* Return a command used to list the available remotes.
*
* @return a {@link RemoteListCommand}
* @since 4.2
*/
public RemoteListCommand remoteList() {
return new RemoteListCommand(repo);
}
/**
* Return a command used to add a new remote.
*
* @return a {@link RemoteAddCommand}
* @since 4.2
*/
public RemoteAddCommand remoteAdd() {
return new RemoteAddCommand(repo);
}
/**
* Return a command used to remove an existing remote.
*
* @return a {@link RemoteRemoveCommand}
* @since 4.2
*/
public RemoteRemoveCommand remoteRemove() {
return new RemoteRemoveCommand(repo);
}
/**
* Return a command used to change the URL of an existing remote.
*
* @return a {@link RemoteSetUrlCommand}
* @since 4.2
*/
public RemoteSetUrlCommand remoteSetUrl() {
return new RemoteSetUrlCommand(repo);
}
/**
* @return the git repository this class is interacting with; see
* {@link #close()} for notes on closing this repository.
*/
public Repository getRepository() {
return repo;

133
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteAddCommand.java

@ -0,0 +1,133 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import java.io.IOException;
import java.net.URISyntaxException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
/**
* Used to add a new remote.
*
* This class has setters for all supported options and arguments of this
* command and a {@link #call()} method to finally execute the command.
*
* @see <a href=
* "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
* documentation about Remote</a>
*
* @since 4.2
*/
public class RemoteAddCommand extends GitCommand<RemoteConfig> {
private String name;
private URIish uri;
/**
* @param repo
*/
protected RemoteAddCommand(Repository repo) {
super(repo);
}
/**
* The name of the remote to add.
*
* @param name
* a remote name
*/
public void setName(String name) {
this.name = name;
}
/**
* The URL of the repository for the new remote.
*
* @param uri
* an URL for the remote
*/
public void setUri(URIish uri) {
this.uri = uri;
}
/**
* Executes the {@code remote add} command with all the options and
* parameters collected by the setter methods of this class.
*
* @return the {@link RemoteConfig} object of the added remote
*/
@Override
public RemoteConfig call() throws GitAPIException {
checkCallable();
try {
StoredConfig config = repo.getConfig();
RemoteConfig remote = new RemoteConfig(config, name);
RefSpec refSpec = new RefSpec();
refSpec = refSpec.setForceUpdate(true);
refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", //$NON-NLS-1$
Constants.R_REMOTES + name + "/*"); //$NON-NLS-1$
remote.addFetchRefSpec(refSpec);
remote.addURI(uri);
remote.update(config);
config.save();
return remote;
} catch (IOException | URISyntaxException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
}

91
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteListCommand.java

@ -0,0 +1,91 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import java.net.URISyntaxException;
import java.util.List;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.RemoteConfig;
/**
* Used to obtain the list of remotes.
*
* This class has setters for all supported options and arguments of this
* command and a {@link #call()} method to finally execute the command.
*
* @see <a href=
* "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
* documentation about Remote</a>
*
* @since 4.2
*/
public class RemoteListCommand extends GitCommand<List<RemoteConfig>> {
/**
* @param repo
*/
protected RemoteListCommand(Repository repo) {
super(repo);
}
/**
* Executes the {@code remote} command with all the options and parameters
* collected by the setter methods of this class.
*
* @return a list of {@link RemoteConfig} objects.
*/
@Override
public List<RemoteConfig> call() throws GitAPIException {
checkCallable();
try {
return RemoteConfig.getAllRemoteConfigs(repo.getConfig());
} catch (URISyntaxException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
}

110
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteRemoveCommand.java

@ -0,0 +1,110 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import java.io.IOException;
import java.net.URISyntaxException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RemoteConfig;
/**
* Used to remove an existing remote.
*
* This class has setters for all supported options and arguments of this
* command and a {@link #call()} method to finally execute the command.
*
* @see <a href=
* "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
* documentation about Remote</a>
*
* @since 4.2
*/
public class RemoteRemoveCommand extends GitCommand<RemoteConfig> {
private String name;
/**
* @param repo
*/
protected RemoteRemoveCommand(Repository repo) {
super(repo);
}
/**
* The name of the remote to remove.
*
* @param name
* a remote name
*/
public void setName(String name) {
this.name = name;
}
/**
* Executes the {@code remote} command with all the options and parameters
* collected by the setter methods of this class.
*
* @return the {@link RemoteConfig} object of the removed remote
*/
@Override
public RemoteConfig call() throws GitAPIException {
checkCallable();
try {
StoredConfig config = repo.getConfig();
RemoteConfig remote = new RemoteConfig(config, name);
config.unsetSection(ConfigConstants.CONFIG_KEY_REMOTE, name);
config.save();
return remote;
} catch (IOException | URISyntaxException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
}

155
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoteSetUrlCommand.java

@ -0,0 +1,155 @@
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
/**
* Used to to change the URL of a remote.
*
* This class has setters for all supported options and arguments of this
* command and a {@link #call()} method to finally execute the command.
*
* @see <a href=
* "http://www.kernel.org/pub/software/scm/git/docs/git-remote.html" > Git
* documentation about Remote</a>
*
* @since 4.2
*/
public class RemoteSetUrlCommand extends GitCommand<RemoteConfig> {
private String name;
private URIish uri;
private boolean push;
/**
* @param repo
*/
protected RemoteSetUrlCommand(Repository repo) {
super(repo);
}
/**
* The name of the remote to change the URL for.
*
* @param name
* a remote name
*/
public void setName(String name) {
this.name = name;
}
/**
* The new URL for the remote.
*
* @param uri
* an URL for the remote
*/
public void setUri(URIish uri) {
this.uri = uri;
}
/**
* Whether to change the push URL of the remote instead of the fetch URL.
*
* @param push
* <code>true</code> to set the push url, <code>false</code> to
* set the fetch url
*/
public void setPush(boolean push) {
this.push = push;
}
/**
* Executes the {@code remote} command with all the options and parameters
* collected by the setter methods of this class.
*
* @return the {@link RemoteConfig} object of the modified remote
*/
@Override
public RemoteConfig call() throws GitAPIException {
checkCallable();
try {
StoredConfig config = repo.getConfig();
RemoteConfig remote = new RemoteConfig(config, name);
if (push) {
List<URIish> uris = remote.getPushURIs();
if (uris.size() > 1) {
throw new JGitInternalException(
"remote.newtest.pushurl has multiple values"); //$NON-NLS-1$
} else if (uris.size() == 1) {
remote.removePushURI(uris.get(0));
}
remote.addPushURI(uri);
} else {
List<URIish> uris = remote.getURIs();
if (uris.size() > 1) {
throw new JGitInternalException(
"remote.newtest.url has multiple values"); //$NON-NLS-1$
} else if (uris.size() == 1) {
remote.removeURI(uris.get(0));
}
remote.addURI(uri);
}
remote.update(config);
config.save();
return remote;
} catch (IOException | URISyntaxException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
}

2
org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

@ -593,6 +593,8 @@ public class URIish implements Serializable {
private static boolean eq(final String a, final String b) {
if (a == b)
return true;
if (StringUtils.isEmptyOrNull(a) && StringUtils.isEmptyOrNull(b))
return true;
if (a == null || b == null)
return false;
return a.equals(b);

Loading…
Cancel
Save