From bbabc19e2f58236c6997e6bbb8b8f053b8c4587e Mon Sep 17 00:00:00 2001 From: Chris Aniszczyk Date: Tue, 7 Sep 2010 09:21:12 -0500 Subject: [PATCH] Add FetchCommand Adds API for performing git fetch operations. Change-Id: Idd95664fd4e3bca03211e4ffda3e354849f92a35 Signed-off-by: Chris Aniszczyk --- .../eclipse/jgit/api/FetchCommandTest.java | 91 ++++++ .../org/eclipse/jgit/JGitText.properties | 2 + .../src/org/eclipse/jgit/JGitText.java | 2 + .../org/eclipse/jgit/api/FetchCommand.java | 275 ++++++++++++++++++ .../src/org/eclipse/jgit/api/Git.java | 13 + .../api/errors/InvalidRemoteException.java | 52 ++++ 6 files changed, 435 insertions(+) create mode 100644 org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/api/errors/InvalidRemoteException.java diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java new file mode 100644 index 000000000..10396b4e7 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2010, Chris Aniszczyk + * 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.Config; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RepositoryTestCase; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTag; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.URIish; + +public class FetchCommandTest extends RepositoryTestCase { + + public void testFetch() throws JGitInternalException, IOException, + GitAPIException, URISyntaxException { + + // create other repository + Repository db2 = createWorkRepository(); + Git git2 = new Git(db2); + + // setup the first repository to fetch from the second repository + final Config config = db.getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + URIish uri = new URIish(db2.getDirectory().toURI().toURL()); + remoteConfig.addURI(uri); + remoteConfig.update(config); + + // create some refs via commits and tag + RevCommit commit = git2.commit().setMessage("initial commit").call(); + RevTag tag = git2.tag().setName("tag").call(); + + Git git1 = new Git(db); + + RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); + git1.fetch().setRemote("test").setRefSpecs(spec) + .call(); + + assertEquals(commit.getId(), + db.resolve(commit.getId().getName() + "^{commit}")); + assertEquals(tag.getId(), db.resolve(tag.getId().getName())); + + } + +} diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties index e8ec148bd..4a1301b31 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties @@ -149,6 +149,7 @@ errorOccurredDuringUnpackingOnTheRemoteEnd=error occurred during unpacking on th errorReadingInfoRefs=error reading info/refs exceptionCaughtDuringExecutionOfAddCommand=Exception caught during execution of add command exceptionCaughtDuringExecutionOfCommitCommand=Exception caught during execution of commit command +exceptionCaughtDuringExecutionOfFetchCommand=Exception caught during execution of fetch command exceptionCaughtDuringExecutionOfMergeCommand=Exception caught during execution of merge command. {0} exceptionCaughtDuringExecutionOfTagCommand=Exception caught during execution of tag command exceptionOccuredDuringAddingOfOptionToALogCommand=Exception occured during adding of {0} as option to a Log command @@ -212,6 +213,7 @@ invalidOldIdSent=invalid old id sent invalidPacketLineHeader=Invalid packet line header: {0} invalidPath=Invalid path: {0} invalidRefName=Invalid ref name: {0} +invalidRemote=Invalid remote: {0} invalidStageForPath=Invalid stage {0} for path {1} invalidTagOption=Invalid tag option: {0} invalidTimeout=Invalid timeout: {0} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java index c7b5a9ee6..2194d67e4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java @@ -209,6 +209,7 @@ public static JGitText get() { /***/ public String errorReadingInfoRefs; /***/ public String exceptionCaughtDuringExecutionOfAddCommand; /***/ public String exceptionCaughtDuringExecutionOfCommitCommand; + /***/ public String exceptionCaughtDuringExecutionOfFetchCommand; /***/ public String exceptionCaughtDuringExecutionOfMergeCommand; /***/ public String exceptionCaughtDuringExecutionOfTagCommand; /***/ public String exceptionOccuredDuringAddingOfOptionToALogCommand; @@ -271,6 +272,7 @@ public static JGitText get() { /***/ public String invalidOldIdSent; /***/ public String invalidPacketLineHeader; /***/ public String invalidPath; + /***/ public String invalidRemote; /***/ public String invalidRefName; /***/ public String invalidStageForPath; /***/ public String invalidTagOption; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java new file mode 100644 index 000000000..e44958a01 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java @@ -0,0 +1,275 @@ +/* + * Copyright (C) 2010, Chris Aniszczyk + * 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.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.api.errors.InvalidRemoteException; +import org.eclipse.jgit.api.errors.JGitInternalException; +import org.eclipse.jgit.errors.NotSupportedException; +import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.NullProgressMonitor; +import org.eclipse.jgit.lib.ProgressMonitor; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.FetchResult; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.Transport; + +/** + * A class used to execute a {@code Fetch} command. It has setters for all + * supported options and arguments of this command and a {@link #call()} method + * to finally execute the command. + * + * @see Git documentation about Fetch + */ +public class FetchCommand extends GitCommand { + + private String remote = Constants.DEFAULT_REMOTE_NAME; + + private List refSpecs; + + private ProgressMonitor monitor = NullProgressMonitor.INSTANCE; + + private boolean checkFetchedObjects; + + private boolean removeDeletedRefs; + + private int timeout; + + + /** + * @param repo + */ + protected FetchCommand(Repository repo) { + super(repo); + refSpecs = new ArrayList(3); + } + + /** + * Executes the {@code fetch} command with all the options and parameters + * collected by the setter methods of this class. Each instance of this + * class should only be used for one invocation of the command (means: one + * call to {@link #call()}) + * + * @return a {@link FetchResult} object representing the successful fetch + * result + * @throws InvalidRemoteException + * when called with an invalid remote uri + * @throws JGitInternalException + * a low-level exception of JGit has occurred. The original + * exception can be retrieved by calling + * {@link Exception#getCause()}. + */ + public FetchResult call() throws JGitInternalException, + InvalidRemoteException { + checkCallable(); + + try { + Transport transport = Transport.open(repo, remote); + transport.setCheckFetchedObjects(checkFetchedObjects); + transport.setRemoveDeletedRefs(removeDeletedRefs); + transport.setTimeout(timeout); + + try { + FetchResult result = transport.fetch(monitor, refSpecs); + return result; + + } catch (TransportException e) { + throw new JGitInternalException( + JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand, + e); + } finally { + transport.close(); + } + } catch (URISyntaxException e) { + throw new InvalidRemoteException(MessageFormat.format( + JGitText.get().invalidRemote, remote)); + } catch (NotSupportedException e) { + throw new JGitInternalException( + JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand, + e); + } + + } + + /** + * The remote (uri or name) used for the fetch operation. If no remote is + * set, the default value of Constants.DEFAULT_REMOTE_NAME will + * be used. + * + * @see Constants#DEFAULT_REMOTE_NAME + * @param remote + * @return {@code this} + */ + public FetchCommand setRemote(String remote) { + checkCallable(); + this.remote = remote; + return this; + } + + /** + * @return the remote used for the remote operation + */ + public String getRemote() { + return remote; + } + + /** + * @param timeout + * the timeout used for the fetch operation + * @return {@code this} + */ + public FetchCommand setTimeout(int timeout) { + checkCallable(); + this.timeout = timeout; + return this; + } + + /** + * @return the timeout used for the fetch operation + */ + public int getTimeout() { + return timeout; + } + + /** + * @return whether to check received objects checked for validity + */ + public boolean isCheckFetchedObjects() { + return checkFetchedObjects; + } + + /** + * If set to true, objects received will be checked for validity + * + * @param checkFetchedObjects + * @return {@code this} + */ + public FetchCommand setCheckFetchedObjects(boolean checkFetchedObjects) { + checkCallable(); + this.checkFetchedObjects = checkFetchedObjects; + return this; + } + + /** + * @return whether or not to remove refs which no longer exist in the source + */ + public boolean isRemoveDeletedRefs() { + return removeDeletedRefs; + } + + /** + * If set to true, refs are removed which no longer exist in the source + * + * @param removeDeletedRefs + * @return {@code this} + */ + public FetchCommand setRemoveDeletedRefs(boolean removeDeletedRefs) { + checkCallable(); + this.removeDeletedRefs = removeDeletedRefs; + return this; + } + + /** + * @return the progress monitor for the fetch operation + */ + public ProgressMonitor getProgressMonitor() { + return monitor; + } + + /** + * The progress monitor associated with the fetch operation. By default, + * this is set to NullProgressMonitor + * + * @see NullProgressMonitor + * + * @param monitor + * @return {@code this} + */ + public FetchCommand setProgressMonitor(ProgressMonitor monitor) { + checkCallable(); + this.monitor = monitor; + return this; + } + + /** + * @return the ref specs + */ + public List getRefSpecs() { + return refSpecs; + } + + /** + * The ref specs to be used in the fetch operation + * + * @param specs + * @return {@code this} + */ + public FetchCommand setRefSpecs(RefSpec... specs) { + checkCallable(); + this.refSpecs.clear(); + for (RefSpec spec : specs) + refSpecs.add(spec); + return this; + } + + /** + * The ref specs to be used in the fetch operation + * + * @param specs + * @return {@code this} + */ + public FetchCommand setRefSpecs(List specs) { + checkCallable(); + this.refSpecs.clear(); + this.refSpecs.addAll(specs); + return this; + } + +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java index f96c52239..95a882704 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java @@ -159,6 +159,19 @@ public TagCommand tag() { return new TagCommand(repo); } + /** + * Returns a command object to execute a {@code Fetch} command + * + * @see Git documentation about Fetch + * @return a {@link FetchCommand} used to collect all optional parameters + * and to finally execute the {@code Fetch} command + */ + public FetchCommand fetch() { + return new FetchCommand(repo); + } + /** * @return the git repository this class is interacting with */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/errors/InvalidRemoteException.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/errors/InvalidRemoteException.java new file mode 100644 index 000000000..4104fd669 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/errors/InvalidRemoteException.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010, Chris Aniszczyk 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.errors; + +/** + * Exception thrown when a fetch command was called with an invalid remote + */ +public class InvalidRemoteException extends GitAPIException { + private static final long serialVersionUID = 1L; + + /** + * @param msg + */ + public InvalidRemoteException(String msg) { + super(msg); + } +}