diff options
| author | Kevin Sawicki | 2011-04-27 19:12:55 +0000 |
|---|---|---|
| committer | Kevin Sawicki | 2011-04-27 19:12:55 +0000 |
| commit | 4a28945a38994e519a6c5eb9b8dd3c1a82487dda (patch) | |
| tree | dc2bf7f5aadcab40b491a9e2af2fb9b1a8194cbf | |
| parent | 2771482d4f9975442e4f35f83d1eea64cbd7951d (diff) | |
| download | egit-4a28945a38994e519a6c5eb9b8dd3c1a82487dda.tar.gz egit-4a28945a38994e519a6c5eb9b8dd3c1a82487dda.tar.xz egit-4a28945a38994e519a6c5eb9b8dd3c1a82487dda.zip | |
Add compare with previous revision menu.
This opens the compare editor for single file
selections and the Git Tree compare view for
non-file selections.
Bug: 344026
Change-Id: Ibb1cadfdc56376a5cf0110b8cc0105d8067cd20d
Signed-off-by: Kevin Sawicki <kevin@github.com>
7 files changed, 223 insertions, 1 deletions
diff --git a/org.eclipse.egit.ui/plugin.properties b/org.eclipse.egit.ui/plugin.properties index 936bbf579c..77fd6dad5e 100644 --- a/org.eclipse.egit.ui/plugin.properties +++ b/org.eclipse.egit.ui/plugin.properties @@ -263,4 +263,6 @@ RenameBranchMenu.label = Rename Branch... DeleteBranchMenu.label = Delete Branch... DeleteBranchCommand.name = Delete Branch RenameBranchCommand.name = Rename Branch -CompareInTreeCommand.name = Compare in Tree
\ No newline at end of file +CompareInTreeCommand.name = Compare in Tree +CompareWithPreviousCommand.name = Compare with Previous Revision +CompareWithPreviousAction.label = Previous Revision
\ No newline at end of file diff --git a/org.eclipse.egit.ui/plugin.xml b/org.eclipse.egit.ui/plugin.xml index 07fcff3d3b..01551fe030 100644 --- a/org.eclipse.egit.ui/plugin.xml +++ b/org.eclipse.egit.ui/plugin.xml @@ -116,6 +116,13 @@ label="%CompareWithBranchOrTagAction.label" menubarPath="compareWithMenu/gitCompareWithGroup"> </action> + <action + class="org.eclipse.egit.ui.internal.actions.CompareWithPreviousAction" + definitionId="org.eclipse.egit.ui.team.CompareWithPrevious" + enablesFor="1" + id="org.eclipse.egit.ui.internal.actions.CompareWithPreviousAction" + label="%CompareWithPreviousAction.label" + menubarPath="compareWithMenu/gitCompareWithGroup"/> <action class="org.eclipse.egit.ui.internal.actions.CompareWithHeadAction" id="org.eclipse.egit.ui.internal.actions.CompareWithHeadAction" @@ -3130,6 +3137,12 @@ id="org.eclipse.egit.ui.team.CompareWithHead" name="%CompareWithHeadCommand.name"> </command> + <command + categoryId="org.eclipse.egit.ui.commandCategory" + defaultHandler="org.eclipse.egit.ui.internal.actions.CompareWithPreviousActionHandler" + id="org.eclipse.egit.ui.team.CompareWithPrevious" + name="%CompareWithPreviousCommand.name"> + </command> <command categoryId="org.eclipse.egit.ui.commandCategory" id="org.eclipse.egit.ui.team.CompareWithRevision" diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java index 03cd122df1..17673ee784 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java @@ -1741,6 +1741,15 @@ public class UIText extends NLS { public static String CompareWithIndexAction_errorOnAddToIndex; /** */ + public static String CompareWithPreviousActionHandler_MessageRevisionNotFound; + + /** */ + public static String CompareWithPreviousActionHandler_TaskGeneratingInput; + + /** */ + public static String CompareWithPreviousActionHandler_TitleRevisionNotFound; + + /** */ public static String ConfirmationPage_cantConnectToAnyTitle; /** */ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java index e883a9cb7a..6d4b5b595a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java @@ -41,6 +41,9 @@ public class ActionCommands { /** "Compare with revision" action command id */ public static final String COMPARE_WITH_REVISION_ACTION = "org.eclipse.egit.ui.team.CompareWithRevision"; //$NON-NLS-1$ + /** "Compare with previous" action command id */ + public static final String COMPARE_WITH_PREVIOUS_ACTION = "org.eclipse.egit.ui.team.CompareWithPrevious"; //$NON-NLS-1$ + /** "Configure Fetch" action command id */ public static final String CONFIGURE_FETCH = "org.eclipse.egit.ui.team.ConfigureFetch"; //$NON-NLS-1$ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithPreviousAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithPreviousAction.java new file mode 100644 index 0000000000..7128f8a089 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithPreviousAction.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.ui.internal.actions; + +/** + * Compare with previous revision action. + */ +public class CompareWithPreviousAction extends RepositoryAction { + + /** + * Create compare with previous revision action + */ + public CompareWithPreviousAction() { + super(ActionCommands.COMPARE_WITH_PREVIOUS_ACTION, + new CompareWithPreviousActionHandler()); + } + +} diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithPreviousActionHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithPreviousActionHandler.java new file mode 100644 index 0000000000..45267d1223 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithPreviousActionHandler.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.ui.internal.actions; + +import java.io.IOException; +import java.text.MessageFormat; + +import org.eclipse.compare.CompareEditorInput; +import org.eclipse.compare.CompareUI; +import org.eclipse.compare.ITypedElement; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.jobs.ISchedulingRule; +import org.eclipse.egit.core.op.IEGitOperation; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.egit.ui.Activator; +import org.eclipse.egit.ui.UIText; +import org.eclipse.egit.ui.internal.CompareUtils; +import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput; +import org.eclipse.egit.ui.internal.dialogs.CompareTreeView; +import org.eclipse.egit.ui.internal.job.JobUtil; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.FollowFilter; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; + +/** + * Compare with previous revision action handler. + */ +public class CompareWithPreviousActionHandler extends RepositoryActionHandler { + + private class CompareWithPreviousOperation implements IEGitOperation { + + private ExecutionEvent event; + + private Repository repository; + + private IResource resource; + + private CompareWithPreviousOperation(ExecutionEvent event, + Repository repository, IResource resource) { + this.event = event; + this.repository = repository; + this.resource = resource; + } + + private String getRepositoryPath() { + return RepositoryMapping.getMapping(resource.getProject()) + .getRepoRelativePath(resource); + } + + public void execute(IProgressMonitor monitor) throws CoreException { + RevCommit previous = findPreviousCommit(); + if (previous != null) + if (resource instanceof IFile) { + final ITypedElement base = SaveableCompareEditorInput + .createFileElement((IFile) resource); + ITypedElement next = CompareUtils + .getFileRevisionTypedElement(getRepositoryPath(), + previous, repository); + CompareEditorInput input = new GitCompareFileRevisionEditorInput( + base, next, null); + CompareUI.openCompareEditor(input); + } else + openCompareTreeView(previous); + else + showNotFoundDialog(); + } + + private void openCompareTreeView(final RevCommit previous) { + final Shell shell = HandlerUtil.getActiveShell(event); + shell.getDisplay().asyncExec(new Runnable() { + + public void run() { + try { + CompareTreeView view = (CompareTreeView) PlatformUI + .getWorkbench().getActiveWorkbenchWindow() + .getActivePage().showView(CompareTreeView.ID); + view.setInput(new IResource[] { resource }, + previous.name()); + } catch (PartInitException e) { + Activator.handleError(e.getMessage(), e, true); + } + } + }); + } + + private RevCommit findPreviousCommit() { + RevWalk rw = new RevWalk(repository); + try { + String path = getRepositoryPath(); + if (path.length() > 0) + rw.setTreeFilter(FollowFilter.create(path)); + RevCommit headCommit = rw.parseCommit(repository.getRef( + Constants.HEAD).getObjectId()); + rw.markStart(headCommit); + headCommit = rw.next(); + if (headCommit != null) + return rw.next(); + } catch (IOException e) { + Activator.handleError(e.getMessage(), e, true); + } finally { + rw.dispose(); + } + return null; + } + + private void showNotFoundDialog() { + final Shell shell = HandlerUtil.getActiveShell(event); + final String message = MessageFormat + .format(UIText.CompareWithPreviousActionHandler_MessageRevisionNotFound, + resource.getName()); + shell.getDisplay().asyncExec(new Runnable() { + + public void run() { + MessageDialog + .openWarning( + shell, + UIText.CompareWithPreviousActionHandler_TitleRevisionNotFound, + message); + } + }); + } + + public ISchedulingRule getSchedulingRule() { + return resource; + } + } + + /** + * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) + */ + public Object execute(ExecutionEvent event) throws ExecutionException { + Repository repository = getRepository(true, event); + if (repository == null) + return null; + + IResource[] resources = getSelectedResources(event); + if (resources.length == 1) + JobUtil.scheduleUserJob( + new CompareWithPreviousOperation(event, repository, + resources[0]), + UIText.CompareWithPreviousActionHandler_TaskGeneratingInput, + null); + return null; + } +} diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 453ca1c112..86ecfe9c95 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -567,6 +567,9 @@ CommitMessageViewer_GettingPreviousTagTaskName=Getting previous tag CommitMessageViewer_parent=Parent CommitMessageViewer_SelectOneCommitMessage=Please select exactly one commit CompareWithIndexAction_errorOnAddToIndex=Error during adding to index +CompareWithPreviousActionHandler_MessageRevisionNotFound=No previous revision of {0} could be found in the repository. +CompareWithPreviousActionHandler_TaskGeneratingInput=Generating comparison with previous revision +CompareWithPreviousActionHandler_TitleRevisionNotFound=Previous revision not found ConfirmationPage_cantConnectToAnyTitle=Can't Connect ConfirmationPage_cantConnectToAny=Can't connect to any URI: {0} |
