Search in sources :

Example 1 with Transport

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());
            }
        }
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) SubMonitor(org.eclipse.core.runtime.SubMonitor) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) PushResult(org.eclipse.jgit.transport.PushResult) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) Git(org.eclipse.jgit.api.Git) Collection(java.util.Collection) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) Transport(org.eclipse.jgit.transport.Transport)

Example 2 with Transport

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);
                }
            }
        });
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfo(com.jcraft.jsch.UserInfo) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) SshTransport(org.eclipse.jgit.transport.SshTransport) Session(com.jcraft.jsch.Session) SSLSession(javax.net.ssl.SSLSession)

Example 3 with Transport

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);
                }
            }
        }
    };
}
Also used : TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) Host(org.eclipse.jgit.transport.OpenSshConfig.Host) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) Transport(org.eclipse.jgit.transport.Transport) SshTransport(org.eclipse.jgit.transport.SshTransport) SshTransport(org.eclipse.jgit.transport.SshTransport) Session(com.jcraft.jsch.Session)

Aggregations

Transport (org.eclipse.jgit.transport.Transport)3 Session (com.jcraft.jsch.Session)2 TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)2 JschConfigSessionFactory (org.eclipse.jgit.transport.JschConfigSessionFactory)2 SshTransport (org.eclipse.jgit.transport.SshTransport)2 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 UserInfo (com.jcraft.jsch.UserInfo)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 Collection (java.util.Collection)1 SSLSession (javax.net.ssl.SSLSession)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 EclipseGitProgressTransformer (org.eclipse.egit.core.EclipseGitProgressTransformer)1 Git (org.eclipse.jgit.api.Git)1 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)1 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)1 CredentialsProviderUserInfo (org.eclipse.jgit.transport.CredentialsProviderUserInfo)1 OpenSshConfig (org.eclipse.jgit.transport.OpenSshConfig)1 Host (org.eclipse.jgit.transport.OpenSshConfig.Host)1