import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
-import java.util.concurrent.Callable;
import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
* >Git documentation about Clone</a>
*/
-public class CloneCommand implements Callable<Git> {
+public class CloneCommand extends TransportCommand<CloneCommand, Git> {
private String uri;
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
- private CredentialsProvider credentialsProvider;
-
- private int timeout;
-
private boolean cloneAllBranches;
private boolean noCheckout;
private Collection<String> branchesToClone;
- private TransportConfigCallback transportConfigCallback;
+ /**
+ * Create clone command with no repository set
+ */
+ public CloneCommand() {
+ super(null);
+ }
/**
* Executes the {@code Clone} command.
return command.call().getRepository();
}
- private FetchResult fetch(Repository repo, URIish u)
+ private FetchResult fetch(Repository clonedRepo, URIish u)
throws URISyntaxException,
JGitInternalException,
InvalidRemoteException, IOException {
// create the remote config and save it
- RemoteConfig config = new RemoteConfig(repo.getConfig(), remote);
+ RemoteConfig config = new RemoteConfig(clonedRepo.getConfig(), remote);
config.addURI(u);
final String dst = bare ? Constants.R_HEADS : Constants.R_REMOTES
refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
config.addFetchRefSpec(refSpec);
- config.update(repo.getConfig());
+ config.update(clonedRepo.getConfig());
- repo.getConfig().save();
+ clonedRepo.getConfig().save();
// run the fetch command
- FetchCommand command = new FetchCommand(repo);
+ FetchCommand command = new FetchCommand(clonedRepo);
command.setRemote(remote);
command.setProgressMonitor(monitor);
command.setTagOpt(TagOpt.FETCH_TAGS);
- command.setTimeout(timeout);
- if (credentialsProvider != null)
- command.setCredentialsProvider(credentialsProvider);
- command.setTransportConfigCallback(transportConfigCallback);
+ configure(command);
List<RefSpec> specs = calculateRefSpecs(dst);
command.setRefSpecs(specs);
return specs;
}
- private void checkout(Repository repo, FetchResult result)
+ private void checkout(Repository clonedRepo, FetchResult result)
throws JGitInternalException,
MissingObjectException, IncorrectObjectTypeException, IOException {
return; // throw exception?
if (head.getName().startsWith(Constants.R_HEADS)) {
- final RefUpdate newHead = repo.updateRef(Constants.HEAD);
+ final RefUpdate newHead = clonedRepo.updateRef(Constants.HEAD);
newHead.disableRefLog();
newHead.link(head.getName());
- addMergeConfig(repo, head);
+ addMergeConfig(clonedRepo, head);
}
- final RevCommit commit = parseCommit(repo, head);
+ final RevCommit commit = parseCommit(clonedRepo, head);
boolean detached = !head.getName().startsWith(Constants.R_HEADS);
- RefUpdate u = repo.updateRef(Constants.HEAD, detached);
+ RefUpdate u = clonedRepo.updateRef(Constants.HEAD, detached);
u.setNewObjectId(commit.getId());
u.forceUpdate();
if (!bare) {
- DirCache dc = repo.lockDirCache();
- DirCacheCheckout co = new DirCacheCheckout(repo, dc,
+ DirCache dc = clonedRepo.lockDirCache();
+ DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
commit.getTree());
co.checkout();
}
return foundBranch;
}
- private void addMergeConfig(Repository repo, Ref head) throws IOException {
+ private void addMergeConfig(Repository clonedRepo, Ref head)
+ throws IOException {
String branchName = Repository.shortenRefName(head.getName());
- repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
+ clonedRepo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
branchName, ConfigConstants.CONFIG_KEY_REMOTE, remote);
- repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
+ clonedRepo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
branchName, ConfigConstants.CONFIG_KEY_MERGE, head.getName());
- repo.getConfig().save();
+ clonedRepo.getConfig().save();
}
- private RevCommit parseCommit(final Repository repo, final Ref ref)
+ private RevCommit parseCommit(final Repository clonedRepo, final Ref ref)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
- final RevWalk rw = new RevWalk(repo);
+ final RevWalk rw = new RevWalk(clonedRepo);
final RevCommit commit;
try {
commit = rw.parseCommit(ref.getObjectId());
return this;
}
- /**
- * @param credentialsProvider
- * the {@link CredentialsProvider} to use
- * @return {@code this}
- */
- public CloneCommand setCredentialsProvider(
- CredentialsProvider credentialsProvider) {
- this.credentialsProvider = credentialsProvider;
- return this;
- }
-
- /**
- * @param timeout
- * the timeout used for the fetch step
- * @return {@code this}
- */
- public CloneCommand setTimeout(int timeout) {
- this.timeout = timeout;
- return this;
- }
-
/**
* @param cloneAllBranches
* true when all branches have to be fetched (indicates wildcard
this.noCheckout = noCheckout;
return this;
}
-
- /**
- * @param transportConfigCallback
- * if set, the callback will be invoked after the Transport has
- * created, but before the Transport is used. The callback can
- * use this opportunity to set additional type-specific
- * configuration on the Transport instance.
- * @return {@code this}
- */
- public CloneCommand setTransportConfigCallback(
- TransportConfigCallback transportConfigCallback) {
- this.transportConfigCallback = transportConfigCallback;
- return this;
- }
}
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.TagOpt;
* @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> {
+public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
private String remote = Constants.DEFAULT_REMOTE_NAME;
private boolean thin = Transport.DEFAULT_FETCH_THIN;
- private int timeout;
-
- private CredentialsProvider credentialsProvider;
-
private TagOpt tagOption;
- private TransportConfigCallback transportConfigCallback;
-
/**
* @param repo
*/
try {
transport.setCheckFetchedObjects(checkFetchedObjects);
transport.setRemoveDeletedRefs(removeDeletedRefs);
- transport.setTimeout(timeout);
transport.setDryRun(dryRun);
if (tagOption != null)
transport.setTagOpt(tagOption);
transport.setFetchThin(thin);
- if (credentialsProvider != null)
- transport.setCredentialsProvider(credentialsProvider);
- if (transportConfigCallback != null)
- transportConfigCallback.configure(transport);
+ configure(transport);
FetchResult result = transport.fetch(monitor, refSpecs);
return result;
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
*/
return this;
}
- /**
- * @param credentialsProvider
- * the {@link CredentialsProvider} to use
- * @return {@code this}
- */
- public FetchCommand setCredentialsProvider(
- CredentialsProvider credentialsProvider) {
- checkCallable();
- this.credentialsProvider = credentialsProvider;
- return this;
- }
-
/**
* Sets the specification of annotated tag behavior during fetch
*
this.tagOption = tagOpt;
return this;
}
-
- /**
- * @param transportConfigCallback
- * if set, the callback will be invoked after the Transport has
- * created, but before the Transport is used. The callback can
- * use this opportunity to set additional type-specific
- * configuration on the Transport instance.
- * @return {@code this}
- */
- public FetchCommand setTransportConfigCallback(
- TransportConfigCallback transportConfigCallback) {
- checkCallable();
- this.transportConfigCallback = transportConfigCallback;
- return this;
- }
}
* href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
* >Git documentation about ls-remote</a>
*/
-public class LsRemoteCommand extends GitCommand<Collection<Ref>> {
+public class LsRemoteCommand extends
+ TransportCommand<LsRemoteCommand, Collection<Ref>> {
private String remote = Constants.DEFAULT_REMOTE_NAME;
try {
Transport transport = Transport.open(repo, remote);
transport.setOptionUploadPack(uploadPack);
+ configure(transport);
try {
Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryState;
-import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.FetchResult;
/**
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
* >Git documentation about Pull</a>
*/
-public class PullCommand extends GitCommand<PullResult> {
- private int timeout = 0;
+public class PullCommand extends TransportCommand<PullCommand, PullResult> {
private final static String DOT = ".";
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
- private CredentialsProvider credentialsProvider;
-
- private TransportConfigCallback transportConfigCallback;
-
/**
* @param repo
*/
super(repo);
}
- /**
- * @param timeout
- * in seconds
- * @return this instance
- */
- public PullCommand setTimeout(int timeout) {
- this.timeout = timeout;
- return this;
- }
-
/**
* @param monitor
* a progress monitor
return this;
}
- /**
- * @param credentialsProvider
- * the {@link CredentialsProvider} to use
- * @return this instance
- */
- public PullCommand setCredentialsProvider(
- CredentialsProvider credentialsProvider) {
- checkCallable();
- this.credentialsProvider = credentialsProvider;
- return this;
- }
-
- /**
- * @param transportConfigCallback
- * if set, the callback will be invoked after the Transport has
- * created, but before the Transport is used. The callback can
- * use this opportunity to set additional type-specific
- * configuration on the Transport instance.
- * @return {@code this}
- */
- public PullCommand setTransportConfigCallback(
- TransportConfigCallback transportConfigCallback) {
- checkCallable();
- this.transportConfigCallback = transportConfigCallback;
- return this;
- }
-
/**
* Executes the {@code Pull} command with all the options and parameters
* collected by the setter methods (e.g.
FetchCommand fetch = new FetchCommand(repo);
fetch.setRemote(remote);
fetch.setProgressMonitor(monitor);
- fetch.setTimeout(this.timeout);
- fetch.setCredentialsProvider(credentialsProvider);
- fetch.setTransportConfigCallback(transportConfigCallback);
+ configure(fetch);
fetchRes = fetch.call();
} else {
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
* >Git documentation about Push</a>
*/
-public class PushCommand extends GitCommand<Iterable<PushResult>> {
+public class PushCommand extends
+ TransportCommand<PushCommand, Iterable<PushResult>> {
private String remote = Constants.DEFAULT_REMOTE_NAME;
private boolean thin = Transport.DEFAULT_PUSH_THIN;
- private int timeout;
-
- private CredentialsProvider credentialsProvider;
-
- private TransportConfigCallback transportConfigCallback;
-
/**
* @param repo
*/
final List<Transport> transports;
transports = Transport.openAll(repo, remote, Transport.Operation.PUSH);
for (final Transport transport : transports) {
- if (0 <= timeout)
- transport.setTimeout(timeout);
transport.setPushThin(thin);
if (receivePack != null)
transport.setOptionReceivePack(receivePack);
transport.setDryRun(dryRun);
- if (credentialsProvider != null)
- transport.setCredentialsProvider(credentialsProvider);
- if (transportConfigCallback != null)
- transportConfigCallback.configure(transport);
+ configure(transport);
final Collection<RemoteRefUpdate> toPush = transport
.findRemoteRefUpdatesFor(refSpecs);
return receivePack;
}
- /**
- * @param timeout
- * the timeout used for the push operation
- * @return {@code this}
- */
- public PushCommand setTimeout(int timeout) {
- checkCallable();
- this.timeout = timeout;
- return this;
- }
-
/**
* @return the timeout used for the push operation
*/
this.force = force;
return this;
}
-
- /**
- * @param credentialsProvider
- * the {@link CredentialsProvider} to use
- * @return {@code this}
- */
- public PushCommand setCredentialsProvider(
- CredentialsProvider credentialsProvider) {
- checkCallable();
- this.credentialsProvider = credentialsProvider;
- return this;
- }
-
- /**
- * @param transportConfigCallback
- * if set, the callback will be invoked after the Transport has
- * created, but before the Transport is used. The callback can
- * use this opportunity to set additional type-specific
- * configuration on the Transport instance.
- * @return {@code this}
- */
- public PushCommand setTransportConfigCallback(
- TransportConfigCallback transportConfigCallback) {
- checkCallable();
- this.transportConfigCallback = transportConfigCallback;
- return this;
- }
}
--- /dev/null
+/*
+ * Copyright (C) 2011, GitHub Inc.
+ * 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 org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.transport.CredentialsProvider;
+import org.eclipse.jgit.transport.Transport;
+
+/**
+ * Base class for commands that use a {@link Transport} during execution.
+ * <p>
+ * This class provides standard configuration of a transport for options such as
+ * a {@link CredentialsProvider}, a timeout, and a
+ * {@link TransportConfigCallback}.
+ *
+ * @param <C>
+ * @param <T>
+ */
+public abstract class TransportCommand<C extends GitCommand, T> extends
+ GitCommand<T> {
+
+ /**
+ * Configured credentials provider
+ */
+ protected CredentialsProvider credentialsProvider;
+
+ /**
+ * Configured transport timeout
+ */
+ protected int timeout;
+
+ /**
+ * Configured callback for transport configuration
+ */
+ protected TransportConfigCallback transportConfigCallback;
+
+ /**
+ * @param repo
+ */
+ protected TransportCommand(final Repository repo) {
+ super(repo);
+ }
+
+ /**
+ * @param credentialsProvider
+ * the {@link CredentialsProvider} to use
+ * @return {@code this}
+ */
+ public C setCredentialsProvider(
+ final CredentialsProvider credentialsProvider) {
+ this.credentialsProvider = credentialsProvider;
+ return self();
+ }
+
+ /**
+ * @param timeout
+ * the timeout used for the transport step
+ * @return {@code this}
+ */
+ public C setTimeout(int timeout) {
+ this.timeout = timeout;
+ return self();
+ }
+
+ /**
+ * @param transportConfigCallback
+ * if set, the callback will be invoked after the
+ * {@link Transport} has created, but before the
+ * {@link Transport} is used. The callback can use this
+ * opportunity to set additional type-specific configuration on
+ * the {@link Transport} instance.
+ * @return {@code this}
+ */
+ public C setTransportConfigCallback(
+ final TransportConfigCallback transportConfigCallback) {
+ this.transportConfigCallback = transportConfigCallback;
+ return self();
+ }
+
+ /** @return {@code this} */
+ @SuppressWarnings("unchecked")
+ protected final C self() {
+ return (C) this;
+ }
+
+ /**
+ * Configure transport with credentials provider, timeout, and config
+ * callback
+ *
+ * @param transport
+ * @return {@code this}
+ */
+ protected C configure(final Transport transport) {
+ if (credentialsProvider != null)
+ transport.setCredentialsProvider(credentialsProvider);
+ transport.setTimeout(timeout);
+ if (transportConfigCallback != null)
+ transportConfigCallback.configure(transport);
+ return self();
+ }
+
+ /**
+ * Configure a child command with the current configuration set in
+ * {@code this} command
+ *
+ * @param childCommand
+ * @return {@code this}
+ */
+ protected C configure(final TransportCommand childCommand) {
+ childCommand.setCredentialsProvider(credentialsProvider);
+ childCommand.setTimeout(timeout);
+ childCommand.setTransportConfigCallback(transportConfigCallback);
+ return self();
+ }
+}