{"thread":{"id":"17746","subject":"[PATCH JGIT] Add \"compare with index\" action.","startedAt":"2009-02-12T14:18:26Z","lastAt":"2009-02-12T14:18:26Z","messageCount":1,"participants":["Yann Simon"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"104387","messageId":"49942FB2.9040903@gmail.com","threadId":"17746","inReplyTo":null,"subject":"[PATCH JGIT] Add \"compare with index\" action.","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-02-12T14:18:26Z","receivedAt":"2009-02-12T14:18:26Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"In the Compare With... menu, the \"compare with index\" action opens\na diff editor that compares the workspace version of a file and its\nindex version.\n\nThe local file can be modified and saved.\n\nSigned-off-by: Yann Simon <yann.simon.fr@gmail.com>\n---\nThis is the second version of the patch and replace the first one\n(except if it was in the time applied, in which case I will provide\na diff).\n\nThis version permits to modify and save the local file.\n\n-- yann\n\n .../core/internal/storage/GitFileRevision.java     |   11 +++\n org.spearce.egit.ui/plugin.properties              |    3 +\n org.spearce.egit.ui/plugin.xml                     |    7 ++\n .../GitCompareFileRevisionEditorInput.java         |   24 +++--\n .../internal/actions/CompareWithIndexAction.java   |   92\n++++++++++++++++++++\n 5 files changed, 127 insertions(+), 10 deletions(-)\n create mode 100644\norg.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java\n\ndiff --git\na/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java\nb/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java\nindex 21ba19e..2f23c7d 100644\n---\na/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java\n+++\nb/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java\n@@ -49,6 +49,17 @@ public static GitFileRevision inCommit(final\nRepository db,\n         return new CommitFileRevision(db, commit, path, blobId);\n     }\n \n+    /**\n+     * @param db\n+     *            the repository which contains the index to use.\n+     * @param path\n+     *            path of the resource in the index\n+     * @return revision implementation for the given path in the index\n+     */\n+    public static GitFileRevision inIndex(final Repository db, final\nString path) {\n+        return new IndexFileRevision(db, path);\n+    }\n+\n     private final String path;\n \n     GitFileRevision(final String fileName) {\ndiff --git a/org.spearce.egit.ui/plugin.properties\nb/org.spearce.egit.ui/plugin.properties\nindex fa043f1..0fc869b 100644\n--- a/org.spearce.egit.ui/plugin.properties\n+++ b/org.spearce.egit.ui/plugin.properties\n@@ -31,6 +31,9 @@ Decorator_description=Shows Git specific information\non resources in projects un\n CompareWithRevisionAction_label=Compare With Git Revision\n CompareWithRevisionAction_tooltip=Compare With a Git Revision\n \n+CompareWithIndexAction_label=Compare with index version\n+CompareWithIndexAction_tooltip=Compare with index version\n+\n ShowResourceInHistoryAction_label=Show in Resource History\n ShowResourceInHistoryAction_tooltip=Show selected files in the resource\nhistory view.\n \ndiff --git a/org.spearce.egit.ui/plugin.xml b/org.spearce.egit.ui/plugin.xml\nindex 869108c..c706309 100644\n--- a/org.spearce.egit.ui/plugin.xml\n+++ b/org.spearce.egit.ui/plugin.xml\n@@ -108,6 +108,13 @@\n                label=\"%CommitAction_label\"\n                menubarPath=\"team.main/group1\"\n                tooltip=\"%CommitAction_tooltip\"/>\n+         <action\n+              \nclass=\"org.spearce.egit.ui.internal.actions.CompareWithIndexAction\"\n+              \nid=\"org.spearce.egit.ui.internal.actions.CompareWithIndexAction\"\n+               label=\"%CompareWithIndexAction_label\"\n+               menubarPath=\"compareWithMenu/gitCompareWithGroup\"\n+               tooltip=\"&amp;CompareWithIndexAction_tooltip\">\n+         </action>\n       </objectContribution>\n       <objectContribution\n          id=\"org.spearce.egit.ui.resetto\"\ndiff --git\na/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitCompareFileRevisionEditorInput.java\nb/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitCompareFileRevisionEditorInput.java\nindex 8aa076f..a54c2ee 100644\n---\na/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitCompareFileRevisionEditorInput.java\n+++\nb/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitCompareFileRevisionEditorInput.java\n@@ -11,7 +11,6 @@\n import java.lang.reflect.InvocationTargetException;\n \n import org.eclipse.compare.CompareConfiguration;\n-import org.eclipse.compare.CompareEditorInput;\n import org.eclipse.compare.IEditableContent;\n import org.eclipse.compare.IResourceProvider;\n import org.eclipse.compare.ITypedElement;\n@@ -33,14 +32,16 @@\n import org.eclipse.team.internal.ui.TeamUIPlugin;\n import org.eclipse.team.internal.ui.Utils;\n import org.eclipse.team.internal.ui.history.FileRevisionTypedElement;\n+import\norg.eclipse.team.internal.ui.history.CompareFileRevisionEditorInput.MyDiffNode;\n import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;\n+import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;\n import org.eclipse.ui.IWorkbenchPage;\n \n /**\n  * The input provider for the compare editor when working on resources\n  * under Git control.\n  */\n-public class GitCompareFileRevisionEditorInput extends CompareEditorInput {\n+public class GitCompareFileRevisionEditorInput extends\nSaveableCompareEditorInput {\n \n     private ITypedElement left;\n     private ITypedElement right;\n@@ -52,7 +53,7 @@\n      * @param page\n      */\n     public GitCompareFileRevisionEditorInput(ITypedElement left,\nITypedElement right, IWorkbenchPage page) {\n-        super(new CompareConfiguration());\n+        super(new CompareConfiguration(), page);\n         this.left = left;\n         this.right = right;\n     }\n@@ -326,12 +327,13 @@ private String getContentIdentifier(ITypedElement\nelement){\n         return TeamUIMessages.CompareFileRevisionEditorInput_2;\n     }\n \n-//    /* (non-Javadoc)\n-//     * @see\norg.eclipse.team.ui.synchronize.LocalResourceCompareEditorInput#fireInputChange()\n-//     */\n-//    protected void fireInputChange() {\n-//        ((DiffNode)getCompareResult()).fireChange();\n-//    }\n+    /* (non-Javadoc)\n+     * @see\norg.eclipse.team.ui.synchronize.SaveableCompareEditorInput#fireInputChange()\n+     */\n+    @Override\n+    protected void fireInputChange() {\n+        ((MyDiffNode)getCompareResult()).fireChange();\n+    }\n //\n //    /* (non-Javadoc)\n //     * @see\norg.eclipse.team.ui.synchronize.SaveableCompareEditorInput#contentsCreated()\n@@ -359,7 +361,9 @@ private LocalResourceTypedElement getLocalElement() {\n         return null;\n     }\n \n-    protected Object prepareInput(IProgressMonitor monitor) throws\nInvocationTargetException, InterruptedException {\n+    @Override\n+    protected ICompareInput prepareCompareInput(IProgressMonitor monitor)\n+            throws InvocationTargetException, InterruptedException {\n         ICompareInput input = createCompareInput();\n         getCompareConfiguration().setLeftEditable(isLeftEditable(input));\n         getCompareConfiguration().setRightEditable(false);\ndiff --git\na/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java\nb/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java\nnew file mode 100644\nindex 0000000..7e14cc9\n--- /dev/null\n+++\nb/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java\n@@ -0,0 +1,92 @@\n+/*\n+ * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Git Development Community nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package org.spearce.egit.ui.internal.actions;\n+\n+import org.eclipse.compare.CompareUI;\n+import org.eclipse.compare.ITypedElement;\n+import org.eclipse.core.resources.IFile;\n+import org.eclipse.core.resources.IResource;\n+import org.eclipse.jface.action.IAction;\n+import org.eclipse.team.core.history.IFileRevision;\n+import org.eclipse.team.internal.ui.history.FileRevisionTypedElement;\n+import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;\n+import org.spearce.egit.core.internal.storage.GitFileRevision;\n+import org.spearce.egit.core.project.RepositoryMapping;\n+import org.spearce.egit.ui.internal.GitCompareFileRevisionEditorInput;\n+import org.spearce.jgit.lib.Repository;\n+\n+/**\n+ * The \"compare with index\" action. This action opens a diff editor\ncomparing\n+ * the file as found in the working directory and the version found in\nthe index\n+ * of the repository.\n+ */\n+@SuppressWarnings(\"restriction\")\n+public class CompareWithIndexAction extends RepositoryAction {\n+\n+    @Override\n+    public void execute(IAction action) {\n+        final IResource resource = getSelectedResources()[0];\n+        final RepositoryMapping mapping =\nRepositoryMapping.getMapping(resource.getProject());\n+        final Repository repository = mapping.getRepository();\n+        final String gitPath = mapping.getRepoRelativePath(resource);\n+\n+        final IFileRevision nextFile =\nGitFileRevision.inIndex(repository, gitPath);\n+\n+        final IFile baseFile = (IFile)resource;\n+        final ITypedElement base =\nSaveableCompareEditorInput.createFileElement(baseFile);\n+        final ITypedElement next = new FileRevisionTypedElement(nextFile);\n+\n+        final GitCompareFileRevisionEditorInput in = new\nGitCompareFileRevisionEditorInput(\n+                base, next, null);\n+        CompareUI.openCompareEditor(in);\n+    }\n+\n+    @Override\n+    public boolean isEnabled() {\n+        final IResource[] selectedResources = getSelectedResources();\n+        if (selectedResources.length != 1)\n+            return false;\n+        final IResource resource = selectedResources[0];\n+        if (!(resource instanceof IFile)) {\n+            return false;\n+        }\n+        final RepositoryMapping mapping =\nRepositoryMapping.getMapping(resource.getProject());\n+        return mapping != null;\n+    }\n+\n+}\n\\ No newline at end of file\n-- \n1.6.1.2\n"}]}