Search in sources :

Example 1 with TransportException

use of org.eclipse.jgit.errors.TransportException 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 2 with TransportException

use of org.eclipse.jgit.errors.TransportException in project bndtools by bndtools.

the class GitUtils method getRepository.

public static synchronized Repository getRepository(File gitRoot, String branch, String gitUrl, String gitPushUrl) throws IOException, ConfigInvalidException, JGitInternalException, GitAPIException {
    File dotGit;
    if (gitRoot.getName().equals(Constants.DOT_GIT)) {
        dotGit = gitRoot;
    } else {
        dotGit = new File(gitRoot, Constants.DOT_GIT);
    }
    Repository repository = localRepos.get(dotGit);
    if (repository != null && dotGit.exists()) {
        return repository;
    }
    if (!dotGit.exists()) {
        Git.cloneRepository().setDirectory(gitRoot).setCloneAllBranches(true).setURI(gitUrl).call();
        FileBasedConfig config = new FileBasedConfig(new File(dotGit, "config"), FS.DETECTED);
        config.load();
        if (gitPushUrl != null) {
            config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "pushurl", gitPushUrl);
        }
        config.save();
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    repository = builder.setGitDir(dotGit).readEnvironment().findGitDir().build();
    localRepos.put(dotGit, repository);
    try {
        repository.incrementOpen();
        Git git = Git.wrap(repository);
        // Check branch
        boolean pull = true;
        String currentBranch = repository.getBranch();
        if (branch != null && !branch.equals(currentBranch)) {
            CheckoutCommand checkout = git.checkout();
            if (!branchExists(git, branch)) {
                checkout.setCreateBranch(true);
                pull = false;
            }
            checkout.setName(branch);
            checkout.call();
        }
        if (pull) {
            git.pull().call();
        } else {
            git.fetch().call();
        }
    } catch (Exception e) {
        if (!(e.getCause() instanceof TransportException)) {
            throw new RuntimeException(e);
        }
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return repository;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) TransportException(org.eclipse.jgit.errors.TransportException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) TransportException(org.eclipse.jgit.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Aggregations

Git (org.eclipse.jgit.api.Git)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)2 TransportException (org.eclipse.jgit.errors.TransportException)2 Repository (org.eclipse.jgit.lib.Repository)2 File (java.io.File)1 IOException (java.io.IOException)1 IProject (org.eclipse.core.resources.IProject)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 EclipseGitProgressTransformer (org.eclipse.egit.core.EclipseGitProgressTransformer)1 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)1 MergeStatus (org.eclipse.jgit.api.MergeResult.MergeStatus)1 PullCommand (org.eclipse.jgit.api.PullCommand)1 PullResult (org.eclipse.jgit.api.PullResult)1 DetachedHeadException (org.eclipse.jgit.api.errors.DetachedHeadException)1