]> source.dussan.org Git - jgit.git/commitdiff
Add FetchCommand 50/1550/7
authorChris Aniszczyk <caniszczyk@gmail.com>
Tue, 7 Sep 2010 14:21:12 +0000 (09:21 -0500)
committerChris Aniszczyk <caniszczyk@gmail.com>
Fri, 17 Sep 2010 18:32:59 +0000 (13:32 -0500)
Adds API for performing git fetch operations.

Change-Id: Idd95664fd4e3bca03211e4ffda3e354849f92a35
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java [new file with mode: 0644]
org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties
org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java
org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java [new file with mode: 0644]
org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java
org.eclipse.jgit/src/org/eclipse/jgit/api/errors/InvalidRemoteException.java [new file with mode: 0644]

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 (file)
index 0000000..10396b4
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.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.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()));
+
+       }
+
+}
index e8ec148bdd8514363b890ede8e550acdb9487454..4a1301b31eef51be3e49125e4c0edcf44dcb7da1 100644 (file)
@@ -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}
index c7b5a9ee6314fcc470dbcc67b14ab730b1cb48af..2194d67e4d5b0622decf59bd62510c3372ce96d7 100644 (file)
@@ -209,6 +209,7 @@ public class JGitText extends TranslationBundle {
        /***/ 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 class JGitText extends TranslationBundle {
        /***/ 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 (file)
index 0000000..e44958a
--- /dev/null
@@ -0,0 +1,275 @@
+/*
+ * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.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.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 <a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
+ *      >Git documentation about Fetch</a>
+ */
+public class FetchCommand extends GitCommand<FetchResult> {
+
+       private String remote = Constants.DEFAULT_REMOTE_NAME;
+
+       private List<RefSpec> 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<RefSpec>(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 <code>Constants.DEFAULT_REMOTE_NAME</code> 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 <code>NullProgressMonitor</code>
+        *
+        * @see NullProgressMonitor
+        *
+        * @param monitor
+        * @return {@code this}
+        */
+       public FetchCommand setProgressMonitor(ProgressMonitor monitor) {
+               checkCallable();
+               this.monitor = monitor;
+               return this;
+       }
+
+       /**
+        * @return the ref specs
+        */
+       public List<RefSpec> 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<RefSpec> specs) {
+               checkCallable();
+               this.refSpecs.clear();
+               this.refSpecs.addAll(specs);
+               return this;
+       }
+
+}
index f96c52239a60b85dc52136a8f1ded76e8199c5bc..95a88270479cb95f6ca343222aed289a74d59c55 100644 (file)
@@ -159,6 +159,19 @@ public class Git {
                return new TagCommand(repo);
        }
 
+       /**
+        * Returns a command object to execute a {@code Fetch} command
+        *
+        * @see <a
+        *      href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
+        *      >Git documentation about Fetch</a>
+        * @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 (file)
index 0000000..4104fd6
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.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.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);
+       }
+}