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);
}
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;
}
Aggregations