Subject: [PATCH JGIT] Add "compare with index" action.

From: Yann Simon <[email-masked]>
Date: Wed, 11 Feb 2009 16:44:17 +0100

In the Compare With... menu, the "compare with index" action opens
a diff editor that compares the workspace version of a file and its
index version.

Signed-off-by: Yann Simon <[email-masked]>
---
My plan with the compare editor is to allow to add some contents into
the index (partial commits).
I do not know yet how to achieve this.

I did not find how to commit only the index with the eclipse interface.
So I guess comparing with index does not bring a lot of value for the
moment.

-- yann

 .../core/internal/storage/GitFileRevision.java     |   24 ++++++
 org.spearce.egit.ui/plugin.properties              |    3 +
 org.spearce.egit.ui/plugin.xml                     |    7 ++
 .../internal/actions/CompareWithIndexAction.java   |   87
++++++++++++++++++++
 4 files changed, 121 insertions(+), 0 deletions(-)
 create mode 100644
org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java

diff --git
a/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java
b/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java
index 21ba19e..3c78dfc 100644
---
a/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java
+++
b/org.spearce.egit.core/src/org/spearce/egit/core/internal/storage/GitFileRevision.java
@@ -11,6 +11,7 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.team.core.history.IFileRevision;
@@ -49,6 +50,29 @@ public static GitFileRevision inCommit(final
Repository db,
         return new CommitFileRevision(db, commit, path, blobId);
     }
 
+    /**
+     * Obtain a file revision for a file in the working repository.
+     *
+     * @param resource
+     *            resource identifying the file in the working repository.
+     * @return revision implementation for the resource in the working
+     *         repository.
+     */
+    public static GitFileRevision inWorkspace(final IResource resource) {
+        return new WorkspaceFileRevision(resource);
+    }
+
+    /**
+     * @param db
+     *            the repository which contains the index to use.
+     * @param path
+     *            path of the resource in the index
+     * @return revision implementation for the given path in the index
+     */
+    public static GitFileRevision inIndex(final Repository db, final
String path) {
+        return new IndexFileRevision(db, path);
+    }
+
     private final String path;
 
     GitFileRevision(final String fileName) {
diff --git a/org.spearce.egit.ui/plugin.properties
b/org.spearce.egit.ui/plugin.properties
index fa043f1..0fc869b 100644
--- a/org.spearce.egit.ui/plugin.properties
+++ b/org.spearce.egit.ui/plugin.properties
@@ -31,6 +31,9 @@ Decorator_description=Shows Git specific information
on resources in projects un
 CompareWithRevisionAction_label=Compare With Git Revision
 CompareWithRevisionAction_tooltip=Compare With a Git Revision
 
+CompareWithIndexAction_label=Compare with index version
+CompareWithIndexAction_tooltip=Compare with index version
+
 ShowResourceInHistoryAction_label=Show in Resource History
 ShowResourceInHistoryAction_tooltip=Show selected files in the resource
history view.
 
diff --git a/org.spearce.egit.ui/plugin.xml b/org.spearce.egit.ui/plugin.xml
index 869108c..c706309 100644
--- a/org.spearce.egit.ui/plugin.xml
+++ b/org.spearce.egit.ui/plugin.xml
@@ -108,6 +108,13 @@
                label="%CommitAction_label"
                menubarPath="team.main/group1"
                tooltip="%CommitAction_tooltip"/>
+         <action
+              
class="org.spearce.egit.ui.internal.actions.CompareWithIndexAction"
+              
id="org.spearce.egit.ui.internal.actions.CompareWithIndexAction"
+               label="%CompareWithIndexAction_label"
+               menubarPath="compareWithMenu/gitCompareWithGroup"
+               tooltip="&amp;CompareWithIndexAction_tooltip">
+         </action>
       </objectContribution>
       <objectContribution
          id="org.spearce.egit.ui.resetto"
diff --git
a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java
b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java
new file mode 100644
index 0000000..a4af944
--- /dev/null
+++
b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CompareWithIndexAction.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2009, Yann Simon <[email-masked]>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.egit.ui.internal.actions;
+
+import org.eclipse.compare.CompareUI;
+import org.eclipse.compare.ITypedElement;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.team.core.history.IFileRevision;
+import org.eclipse.team.internal.ui.history.FileRevisionTypedElement;
+import org.spearce.egit.core.internal.storage.GitFileRevision;
+import org.spearce.egit.core.project.RepositoryMapping;
+import org.spearce.egit.ui.internal.GitCompareFileRevisionEditorInput;
+import org.spearce.jgit.lib.Repository;
+
+/**
+ * The "compare with index" action. This action opens a diff editor
comparing
+ * the file as found in the working directory and the version found in
the index
+ * of the repository.
+ */
+@SuppressWarnings("restriction")
+public class CompareWithIndexAction extends RepositoryAction {
+
+    @Override
+    public void execute(IAction action) {
+        final IResource resource = getSelectedResources()[0];
+        final RepositoryMapping mapping =
RepositoryMapping.getMapping(resource.getProject());
+        final Repository repository = mapping.getRepository();
+        final String gitPath = mapping.getRepoRelativePath(resource);
+
+        final IFileRevision baseFile =
GitFileRevision.inWorkspace(resource);
+        final IFileRevision nextFile =
GitFileRevision.inIndex(repository, gitPath);
+
+        final ITypedElement base = new FileRevisionTypedElement(baseFile);
+        final ITypedElement next = new FileRevisionTypedElement(nextFile);
+
+        final GitCompareFileRevisionEditorInput in = new
GitCompareFileRevisionEditorInput(
+                base, next, null);
+        CompareUI.openCompareEditor(in);
+    }
+
+    @Override
+    public boolean isEnabled() {
+        final IResource[] selectedResources = getSelectedResources();
+        if (selectedResources.length != 1)
+            return false;
+        final IResource resource = selectedResources[0];
+        final RepositoryMapping mapping =
RepositoryMapping.getMapping(resource.getProject());
+        return mapping != null;
+    }
+
+}
\ No newline at end of file
-- 
1.6.0.4