Search in sources :

Example 1 with EclipseGitProgressTransformer

use of org.eclipse.egit.core.EclipseGitProgressTransformer in project egit by eclipse.

the class CloneOperation method run.

/**
 * @param monitor
 *            the monitor to be used for reporting progress and responding
 *            to cancellation. The monitor is never <code>null</code>
 * @throws InvocationTargetException
 * @throws InterruptedException
 */
public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    String title = NLS.bind(CoreText.CloneOperation_title, uri);
    SubMonitor progress = SubMonitor.convert(monitor, title, postCloneTasks.isEmpty() ? 10 : 11);
    EclipseGitProgressTransformer gitMonitor = new EclipseGitProgressTransformer(progress.newChild(10));
    CloneCommand.Callback callback = new CloneCommand.Callback() {

        @Override
        public void initializedSubmodules(Collection<String> submodules) {
        // Nothing to do
        }

        @Override
        public void cloningSubmodule(String path) {
            progress.setTaskName(NLS.bind(CoreText.CloneOperation_submodule_title, uri, path));
        }

        @Override
        public void checkingOut(AnyObjectId commit, String path) {
        // Nothing to do
        }
    };
    Repository repository = null;
    try {
        CloneCommand cloneRepository = Git.cloneRepository();
        cloneRepository.setCredentialsProvider(credentialsProvider);
        if (refName != null) {
            cloneRepository.setBranch(refName);
        } else {
            cloneRepository.setNoCheckout(true);
        }
        cloneRepository.setDirectory(workdir);
        cloneRepository.setProgressMonitor(gitMonitor);
        cloneRepository.setRemote(remoteName);
        cloneRepository.setURI(uri.toString());
        cloneRepository.setTimeout(timeout);
        cloneRepository.setCloneAllBranches(allSelected);
        cloneRepository.setCloneSubmodules(cloneSubmodules);
        if (cloneSubmodules) {
            cloneRepository.setCallback(callback);
        }
        if (selectedBranches != null) {
            List<String> branches = new ArrayList<String>();
            for (Ref branch : selectedBranches) {
                branches.add(branch.getName());
            }
            cloneRepository.setBranchesToClone(branches);
        }
        Git git = cloneRepository.call();
        repository = git.getRepository();
        if (!postCloneTasks.isEmpty()) {
            progress.setTaskName(title);
            progress.setWorkRemaining(postCloneTasks.size());
            progress.subTask(CoreText.CloneOperation_configuring);
            for (PostCloneTask task : postCloneTasks) {
                task.execute(repository, progress.newChild(1));
            }
        }
    } catch (final Exception e) {
        try {
            if (repository != null) {
                repository.close();
                repository = null;
            }
            FileUtils.delete(workdir, FileUtils.RECURSIVE);
        } catch (IOException ioe) {
            throw new InvocationTargetException(e, NLS.bind(CoreText.CloneOperation_failed_cleanup, ioe.getLocalizedMessage()));
        }
        if (monitor.isCanceled()) {
            throw new InterruptedException();
        } else {
            throw new InvocationTargetException(e);
        }
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) IOException(java.io.IOException) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) Collection(java.util.Collection)

Example 2 with EclipseGitProgressTransformer

use of org.eclipse.egit.core.EclipseGitProgressTransformer in project egit by eclipse.

the class CreatePatchOperation method createDiffFormatter.

private DiffFormatter createDiffFormatter(final ByteArrayOutputStream outputStream, IProgressMonitor monitor) {
    DiffFormatter diffFmt = new DiffFormatter(outputStream) {

        private IProject project;

        @Override
        public void format(DiffEntry ent) throws IOException, CorruptObjectException, MissingObjectException {
            // changes
            if (DiffHeaderFormat.WORKSPACE == headerFormat) {
                IProject p = getProject(ent);
                if (p != null && !p.equals(project)) {
                    project = p;
                    getOutputStream().write(// $NON-NLS-1$ //$NON-NLS-2$
                    encodeASCII("#P " + project.getName() + "\n"));
                }
            }
            super.format(ent);
        }
    };
    diffFmt.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
    return diffFmt;
}
Also used : EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) IProject(org.eclipse.core.resources.IProject) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 3 with EclipseGitProgressTransformer

use of org.eclipse.egit.core.EclipseGitProgressTransformer in project egit by eclipse.

the class MergeOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    if (mergeResult != null)
        throw new CoreException(new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor mymonitor) throws CoreException {
            IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
            SubMonitor progress = SubMonitor.convert(mymonitor, NLS.bind(CoreText.MergeOperation_ProgressMerge, refName), 3);
            try (Git git = new Git(repository)) {
                progress.worked(1);
                MergeCommand merge = git.merge().setProgressMonitor(new EclipseGitProgressTransformer(progress.newChild(1)));
                Ref ref = repository.findRef(refName);
                if (ref != null) {
                    merge.include(ref);
                } else {
                    merge.include(ObjectId.fromString(refName));
                }
                if (fastForwardMode != null) {
                    merge.setFastForward(fastForwardMode);
                }
                if (commit != null) {
                    merge.setCommit(commit.booleanValue());
                }
                if (squash != null) {
                    merge.setSquash(squash.booleanValue());
                }
                if (mergeStrategy != null) {
                    merge.setStrategy(mergeStrategy);
                }
                if (message != null) {
                    merge.setMessage(message);
                }
                mergeResult = merge.call();
                if (MergeResult.MergeStatus.NOT_SUPPORTED.equals(mergeResult.getMergeStatus())) {
                    throw new TeamException(new Status(IStatus.INFO, Activator.getPluginId(), mergeResult.toString()));
                }
            } catch (IOException e) {
                throw new TeamException(CoreText.MergeOperation_InternalError, e);
            } catch (NoHeadException e) {
                throw new TeamException(CoreText.MergeOperation_MergeFailedNoHead, e);
            } catch (ConcurrentRefUpdateException e) {
                throw new TeamException(CoreText.MergeOperation_MergeFailedRefUpdate, e);
            } catch (CheckoutConflictException e) {
                mergeResult = new MergeResult(e.getConflictingPaths());
                return;
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                ProjectUtil.refreshValidProjects(validProjects, progress.newChild(1));
            }
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NoHeadException(org.eclipse.jgit.api.errors.NoHeadException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) SubMonitor(org.eclipse.core.runtime.SubMonitor) MergeCommand(org.eclipse.jgit.api.MergeCommand) MergeResult(org.eclipse.jgit.api.MergeResult) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) IOException(java.io.IOException) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) IProject(org.eclipse.core.resources.IProject) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) CoreException(org.eclipse.core.runtime.CoreException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)

Example 4 with EclipseGitProgressTransformer

use of org.eclipse.egit.core.EclipseGitProgressTransformer in project egit by eclipse.

the class PullOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    if (!results.isEmpty())
        throw new CoreException(new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
    SubMonitor totalProgress = SubMonitor.convert(m, NLS.bind(CoreText.PullOperation_TaskName, Integer.valueOf(repositories.length)), 1);
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor mymonitor) throws CoreException {
            if (mymonitor.isCanceled())
                throw new CoreException(Status.CANCEL_STATUS);
            SubMonitor progress = SubMonitor.convert(mymonitor, repositories.length * 2);
            for (int i = 0; i < repositories.length; i++) {
                Repository repository = repositories[i];
                IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
                PullResult pullResult = null;
                try (Git git = new Git(repository)) {
                    PullCommand pull = git.pull();
                    SubMonitor newChild = progress.newChild(1, SubMonitor.SUPPRESS_NONE);
                    pull.setProgressMonitor(new EclipseGitProgressTransformer(newChild));
                    pull.setTimeout(timeout);
                    pull.setCredentialsProvider(credentialsProvider);
                    PullReferenceConfig config = configs.get(repository);
                    newChild.setTaskName(getPullTaskName(repository, config));
                    if (config != null) {
                        if (config.getRemote() != null) {
                            pull.setRemote(config.getRemote());
                        }
                        if (config.getReference() != null) {
                            pull.setRemoteBranchName(config.getReference());
                        }
                        pull.setRebase(config.getUpstreamConfig());
                    }
                    MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                    if (strategy != null) {
                        pull.setStrategy(strategy);
                    }
                    pullResult = pull.call();
                    results.put(repository, pullResult);
                } catch (DetachedHeadException e) {
                    results.put(repository, Activator.error(CoreText.PullOperation_DetachedHeadMessage, e));
                } catch (InvalidConfigurationException e) {
                    IStatus error = Activator.error(CoreText.PullOperation_PullNotConfiguredMessage, e);
                    results.put(repository, error);
                } catch (GitAPIException e) {
                    results.put(repository, Activator.error(e.getMessage(), e));
                } catch (JGitInternalException e) {
                    Throwable cause = e.getCause();
                    if (cause == null || !(cause instanceof TransportException))
                        cause = e;
                    results.put(repository, Activator.error(cause.getMessage(), cause));
                } finally {
                    if (refreshNeeded(pullResult)) {
                        ProjectUtil.refreshValidProjects(validProjects, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
                    } else {
                        progress.worked(1);
                    }
                }
            }
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, totalProgress);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) MergeStatus(org.eclipse.jgit.api.MergeResult.MergeStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) PullCommand(org.eclipse.jgit.api.PullCommand) IStatus(org.eclipse.core.runtime.IStatus) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) SubMonitor(org.eclipse.core.runtime.SubMonitor) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) TransportException(org.eclipse.jgit.errors.TransportException) IProject(org.eclipse.core.resources.IProject) PullResult(org.eclipse.jgit.api.PullResult) InvalidConfigurationException(org.eclipse.jgit.api.errors.InvalidConfigurationException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) CoreException(org.eclipse.core.runtime.CoreException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) DetachedHeadException(org.eclipse.jgit.api.errors.DetachedHeadException)

Example 5 with EclipseGitProgressTransformer

use of org.eclipse.egit.core.EclipseGitProgressTransformer 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)

Aggregations

EclipseGitProgressTransformer (org.eclipse.egit.core.EclipseGitProgressTransformer)12 SubMonitor (org.eclipse.core.runtime.SubMonitor)7 Git (org.eclipse.jgit.api.Git)7 IProject (org.eclipse.core.resources.IProject)5 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)5 CoreException (org.eclipse.core.runtime.CoreException)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)4 Repository (org.eclipse.jgit.lib.Repository)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 IndexDiff (org.eclipse.jgit.lib.IndexDiff)3 MergeStrategy (org.eclipse.jgit.merge.MergeStrategy)3 Collection (java.util.Collection)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 Ref (org.eclipse.jgit.lib.Ref)2