diff options
author | Andrey Loskutov | 2015-02-01 09:34:38 +0000 |
---|---|---|
committer | Matthias Sohn | 2015-02-01 23:27:15 +0000 |
commit | 579170f59b91cad6308a47d6db5205898ed21a6f (patch) | |
tree | 1f474ebf918a980eceef07b0a885851e0cacc4db /org.eclipse.egit.ui/src/org | |
parent | f58519649507bbc1ec9ec32e71fbce13c53f9f81 (diff) | |
download | egit-579170f59b91cad6308a47d6db5205898ed21a6f.tar.gz egit-579170f59b91cad6308a47d6db5205898ed21a6f.tar.xz egit-579170f59b91cad6308a47d6db5205898ed21a6f.zip |
Always run compare input calculation in background thread
Bug: 458251
Change-Id: I5a52025dfa59a2c28515d13f6f5161ad64769449
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Diffstat (limited to 'org.eclipse.egit.ui/src/org')
3 files changed, 144 insertions, 56 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/CompareUtils.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/CompareUtils.java index 1828236d32..8ab20c553d 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/CompareUtils.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/CompareUtils.java @@ -34,8 +34,12 @@ import org.eclipse.core.resources.mapping.ResourceTraversal; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; @@ -79,6 +83,7 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter; import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.io.EolCanonicalizingInputStream; import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.widgets.Display; import org.eclipse.team.core.history.IFileRevision; import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; import org.eclipse.ui.IEditorInput; @@ -86,6 +91,7 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IReusableEditor; import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; /** @@ -442,24 +448,70 @@ public class CompareUtils { * @param page * If not {@null} try to re-use a compare editor on this * page if any is available. Otherwise open a new one. - * @throws IOException - * If HEAD or {@code refName} can't be resolved in the given - * repository. */ - private static void compareWorkspaceWithRef(Repository repository, - IFile file, String refName, IWorkbenchPage page) throws IOException { - final RepositoryMapping mapping = RepositoryMapping.getMapping(file); - final String gitPath = mapping.getRepoRelativePath(file); - final ITypedElement base = SaveableCompareEditorInput - .createFileElement(file); + private static void compareWorkspaceWithRef(final Repository repository, + final IFile file, final String refName, final IWorkbenchPage page) { + + Job job = new Job(UIText.CompareUtils_jobName) { + + @Override + public IStatus run(IProgressMonitor monitor) { + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + final RepositoryMapping mapping = RepositoryMapping + .getMapping(file); + final String gitPath = mapping.getRepoRelativePath(file); + final ITypedElement base = SaveableCompareEditorInput + .createFileElement(file); + + CompareEditorInput in; + try { + in = prepareCompareInput(repository, gitPath, base, refName); + } catch (IOException e) { + return Activator.createErrorStatus( + UIText.CompareWithRefAction_errorOnSynchronize, e); + } + + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + openCompareEditorRunnable(page, in); + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); + } - CompareEditorInput in = prepareCompareInput(repository, gitPath, base, - refName); + /** + * Opens compare editor in UI thread. Safe to start from background threads + * too - in this case the operation will be started asynchronously in UI + * thread. + * + * @param page + * can be null + * @param in + * non null + */ + private static void openCompareEditorRunnable( + final IWorkbenchPage page, + final CompareEditorInput in) { + // safety check: make sure we open compare editor from UI thread + if (Display.getCurrent() == null) { + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + public void run() { + openCompareEditorRunnable(page, in); + } + }); + return; + } - if (page != null) + if (page != null) { openInCompare(page, in); - else + } else { CompareUI.openCompareEditor(in); + } } /** @@ -478,23 +530,39 @@ public class CompareUtils { * @param page * If not {@null} try to re-use a compare editor on this * page if any is available. Otherwise open a new one. - * @throws IOException - * If HEAD or {@code refName} can't be resolved in the given - * repository. */ - private static void compareLocalWithRef(Repository repository, - IPath location, String refName, IWorkbenchPage page) - throws IOException { - final String gitPath = getRepoRelativePath(location, repository); - final ITypedElement base = new LocalNonWorkspaceTypedElement(location); + private static void compareLocalWithRef(final Repository repository, + final IPath location, final String refName, + final IWorkbenchPage page) { - CompareEditorInput in = prepareCompareInput(repository, gitPath, base, - refName); + Job job = new Job(UIText.CompareUtils_jobName) { - if (page != null) - openInCompare(page, in); - else - CompareUI.openCompareEditor(in); + @Override + public IStatus run(IProgressMonitor monitor) { + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + final String gitPath = getRepoRelativePath(location, repository); + final ITypedElement base = new LocalNonWorkspaceTypedElement( + location); + + CompareEditorInput in; + try { + in = prepareCompareInput(repository, gitPath, base, refName); + } catch (IOException e) { + return Activator.createErrorStatus( + UIText.CompareWithRefAction_errorOnSynchronize, e); + } + + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + openCompareEditorRunnable(page, in); + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); } /* @@ -671,11 +739,10 @@ public class CompareUtils { * @param page * If not {@null} try to re-use a compare editor on this * page if any is available. Otherwise open a new one. - * @throws IOException */ public static void compare(IPath location, Repository repository, String leftRev, String rightRev, boolean includeLocal, - IWorkbenchPage page) throws IOException { + IWorkbenchPage page) { if (includeLocal) compareLocalWithRef(repository, location, rightRev, page); else { @@ -685,8 +752,7 @@ public class CompareUtils { } private static void compareBetween(Repository repository, String gitPath, - String leftRev, String rightRev, IWorkbenchPage page) - throws IOException { + String leftRev, String rightRev, IWorkbenchPage page) { compareBetween(repository, gitPath, gitPath, leftRev, rightRev, page); } @@ -710,34 +776,52 @@ public class CompareUtils { * @param page * If not {@null} try to re-use a compare editor on this * page if any is available. Otherwise open a new one. - * @throws IOException */ - private static void compareBetween(Repository repository, - String leftGitPath, String rightGitPath, String leftRev, - String rightRev, IWorkbenchPage page) throws IOException { - final ITypedElement left = getTypedElementFor(repository, leftGitPath, - leftRev); - final ITypedElement right = getTypedElementFor(repository, - rightGitPath, - rightRev); - - final ITypedElement commonAncestor; - if (left != null && right != null && !GitFileRevision.INDEX.equals(leftRev) - && !GitFileRevision.INDEX.equals(rightRev)) - commonAncestor = getTypedElementForCommonAncestor(repository, - rightGitPath, leftRev, rightRev); - else - commonAncestor = null; + private static void compareBetween(final Repository repository, + final String leftGitPath, final String rightGitPath, + final String leftRev, final String rightRev, + final IWorkbenchPage page) { - final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput( - left, right, commonAncestor, null); - in.getCompareConfiguration().setLeftLabel(leftRev); - in.getCompareConfiguration().setRightLabel(rightRev); + Job job = new Job(UIText.CompareUtils_jobName) { - if (page != null) - openInCompare(page, in); - else - CompareUI.openCompareEditor(in); + @Override + public IStatus run(IProgressMonitor monitor) { + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + final ITypedElement left; + final ITypedElement right; + try { + left = getTypedElementFor(repository, leftGitPath, leftRev); + right = getTypedElementFor(repository, rightGitPath, + rightRev); + } catch (IOException e) { + return Activator.createErrorStatus( + UIText.CompareWithRefAction_errorOnSynchronize, e); + } + final ITypedElement commonAncestor; + if (left != null && right != null + && !GitFileRevision.INDEX.equals(leftRev) + && !GitFileRevision.INDEX.equals(rightRev)) { + commonAncestor = getTypedElementForCommonAncestor( + repository, rightGitPath, leftRev, rightRev); + } else { + commonAncestor = null; + } + + final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput( + left, right, commonAncestor, null); + in.getCompareConfiguration().setLeftLabel(leftRev); + in.getCompareConfiguration().setRightLabel(rightRev); + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + openCompareEditorRunnable(page, in); + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); } private static String getRepoRelativePath(IPath location, diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java index c80b53c0e7..668e62cf33 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java @@ -2503,6 +2503,9 @@ public class UIText extends NLS { public static String CompareWithPreviousActionHandler_TitleRevisionNotFound; /** */ + public static String CompareUtils_jobName; + + /** */ public static String CompareUtils_errorCommonAncestor; /** */ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties index a34f27797a..771b840dea 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties @@ -304,6 +304,7 @@ CompareTreeView_NoInputText=No input CompareTreeView_RepositoryRootName=Repository root CompareTreeView_WorkspaceVersionText=Workspace CompareTreeView_CompareModeTooltip=Compare mode +CompareUtils_jobName=Git Compare... CompareUtils_errorGettingEncoding=Getting encoding failed CompareUtils_errorGettingHeadCommit=Getting HEAD commit failed |