[PATCH 2/3] Add compare with previous action.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When only one item is selected, add a compare action for the previous
version. 

This solution has a wart, such that when two consequent commits are
selected the menu shows only a "Show commit diff".

Signed-off-by: Robin Rosenberg <robin.rosenberg@xxxxxxxxxx>
---

 .../src/org/spearce/egit/ui/GitHistoryPage.java    |   32 +++++++++++++++++++++++
 .../internal/actions/GitCompareRevisionAction.java |   15 ++++++++++-
 2 files changed, 46 insertions(+), 1 deletions(-)

diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
index bd80324..5655105 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
@@ -16,10 +16,12 @@
  */
 package org.spearce.egit.ui;
 
+import java.io.IOException;
 import java.util.Date;
 
 import org.eclipse.compare.CompareConfiguration;
 import org.eclipse.compare.structuremergeviewer.ICompareInput;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IResourceChangeEvent;
 import org.eclipse.core.resources.IResourceChangeListener;
@@ -61,8 +63,12 @@ import org.eclipse.team.internal.ui.history.DialogHistoryPageSite;
 import org.eclipse.team.ui.history.HistoryPage;
 import org.eclipse.team.ui.history.IHistoryCompareAdapter;
 import org.eclipse.team.ui.history.IHistoryPageSite;
+import org.spearce.egit.core.GitProvider;
 import org.spearce.egit.core.internal.mapping.GitFileRevision;
+import org.spearce.egit.core.project.RepositoryMapping;
 import org.spearce.egit.ui.internal.actions.GitCompareRevisionAction;
+import org.spearce.jgit.lib.Commit;
+import org.spearce.jgit.lib.ObjectId;
 
 public class GitHistoryPage extends HistoryPage implements IAdaptable,
 		IHistoryCompareAdapter {
@@ -104,6 +110,8 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 
 		final GitCompareRevisionAction compareAction = new GitCompareRevisionAction(
 				"Compare");
+		final GitCompareRevisionAction compareActionPrev = new GitCompareRevisionAction(
+				"Show commit");
 		tree.addSelectionListener(new SelectionAdapter() {
 			public void widgetSelected(SelectionEvent e) {
 				// update the current
@@ -116,14 +124,38 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 				compareAction.setCurrentFileRevision(fileRevisions[0]);
 				compareAction.selectionChanged(new StructuredSelection(
 						selection2));
+				IProject project = ((IResource) getInput()).getProject();
+				GitProvider provider = (GitProvider)RepositoryProvider
+						.getProvider(project);
+				RepositoryMapping repositoryMapping = provider.getData().getRepositoryMapping(project);
+				ObjectId parentId = (ObjectId)((GitFileRevision)selection2[0]).getCommit().getParentIds().get(0);
+				try {
+					if (selection2.length == 1) {
+						Commit parent = repositoryMapping.getRepository().mapCommit(parentId);
+						IFileRevision previous = new GitFileRevision(parent,
+								((GitFileRevision)selection2[0]).getResource(),
+								((GitFileRevision)selection2[0]).getCount()+1);
+//						compareActionPrev.setCurrentFileRevision(selection2[0]);
+						compareActionPrev.setCurrentFileRevision(null);
+						compareActionPrev.selectionChanged(new StructuredSelection(new IFileRevision[] {selection2[0], previous}));
+					} else {
+						compareActionPrev.setCurrentFileRevision(null);
+						compareActionPrev.selectionChanged(new StructuredSelection(new IFileRevision[0]));
+					}
+				} catch (IOException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
+				}
 			}
 		});
 		compareAction.setPage(this);
+		compareActionPrev.setPage(this);
 		MenuManager menuMgr = new MenuManager();
 		Menu menu = menuMgr.createContextMenu(tree);
 		menuMgr.addMenuListener(new IMenuListener() {
 			public void menuAboutToShow(IMenuManager menuMgr) {
 				menuMgr.add(compareAction);
+				menuMgr.add(compareActionPrev);
 			}
 		});
 		menuMgr.setRemoveAllWhenShown(true);
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/GitCompareRevisionAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/GitCompareRevisionAction.java
index 9487197..d5780a5 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/GitCompareRevisionAction.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/GitCompareRevisionAction.java
@@ -32,8 +32,10 @@ import org.eclipse.ui.IReusableEditor;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.actions.BaseSelectionListenerAction;
 import org.spearce.egit.core.GitWorkspaceFileRevision;
+import org.spearce.egit.core.internal.mapping.GitFileRevision;
 import org.spearce.egit.ui.internal.GitCompareFileRevisionEditorInput;
 import org.spearce.egit.ui.internal.GitResourceNode;
+import org.spearce.jgit.lib.ObjectId;
 
 /**
  * Action to invoke a Git based compare on selected revivsions in the history window.
@@ -188,7 +190,18 @@ public class GitCompareRevisionAction extends BaseSelectionListenerAction {
 			return shouldShow();
 		}
 		else if (selection.size() == 2){
-			this.setText(TeamUIMessages.CompareRevisionAction_CompareWithOther);
+			IFileRevision rev1=(IFileRevision)selection.toArray()[0];
+			IFileRevision rev2=(IFileRevision)selection.toArray()[1];
+			System.out.println("Compare "+rev1.getContentIdentifier()+" with "+rev2.getContentIdentifier());
+			if (rev1 instanceof GitFileRevision && rev2 instanceof GitFileRevision) {
+				ObjectId pid = (ObjectId) ((GitFileRevision)rev1).getCommit().getParentIds().get(0);
+				if (pid.equals(((GitFileRevision)rev2).getCommit().getCommitId())) {
+					this.setText("Show commit diff");
+				} else {
+					this.setText(TeamUIMessages.CompareRevisionAction_CompareWithOther);
+				}
+			} else
+				this.setText(TeamUIMessages.CompareRevisionAction_CompareWithOther);
 			return shouldShow();
 		}
 

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]