Support a configured credentials provider in LsRemoteCommand
Refactored the three common transport configuration options: credentials provider, timeout, and transport config callback into a new TransportCommand base class which is now extended by all commands that use a Transport object during execution. Bug: 349188 Change-Id: I90c2c14fb4e3cc4712905158f9047153a0c235c2 Signed-off-by: Kevin Sawicki <kevin@github.com> Signed-off-by: Chris Aniszczyk <zx@twitter.com>
This commit is contained in:
parent
c0392381ee
commit
da901c4968
|
@ -49,7 +49,6 @@
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
import org.eclipse.jgit.JGitText;
|
import org.eclipse.jgit.JGitText;
|
||||||
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
||||||
|
@ -67,7 +66,6 @@
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.revwalk.RevCommit;
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
import org.eclipse.jgit.revwalk.RevWalk;
|
import org.eclipse.jgit.revwalk.RevWalk;
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
|
||||||
import org.eclipse.jgit.transport.FetchResult;
|
import org.eclipse.jgit.transport.FetchResult;
|
||||||
import org.eclipse.jgit.transport.RefSpec;
|
import org.eclipse.jgit.transport.RefSpec;
|
||||||
import org.eclipse.jgit.transport.RemoteConfig;
|
import org.eclipse.jgit.transport.RemoteConfig;
|
||||||
|
@ -80,7 +78,7 @@
|
||||||
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
|
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
|
||||||
* >Git documentation about Clone</a>
|
* >Git documentation about Clone</a>
|
||||||
*/
|
*/
|
||||||
public class CloneCommand implements Callable<Git> {
|
public class CloneCommand extends TransportCommand<CloneCommand, Git> {
|
||||||
|
|
||||||
private String uri;
|
private String uri;
|
||||||
|
|
||||||
|
@ -94,17 +92,18 @@ public class CloneCommand implements Callable<Git> {
|
||||||
|
|
||||||
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
|
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
|
||||||
|
|
||||||
private CredentialsProvider credentialsProvider;
|
|
||||||
|
|
||||||
private int timeout;
|
|
||||||
|
|
||||||
private boolean cloneAllBranches;
|
private boolean cloneAllBranches;
|
||||||
|
|
||||||
private boolean noCheckout;
|
private boolean noCheckout;
|
||||||
|
|
||||||
private Collection<String> branchesToClone;
|
private Collection<String> branchesToClone;
|
||||||
|
|
||||||
private TransportConfigCallback transportConfigCallback;
|
/**
|
||||||
|
* Create clone command with no repository set
|
||||||
|
*/
|
||||||
|
public CloneCommand() {
|
||||||
|
super(null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the {@code Clone} command.
|
* Executes the {@code Clone} command.
|
||||||
|
@ -142,12 +141,12 @@ private Repository init(URIish u) {
|
||||||
return command.call().getRepository();
|
return command.call().getRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
private FetchResult fetch(Repository repo, URIish u)
|
private FetchResult fetch(Repository clonedRepo, URIish u)
|
||||||
throws URISyntaxException,
|
throws URISyntaxException,
|
||||||
JGitInternalException,
|
JGitInternalException,
|
||||||
InvalidRemoteException, IOException {
|
InvalidRemoteException, IOException {
|
||||||
// create the remote config and save it
|
// create the remote config and save it
|
||||||
RemoteConfig config = new RemoteConfig(repo.getConfig(), remote);
|
RemoteConfig config = new RemoteConfig(clonedRepo.getConfig(), remote);
|
||||||
config.addURI(u);
|
config.addURI(u);
|
||||||
|
|
||||||
final String dst = bare ? Constants.R_HEADS : Constants.R_REMOTES
|
final String dst = bare ? Constants.R_HEADS : Constants.R_REMOTES
|
||||||
|
@ -157,19 +156,16 @@ private FetchResult fetch(Repository repo, URIish u)
|
||||||
refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
|
refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
|
||||||
config.addFetchRefSpec(refSpec);
|
config.addFetchRefSpec(refSpec);
|
||||||
config.update(repo.getConfig());
|
config.update(clonedRepo.getConfig());
|
||||||
|
|
||||||
repo.getConfig().save();
|
clonedRepo.getConfig().save();
|
||||||
|
|
||||||
// run the fetch command
|
// run the fetch command
|
||||||
FetchCommand command = new FetchCommand(repo);
|
FetchCommand command = new FetchCommand(clonedRepo);
|
||||||
command.setRemote(remote);
|
command.setRemote(remote);
|
||||||
command.setProgressMonitor(monitor);
|
command.setProgressMonitor(monitor);
|
||||||
command.setTagOpt(TagOpt.FETCH_TAGS);
|
command.setTagOpt(TagOpt.FETCH_TAGS);
|
||||||
command.setTimeout(timeout);
|
configure(command);
|
||||||
if (credentialsProvider != null)
|
|
||||||
command.setCredentialsProvider(credentialsProvider);
|
|
||||||
command.setTransportConfigCallback(transportConfigCallback);
|
|
||||||
|
|
||||||
List<RefSpec> specs = calculateRefSpecs(dst);
|
List<RefSpec> specs = calculateRefSpecs(dst);
|
||||||
command.setRefSpecs(specs);
|
command.setRefSpecs(specs);
|
||||||
|
@ -193,7 +189,7 @@ else if (branchesToClone != null
|
||||||
return specs;
|
return specs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkout(Repository repo, FetchResult result)
|
private void checkout(Repository clonedRepo, FetchResult result)
|
||||||
throws JGitInternalException,
|
throws JGitInternalException,
|
||||||
MissingObjectException, IncorrectObjectTypeException, IOException {
|
MissingObjectException, IncorrectObjectTypeException, IOException {
|
||||||
|
|
||||||
|
@ -208,22 +204,22 @@ private void checkout(Repository repo, FetchResult result)
|
||||||
return; // throw exception?
|
return; // throw exception?
|
||||||
|
|
||||||
if (head.getName().startsWith(Constants.R_HEADS)) {
|
if (head.getName().startsWith(Constants.R_HEADS)) {
|
||||||
final RefUpdate newHead = repo.updateRef(Constants.HEAD);
|
final RefUpdate newHead = clonedRepo.updateRef(Constants.HEAD);
|
||||||
newHead.disableRefLog();
|
newHead.disableRefLog();
|
||||||
newHead.link(head.getName());
|
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);
|
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.setNewObjectId(commit.getId());
|
||||||
u.forceUpdate();
|
u.forceUpdate();
|
||||||
|
|
||||||
if (!bare) {
|
if (!bare) {
|
||||||
DirCache dc = repo.lockDirCache();
|
DirCache dc = clonedRepo.lockDirCache();
|
||||||
DirCacheCheckout co = new DirCacheCheckout(repo, dc,
|
DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
|
||||||
commit.getTree());
|
commit.getTree());
|
||||||
co.checkout();
|
co.checkout();
|
||||||
}
|
}
|
||||||
|
@ -246,19 +242,20 @@ private Ref findBranchToCheckout(FetchResult result) {
|
||||||
return foundBranch;
|
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());
|
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);
|
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());
|
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,
|
throws MissingObjectException, IncorrectObjectTypeException,
|
||||||
IOException {
|
IOException {
|
||||||
final RevWalk rw = new RevWalk(repo);
|
final RevWalk rw = new RevWalk(clonedRepo);
|
||||||
final RevCommit commit;
|
final RevCommit commit;
|
||||||
try {
|
try {
|
||||||
commit = rw.parseCommit(ref.getObjectId());
|
commit = rw.parseCommit(ref.getObjectId());
|
||||||
|
@ -342,27 +339,6 @@ public CloneCommand setProgressMonitor(ProgressMonitor monitor) {
|
||||||
return this;
|
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
|
* @param cloneAllBranches
|
||||||
* true when all branches have to be fetched (indicates wildcard
|
* true when all branches have to be fetched (indicates wildcard
|
||||||
|
@ -396,18 +372,4 @@ public CloneCommand setNoCheckout(boolean noCheckout) {
|
||||||
this.noCheckout = noCheckout;
|
this.noCheckout = noCheckout;
|
||||||
return this;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,6 @@
|
||||||
import org.eclipse.jgit.lib.NullProgressMonitor;
|
import org.eclipse.jgit.lib.NullProgressMonitor;
|
||||||
import org.eclipse.jgit.lib.ProgressMonitor;
|
import org.eclipse.jgit.lib.ProgressMonitor;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
|
||||||
import org.eclipse.jgit.transport.FetchResult;
|
import org.eclipse.jgit.transport.FetchResult;
|
||||||
import org.eclipse.jgit.transport.RefSpec;
|
import org.eclipse.jgit.transport.RefSpec;
|
||||||
import org.eclipse.jgit.transport.TagOpt;
|
import org.eclipse.jgit.transport.TagOpt;
|
||||||
|
@ -71,7 +70,7 @@
|
||||||
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
|
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
|
||||||
* >Git documentation about Fetch</a>
|
* >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 String remote = Constants.DEFAULT_REMOTE_NAME;
|
||||||
|
|
||||||
|
@ -87,14 +86,8 @@ public class FetchCommand extends GitCommand<FetchResult> {
|
||||||
|
|
||||||
private boolean thin = Transport.DEFAULT_FETCH_THIN;
|
private boolean thin = Transport.DEFAULT_FETCH_THIN;
|
||||||
|
|
||||||
private int timeout;
|
|
||||||
|
|
||||||
private CredentialsProvider credentialsProvider;
|
|
||||||
|
|
||||||
private TagOpt tagOption;
|
private TagOpt tagOption;
|
||||||
|
|
||||||
private TransportConfigCallback transportConfigCallback;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param repo
|
* @param repo
|
||||||
*/
|
*/
|
||||||
|
@ -127,15 +120,11 @@ public FetchResult call() throws JGitInternalException,
|
||||||
try {
|
try {
|
||||||
transport.setCheckFetchedObjects(checkFetchedObjects);
|
transport.setCheckFetchedObjects(checkFetchedObjects);
|
||||||
transport.setRemoveDeletedRefs(removeDeletedRefs);
|
transport.setRemoveDeletedRefs(removeDeletedRefs);
|
||||||
transport.setTimeout(timeout);
|
|
||||||
transport.setDryRun(dryRun);
|
transport.setDryRun(dryRun);
|
||||||
if (tagOption != null)
|
if (tagOption != null)
|
||||||
transport.setTagOpt(tagOption);
|
transport.setTagOpt(tagOption);
|
||||||
transport.setFetchThin(thin);
|
transport.setFetchThin(thin);
|
||||||
if (credentialsProvider != null)
|
configure(transport);
|
||||||
transport.setCredentialsProvider(credentialsProvider);
|
|
||||||
if (transportConfigCallback != null)
|
|
||||||
transportConfigCallback.configure(transport);
|
|
||||||
|
|
||||||
FetchResult result = transport.fetch(monitor, refSpecs);
|
FetchResult result = transport.fetch(monitor, refSpecs);
|
||||||
return result;
|
return result;
|
||||||
|
@ -182,17 +171,6 @@ public String getRemote() {
|
||||||
return remote;
|
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 the timeout used for the fetch operation
|
||||||
*/
|
*/
|
||||||
|
@ -334,18 +312,6 @@ public FetchCommand setThin(boolean thin) {
|
||||||
return this;
|
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
|
* Sets the specification of annotated tag behavior during fetch
|
||||||
*
|
*
|
||||||
|
@ -357,19 +323,4 @@ public FetchCommand setTagOpt(TagOpt tagOpt) {
|
||||||
this.tagOption = tagOpt;
|
this.tagOption = tagOpt;
|
||||||
return this;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,8 @@
|
||||||
* href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
|
* href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
|
||||||
* >Git documentation about ls-remote</a>
|
* >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;
|
private String remote = Constants.DEFAULT_REMOTE_NAME;
|
||||||
|
|
||||||
|
@ -133,6 +134,7 @@ public Collection<Ref> call() throws Exception {
|
||||||
try {
|
try {
|
||||||
Transport transport = Transport.open(repo, remote);
|
Transport transport = Transport.open(repo, remote);
|
||||||
transport.setOptionUploadPack(uploadPack);
|
transport.setOptionUploadPack(uploadPack);
|
||||||
|
configure(transport);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
|
Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
|
||||||
|
|
|
@ -70,7 +70,6 @@
|
||||||
import org.eclipse.jgit.lib.Ref;
|
import org.eclipse.jgit.lib.Ref;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.lib.RepositoryState;
|
import org.eclipse.jgit.lib.RepositoryState;
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
|
||||||
import org.eclipse.jgit.transport.FetchResult;
|
import org.eclipse.jgit.transport.FetchResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,17 +78,12 @@
|
||||||
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
|
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
|
||||||
* >Git documentation about Pull</a>
|
* >Git documentation about Pull</a>
|
||||||
*/
|
*/
|
||||||
public class PullCommand extends GitCommand<PullResult> {
|
public class PullCommand extends TransportCommand<PullCommand, PullResult> {
|
||||||
private int timeout = 0;
|
|
||||||
|
|
||||||
private final static String DOT = ".";
|
private final static String DOT = ".";
|
||||||
|
|
||||||
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
|
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
|
||||||
|
|
||||||
private CredentialsProvider credentialsProvider;
|
|
||||||
|
|
||||||
private TransportConfigCallback transportConfigCallback;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param repo
|
* @param repo
|
||||||
*/
|
*/
|
||||||
|
@ -97,16 +91,6 @@ protected PullCommand(Repository repo) {
|
||||||
super(repo);
|
super(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param timeout
|
|
||||||
* in seconds
|
|
||||||
* @return this instance
|
|
||||||
*/
|
|
||||||
public PullCommand setTimeout(int timeout) {
|
|
||||||
this.timeout = timeout;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param monitor
|
* @param monitor
|
||||||
* a progress monitor
|
* a progress monitor
|
||||||
|
@ -117,33 +101,6 @@ public PullCommand setProgressMonitor(ProgressMonitor monitor) {
|
||||||
return this;
|
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
|
* Executes the {@code Pull} command with all the options and parameters
|
||||||
* collected by the setter methods (e.g.
|
* collected by the setter methods (e.g.
|
||||||
|
@ -229,9 +186,7 @@ public PullResult call() throws WrongRepositoryStateException,
|
||||||
FetchCommand fetch = new FetchCommand(repo);
|
FetchCommand fetch = new FetchCommand(repo);
|
||||||
fetch.setRemote(remote);
|
fetch.setRemote(remote);
|
||||||
fetch.setProgressMonitor(monitor);
|
fetch.setProgressMonitor(monitor);
|
||||||
fetch.setTimeout(this.timeout);
|
configure(fetch);
|
||||||
fetch.setCredentialsProvider(credentialsProvider);
|
|
||||||
fetch.setTransportConfigCallback(transportConfigCallback);
|
|
||||||
|
|
||||||
fetchRes = fetch.call();
|
fetchRes = fetch.call();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -60,7 +60,6 @@
|
||||||
import org.eclipse.jgit.lib.ProgressMonitor;
|
import org.eclipse.jgit.lib.ProgressMonitor;
|
||||||
import org.eclipse.jgit.lib.Ref;
|
import org.eclipse.jgit.lib.Ref;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
|
||||||
import org.eclipse.jgit.transport.PushResult;
|
import org.eclipse.jgit.transport.PushResult;
|
||||||
import org.eclipse.jgit.transport.RefSpec;
|
import org.eclipse.jgit.transport.RefSpec;
|
||||||
import org.eclipse.jgit.transport.RemoteConfig;
|
import org.eclipse.jgit.transport.RemoteConfig;
|
||||||
|
@ -75,7 +74,8 @@
|
||||||
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
|
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
|
||||||
* >Git documentation about Push</a>
|
* >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 String remote = Constants.DEFAULT_REMOTE_NAME;
|
||||||
|
|
||||||
|
@ -91,12 +91,6 @@ public class PushCommand extends GitCommand<Iterable<PushResult>> {
|
||||||
|
|
||||||
private boolean thin = Transport.DEFAULT_PUSH_THIN;
|
private boolean thin = Transport.DEFAULT_PUSH_THIN;
|
||||||
|
|
||||||
private int timeout;
|
|
||||||
|
|
||||||
private CredentialsProvider credentialsProvider;
|
|
||||||
|
|
||||||
private TransportConfigCallback transportConfigCallback;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param repo
|
* @param repo
|
||||||
*/
|
*/
|
||||||
|
@ -145,16 +139,11 @@ public Iterable<PushResult> call() throws JGitInternalException,
|
||||||
final List<Transport> transports;
|
final List<Transport> transports;
|
||||||
transports = Transport.openAll(repo, remote, Transport.Operation.PUSH);
|
transports = Transport.openAll(repo, remote, Transport.Operation.PUSH);
|
||||||
for (final Transport transport : transports) {
|
for (final Transport transport : transports) {
|
||||||
if (0 <= timeout)
|
|
||||||
transport.setTimeout(timeout);
|
|
||||||
transport.setPushThin(thin);
|
transport.setPushThin(thin);
|
||||||
if (receivePack != null)
|
if (receivePack != null)
|
||||||
transport.setOptionReceivePack(receivePack);
|
transport.setOptionReceivePack(receivePack);
|
||||||
transport.setDryRun(dryRun);
|
transport.setDryRun(dryRun);
|
||||||
if (credentialsProvider != null)
|
configure(transport);
|
||||||
transport.setCredentialsProvider(credentialsProvider);
|
|
||||||
if (transportConfigCallback != null)
|
|
||||||
transportConfigCallback.configure(transport);
|
|
||||||
|
|
||||||
final Collection<RemoteRefUpdate> toPush = transport
|
final Collection<RemoteRefUpdate> toPush = transport
|
||||||
.findRemoteRefUpdatesFor(refSpecs);
|
.findRemoteRefUpdatesFor(refSpecs);
|
||||||
|
@ -233,17 +222,6 @@ public String getReceivePack() {
|
||||||
return receivePack;
|
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
|
* @return the timeout used for the push operation
|
||||||
*/
|
*/
|
||||||
|
@ -423,31 +401,4 @@ public PushCommand setForce(boolean force) {
|
||||||
this.force = force;
|
this.force = force;
|
||||||
return this;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,155 @@
|
||||||
|
/*
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue