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