use of org.eclipse.jgit.transport.Transport in project egit by eclipse.
the class PushOperation method run.
/**
* @param actMonitor
* may be <code>null</code> if progress monitoring is not desired
* @throws InvocationTargetException
* not really used: failure is communicated via the result (see
* {@link #getOperationResult()})
*/
public void run(IProgressMonitor actMonitor) throws InvocationTargetException {
if (operationResult != null)
throw new IllegalStateException(CoreText.OperationAlreadyExecuted);
if (this.specification != null)
for (URIish uri : this.specification.getURIs()) {
for (RemoteRefUpdate update : this.specification.getRefUpdates(uri)) if (update.getStatus() != Status.NOT_ATTEMPTED)
throw new IllegalStateException(CoreText.RemoteRefUpdateCantBeReused);
}
final int totalWork;
if (specification != null) {
totalWork = specification.getURIsNumber();
} else {
totalWork = 1;
}
String taskName = dryRun ? CoreText.PushOperation_taskNameDryRun : CoreText.PushOperation_taskNameNormalRun;
SubMonitor progress = SubMonitor.convert(actMonitor, totalWork);
progress.setTaskName(taskName);
operationResult = new PushOperationResult();
try (Git git = new Git(localDb)) {
if (specification != null)
for (final URIish uri : specification.getURIs()) {
if (progress.isCanceled()) {
operationResult.addOperationResult(uri, CoreText.PushOperation_resultCancelled);
progress.worked(1);
continue;
}
Collection<RemoteRefUpdate> refUpdates = specification.getRefUpdates(uri);
final EclipseGitProgressTransformer gitSubMonitor = new EclipseGitProgressTransformer(progress.newChild(1));
try (Transport transport = Transport.open(localDb, uri)) {
transport.setDryRun(dryRun);
transport.setTimeout(timeout);
if (credentialsProvider != null) {
transport.setCredentialsProvider(credentialsProvider);
}
PushResult result = transport.push(gitSubMonitor, refUpdates, out);
operationResult.addOperationResult(result.getURI(), result);
specification.addURIRefUpdates(result.getURI(), result.getRemoteUpdates());
} catch (JGitInternalException e) {
String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
String userMessage = NLS.bind(CoreText.PushOperation_InternalExceptionOccurredMessage, errorMessage);
handleException(uri, e, userMessage);
} catch (Exception e) {
handleException(uri, e, e.getMessage());
}
}
else {
final EclipseGitProgressTransformer gitMonitor = new EclipseGitProgressTransformer(progress.newChild(totalWork));
try {
Iterable<PushResult> results = git.push().setRemote(remoteName).setDryRun(dryRun).setTimeout(timeout).setProgressMonitor(gitMonitor).setCredentialsProvider(credentialsProvider).setOutputStream(out).call();
for (PushResult result : results) {
operationResult.addOperationResult(result.getURI(), result);
}
} catch (JGitInternalException e) {
String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
String userMessage = NLS.bind(CoreText.PushOperation_InternalExceptionOccurredMessage, errorMessage);
URIish uri = getPushURIForErrorHandling();
handleException(uri, e, userMessage);
} catch (Exception e) {
URIish uri = getPushURIForErrorHandling();
handleException(uri, e, e.getMessage());
}
}
}
}
use of org.eclipse.jgit.transport.Transport in project fabric8 by fabric8io.
the class GitUtils method configureCommand.
/**
* Configures the transport of the command to deal with things like SSH
*/
public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) {
LOG.info("Using " + credentialsProvider);
if (sshPrivateKey != null) {
final CredentialsProvider provider = credentialsProvider;
command.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
if (transport instanceof SshTransport) {
SshTransport sshTransport = (SshTransport) transport;
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
session.setUserInfo(userInfo);
}
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch jsch = super.createDefaultJSch(fs);
jsch.removeAllIdentity();
String absolutePath = sshPrivateKey.getAbsolutePath();
if (LOG.isDebugEnabled()) {
LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: " + sshPublicKey);
}
if (sshPublicKey != null) {
jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null);
} else {
jsch.addIdentity(absolutePath);
}
return jsch;
}
};
sshTransport.setSshSessionFactory(sshSessionFactory);
}
}
});
}
}
use of org.eclipse.jgit.transport.Transport in project CzechIdMng by bcvsolutions.
the class AbstractReleaseManager method getTransportConfigCallback.
/**
* Append credentials to git active operations
*
* @return
*/
protected TransportConfigCallback getTransportConfigCallback() {
return new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
//
transport.setCredentialsProvider(getCredentialsProvider());
//
if (transport instanceof SshTransport) {
SshTransport sshTransport = (SshTransport) transport;
//
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
@Override
protected void configure(Host host, Session session) {
if (password != null) {
session.setPassword(password.asString());
//
LOG.info("Ssh passphrase given, will be set for ssh public key ...");
}
}
});
} else {
if (username == null || password == null) {
LOG.warn("No credentials given. Set git username and password, if repository authentication is needed.");
} else {
LOG.info("Git credentials given, username [{}].", username);
}
}
}
};
}
Aggregations