summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorLaurent Goubet2013-02-15 11:17:03 (EST)
committer Laurent Goubet2013-02-27 10:03:14 (EST)
commitad4cc959f859a493dd05054af6b8ce222a1425d3 (patch)
tree40eb2268339d8cf0585d68a1954f9b32fda7f03e
parentb90f4642b5d63ff2eb448fdc60bede40f48a3022 (diff)
downloadegit-ad4cc959f859a493dd05054af6b8ce222a1425d3.zip
egit-ad4cc959f859a493dd05054af6b8ce222a1425d3.tar.gz
egit-ad4cc959f859a493dd05054af6b8ce222a1425d3.tar.bz2
Consider the ancestor when comparing with remote revisionsrefs/changes/91/10391/6
This changes all of the actions where comparing with a common ancestor makes sense : 1. Compare With > Commit 2. Compare With > Branch, Tag, Reference 3. Double-click a commit in the history view 4. Right-click a commit in the history view > Compare With Workspace 5. Right-click a file in the history view > Compare With Workspace 6. Right-click a file in the commit viewer > Compare With Workspace The only two actions that were previously ignoring common ancestors were the comparison with a branch tag or reference, and comparisons from history/commit viewer "with the workspace". All were modified in order to factorize common code as much as possible. Comparisons of files located out of the workspace (java.io.File) was not functional because of a regression in HistoryPageInput introduced by 5246f101fed6fe7541aa1ce8f9fec5b778beb788 that made the "singleFile" input of the history view a Boolean instead of an actual File. Furthermore, comparing these "local" files, which can be done through actions numbered 3 to 6 above was incoherent : while 3 and 4 compared the HEAD revision, actions 5 and 6 compared the working directory, including local changes. All four now consistently take the local changes into account. Bug: 400943 Change-Id: I26bce7a1bde3b944f10f40eacbbdb7c41e486c0b
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/CompareUtils.java115
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithCommitActionHandler.java46
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithRefActionHandler.java37
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitFileDiffViewer.java54
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/HistoryPageInput.java4
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java95
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java166
7 files changed, 232 insertions, 285 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 aadacf1..c428b7f 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010-2012 SAP AG and others.
+ * Copyright (c) 2010, 2013 SAP AG and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -37,6 +37,8 @@ import org.eclipse.core.resources.mapping.ResourceMappingContext;
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.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
@@ -55,6 +57,7 @@ import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput.EmptyTypedElement;
import org.eclipse.egit.ui.internal.actions.CompareWithCommitActionHandler;
import org.eclipse.egit.ui.internal.merge.GitCompareEditorInput;
+import org.eclipse.egit.ui.internal.synchronize.compare.LocalNonWorkspaceTypedElement;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jgit.dircache.DirCache;
@@ -387,6 +390,116 @@ public class CompareUtils {
}
/**
+ * Opens a compare editor comparing the working directory version of the
+ * given IFile with the version of that file corresponding to
+ * {@code refName}.
+ *
+ * @param repository
+ * The repository to load file revisions from.
+ * @param file
+ * File to compare revisions for.
+ * @param refName
+ * Reference to compare with the workspace version of
+ * {@code file}. Can be either a commit ID, a reference or a
+ * branch name.
+ * @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.
+ */
+ public 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);
+
+ CompareEditorInput in = prepareCompareInput(repository, gitPath, base,
+ refName);
+
+ if (page != null)
+ openInCompare(page, in);
+ else
+ CompareUI.openCompareEditor(in);
+ }
+
+ /**
+ * Opens a compare editor comparing the working directory version of the
+ * given File with the version corresponding to {@code refName} of the same
+ * file.
+ *
+ * @param repository
+ * The repository to load file revisions from.
+ * @param file
+ * File to compare revisions for.
+ * @param refName
+ * Reference to compare with the workspace version of
+ * {@code file}. Can be either a commit ID, a reference or a
+ * branch name.
+ * @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.
+ */
+ public static void compareLocalWithRef(Repository repository, File file,
+ String refName, IWorkbenchPage page) throws IOException {
+ final String gitPath = getRepoRelativePath(repository, file);
+ final ITypedElement base = new LocalNonWorkspaceTypedElement(new Path(
+ file.getAbsolutePath()));
+
+ CompareEditorInput in = prepareCompareInput(repository, gitPath, base,
+ refName);
+
+ if (page != null)
+ openInCompare(page, in);
+ else
+ CompareUI.openCompareEditor(in);
+ }
+
+ /*
+ * Creates a compare input that can be used to compare a given local file
+ * with another reference. The given "base" element should always reflect a
+ * local file, either in the workspace (IFile) or on the file system
+ * (java.io.File) since we'll use "HEAD" to find a common ancestor of this
+ * base and the reference we compare it with.
+ */
+ private static CompareEditorInput prepareCompareInput(
+ Repository repository, String gitPath, ITypedElement base,
+ String refName) throws IOException {
+ final ObjectId destCommitId = repository.resolve(refName);
+ RevWalk rw = new RevWalk(repository);
+ RevCommit commit = rw.parseCommit(destCommitId);
+ rw.release();
+ final ITypedElement destCommit = getFileRevisionTypedElement(gitPath,
+ commit, repository);
+
+ final ITypedElement commonAncestor;
+ if (base != null && commit != null) {
+ final ObjectId headCommitId = repository.resolve(Constants.HEAD);
+ commonAncestor = getFileRevisionTypedElementForCommonAncestor(
+ gitPath, headCommitId, destCommitId, repository);
+ } else {
+ commonAncestor = null;
+ }
+
+ final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
+ base, destCommit, commonAncestor, null);
+ in.getCompareConfiguration().setRightLabel(refName);
+ return in;
+ }
+
+ private static String getRepoRelativePath(Repository repository, File file) {
+ IPath workdirPath = new Path(repository.getWorkTree().getPath());
+ IPath filePath = new Path(file.getPath()).setDevice(null);
+ return filePath.removeFirstSegments(workdirPath.segmentCount())
+ .toString();
+ }
+
+ /**
* Opens a compare editor. The working tree version of the given file is
* compared with the version in the HEAD commit. Use this method if the
* given file is outide the workspace.
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithCommitActionHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithCommitActionHandler.java
index aeea16a..b72dd2b 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithCommitActionHandler.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithCommitActionHandler.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (C) 2011, 2012 Mathias Kinzler <mathias.kinzler@sap.com> and others.
+ * Copyright (C) 2011, 2013 Mathias Kinzler <mathias.kinzler@sap.com> and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -11,26 +11,17 @@ package org.eclipse.egit.ui.internal.actions;
import java.io.IOException;
-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.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.history.CommitSelectionDialog;
import org.eclipse.jface.window.Window;
-import org.eclipse.jgit.lib.Constants;
-import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
@@ -55,32 +46,14 @@ public class CompareWithCommitActionHandler extends RepositoryActionHandler {
if (resources.length == 1 && resources[0] instanceof IFile) {
final IFile baseFile = (IFile) resources[0];
- final ITypedElement base = SaveableCompareEditorInput
- .createFileElement(baseFile);
-
- final ITypedElement next;
- final ITypedElement ancestor;
try {
- RepositoryMapping mapping = RepositoryMapping
- .getMapping(resources[0]);
- next = getElementForCommit(mapping.getRepository(), mapping
- .getRepoRelativePath(baseFile), dlg.getCommitId());
-
- ancestor = CompareUtils.getFileRevisionTypedElementForCommonAncestor(mapping
- .getRepoRelativePath(baseFile), repo.resolve(Constants.HEAD),
- dlg.getCommitId(), repo);
+ CompareUtils.compareWorkspaceWithRef(repo, baseFile, dlg
+ .getCommitId().getName(), null);
} catch (IOException e) {
Activator.handleError(
- UIText.CompareWithIndexAction_errorOnAddToIndex, e,
- true);
- return null;
+ UIText.CompareWithRefAction_errorOnSynchronize,
+ e, true);
}
-
- final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
- base, next, ancestor, null);
- in.getCompareConfiguration()
- .setRightLabel(dlg.getCommitId().name());
- CompareUI.openCompareEditor(in);
} else {
CompareTreeView view;
try {
@@ -95,15 +68,6 @@ public class CompareWithCommitActionHandler extends RepositoryActionHandler {
return null;
}
- private ITypedElement getElementForCommit(final Repository repository,
- final String gitPath, final ObjectId commitId) throws IOException {
- RevWalk rw = new RevWalk(repository);
- RevCommit commit = rw.parseCommit(commitId);
- rw.release();
-
- return CompareUtils.getFileRevisionTypedElement(gitPath, commit, repository);
- }
-
@Override
public boolean isEnabled() {
return selectionMapsToSingleRepository();
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithRefActionHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithRefActionHandler.java
index d5ec2c5..8a596eb 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithRefActionHandler.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithRefActionHandler.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (C) 2010, 2012 Mathias Kinzler <mathias.kinzler@sap.com> and others.
+ * Copyright (C) 2010, 2013 Mathias Kinzler <mathias.kinzler@sap.com> and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -16,26 +16,18 @@ package org.eclipse.egit.ui.internal.actions;
import java.io.IOException;
-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.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.CompareTargetSelectionDialog;
import org.eclipse.egit.ui.internal.dialogs.CompareTreeView;
import org.eclipse.egit.ui.internal.synchronize.GitModelSynchronize;
import org.eclipse.jface.window.Window;
-import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
@@ -80,24 +72,13 @@ public class CompareWithRefActionHandler extends RepositoryActionHandler {
}
private void showSingleFileComparison(IFile file, String refName) {
- final ITypedElement base = SaveableCompareEditorInput
- .createFileElement(file);
-
- final ITypedElement next;
try {
- RepositoryMapping mapping = RepositoryMapping.getMapping(file);
- next = getElementForRef(mapping.getRepository(),
- mapping.getRepoRelativePath(file), refName);
+ CompareUtils.compareWorkspaceWithRef(getRepository(), file,
+ refName, null);
} catch (IOException e) {
Activator.handleError(
- UIText.CompareWithIndexAction_errorOnAddToIndex, e, true);
- return;
+ UIText.CompareWithRefAction_errorOnSynchronize, e, true);
}
-
- final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
- base, next, null);
- in.getCompareConfiguration().setRightLabel(refName);
- CompareUI.openCompareEditor(in);
}
private void synchronizeModel(final IFile file, Repository repo,
@@ -112,16 +93,6 @@ public class CompareWithRefActionHandler extends RepositoryActionHandler {
}
}
- private ITypedElement getElementForRef(final Repository repository,
- final String gitPath, final String refName) throws IOException {
- ObjectId commitId = repository.resolve(refName + "^{commit}"); //$NON-NLS-1$
- RevWalk rw = new RevWalk(repository);
- RevCommit commit = rw.parseCommit(commitId);
- rw.release();
-
- return CompareUtils.getFileRevisionTypedElement(gitPath, commit, repository);
- }
-
@Override
public boolean isEnabled() {
return selectionMapsToSingleRepository();
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitFileDiffViewer.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitFileDiffViewer.java
index 9b67033..0ffe38d 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitFileDiffViewer.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitFileDiffViewer.java
@@ -3,6 +3,7 @@
* Copyright (C) 2012, Daniel Megert <daniel_megert@ch.ibm.com>
* Copyright (C) 2012, 2013 Robin Stocker <robin@nibor.org>
* Copyright (C) 2012, Gunnar Wagenknecht <gunnar@wagenknecht.org>
+ * Copyright (C) 2013, Laurent Goubet <laurent.goubet@obeo.fr>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -37,7 +38,6 @@ import org.eclipse.egit.ui.internal.EgitUiEditorUtils;
import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;
import org.eclipse.egit.ui.internal.blame.BlameOperation;
import org.eclipse.egit.ui.internal.synchronize.GitModelSynchronize;
-import org.eclipse.egit.ui.internal.synchronize.compare.LocalNonWorkspaceTypedElement;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
@@ -73,7 +73,6 @@ import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.team.core.history.IFileRevision;
-import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.IWorkbenchWindow;
@@ -538,43 +537,36 @@ public class CommitFileDiffViewer extends TableViewer {
}
void showWorkingDirectoryFileDiff(final FileDiff d) {
- final GitCompareFileRevisionEditorInput in;
-
final String p = d.getPath();
final RevCommit commit = d.getCommit();
- final ObjectId[] blobs = d.getBlobs();
- final ITypedElement base;
- final ITypedElement next;
+ if (commit == null) {
+ Activator.showError(UIText.GitHistoryPage_openFailed, null);
+ return;
+ }
+
+ IWorkbenchPage activePage = site.getWorkbenchWindow().getActivePage();
IFile file = ResourceUtil.getFileForLocation(getRepository(), p);
- if (file != null && commit != null) {
- if (!CompareUtils.canDirectlyOpenInCompare(file)) {
- try {
+ try {
+ if (file != null) {
+ if (!CompareUtils.canDirectlyOpenInCompare(file))
GitModelSynchronize.synchronizeModelWithWorkspace(file,
getRepository(), commit.getName());
- } catch (Exception e) {
- Activator.logError(UIText.GitHistoryPage_openFailed, e);
- Activator.showError(UIText.GitHistoryPage_openFailed, null);
- }
- return;
+ else
+ CompareUtils.compareWorkspaceWithRef(getRepository(), file,
+ commit.getName(), null);
+ } else {
+ IPath path = new Path(getRepository().getWorkTree()
+ .getAbsolutePath()).append(p);
+ File ioFile = path.toFile();
+ if (ioFile.exists())
+ CompareUtils.compareLocalWithRef(getRepository(), ioFile,
+ commit.getName(), activePage);
}
+ } catch (IOException e) {
+ Activator.logError(UIText.GitHistoryPage_openFailed, e);
+ Activator.showError(UIText.GitHistoryPage_openFailed, null);
}
-
- if (file != null)
- next = SaveableCompareEditorInput.createFileElement(file);
- else
- next = new LocalNonWorkspaceTypedElement(new Path(getRepository()
- .getWorkTree().getAbsolutePath()).append(p));
-
- if (d.getChange().equals(ChangeType.DELETE))
- base = new GitCompareFileRevisionEditorInput.EmptyTypedElement(""); //$NON-NLS-1$
- else
- base = CompareUtils.getFileRevisionTypedElement(p, commit,
- getRepository(), blobs[blobs.length - 1]);
-
- in = new GitCompareFileRevisionEditorInput(next, base, null);
- CompareUtils.openInCompare(site.getWorkbenchWindow().getActivePage(),
- in);
}
TreeWalk getTreeWalk() {
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/HistoryPageInput.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/HistoryPageInput.java
index 44825f5..f34e46b 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/HistoryPageInput.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/HistoryPageInput.java
@@ -69,9 +69,9 @@ public class HistoryPageInput {
public HistoryPageInput(final Repository repository, final File[] fileItems) {
this.repo = repository;
list = null;
- if (fileItems.length == 1) {
+ if (fileItems.length == 1 && fileItems[0].isFile()) {
singleItem = fileItems[0];
- singleFile = Boolean.valueOf(fileItems[0].isFile());
+ singleFile = fileItems[0];
} else {
singleItem = null;
singleFile = null;
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java
index e33057e..e7434e5 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java
@@ -1,6 +1,7 @@
/*******************************************************************************
* Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
* Copyright (C) 2012, Gunnar Wagenknecht <gunnar@wagenknecht.org>
+ * Copyright (C) 2013, Laurent Goubet <laurent.goubet@obeo.fr>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -11,24 +12,20 @@ package org.eclipse.egit.ui.internal.history.command;
import java.io.File;
import java.io.IOException;
-import java.util.Iterator;
-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.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.history.GitHistoryPage;
import org.eclipse.egit.ui.internal.synchronize.GitModelSynchronize;
import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jgit.lib.Constants;
-import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.handlers.HandlerUtil;
/**
* Compare the file content of a commit with the working tree
@@ -37,67 +34,31 @@ public class CompareWithWorkingTreeHandler extends
AbstractHistoryCommandHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = getSelection(getPage());
- if (selection.size() == 1) {
- Iterator<?> it = selection.iterator();
- RevCommit commit = (RevCommit) it.next();
- Object input = getPage().getInputInternal().getSingleFile();
- Repository repo = getRepository(event);
- ObjectId headCommit;
- try {
- headCommit = repo.resolve(Constants.HEAD);
- } catch (IOException e) {
- throw new ExecutionException(e.getMessage(), e);
- }
+ if (selection.isEmpty())
+ return null;
+
+ // Even if there's more than one element, only consider the first
+ RevCommit commit = (RevCommit) selection.getFirstElement();
+ Object input = getPage().getInputInternal().getSingleFile();
+ Repository repository = getRepository(event);
+
+ try {
+ IWorkbenchPage workBenchPage = HandlerUtil
+ .getActiveWorkbenchWindowChecked(event).getActivePage();
if (input instanceof IFile) {
IFile file = (IFile) input;
- if (CompareUtils.canDirectlyOpenInCompare(file)) {
- final RepositoryMapping mapping = RepositoryMapping
- .getMapping(file.getProject());
- final String gitPath = mapping.getRepoRelativePath(file);
- ITypedElement right = CompareUtils.getFileRevisionTypedElement(
- gitPath, commit, mapping.getRepository());
- final ITypedElement ancestor = CompareUtils.
- getFileRevisionTypedElementForCommonAncestor(
- gitPath, headCommit, commit, repo);
-
- final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
- SaveableCompareEditorInput.createFileElement(file),
- right, ancestor, null);
- openInCompare(event, in);
- } else {
- try {
- GitModelSynchronize.synchronizeModelWithWorkspace(file,
- repo, commit.getName());
- } catch (IOException e) {
- throw new ExecutionException(e.getMessage(), e);
- }
- }
- }
- if (input instanceof File) {
- File file = (File) input;
- // TODO can we create a ITypedElement from the local file?
- RevCommit leftCommit;
- RevWalk walk = new RevWalk(repo);
- try {
- leftCommit = walk.parseCommit(headCommit);
- } catch (Exception e) {
- throw new ExecutionException(e.getMessage(), e);
- } finally {
- walk.release();
- }
- final String gitPath = getRepoRelativePath(repo, file);
- ITypedElement left = CompareUtils.getFileRevisionTypedElement(
- gitPath, leftCommit, repo);
- ITypedElement right = CompareUtils.getFileRevisionTypedElement(
- gitPath, commit, repo);
- final ITypedElement ancestor = CompareUtils.
- getFileRevisionTypedElementForCommonAncestor(
- gitPath, headCommit, commit, repo);
- final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
- left, right, ancestor, null);
- openInCompare(event, in);
- return null;
- }
+ if (CompareUtils.canDirectlyOpenInCompare(file))
+ CompareUtils.compareWorkspaceWithRef(repository, file,
+ commit.getId().getName(), workBenchPage);
+ else
+ GitModelSynchronize.synchronizeModelWithWorkspace(file,
+ repository, commit.getName());
+ } else
+ CompareUtils.compareLocalWithRef(repository, (File) input,
+ commit.getId().getName(), workBenchPage);
+ } catch (IOException e) {
+ Activator.handleError(
+ UIText.CompareWithRefAction_errorOnSynchronize, e, true);
}
return null;
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java
index 8cba50d..8601638 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
+ * Copyright (C) 2010, 2013 Mathias Kinzler <mathias.kinzler@sap.com> and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -14,7 +14,6 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import org.eclipse.compare.ITypedElement;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
@@ -25,18 +24,16 @@ 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.EgitUiEditorUtils;
-import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;
import org.eclipse.egit.ui.internal.history.GitHistoryPage;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.history.IFileRevision;
-import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.handlers.HandlerUtil;
/**
* Show versions/open.
@@ -46,127 +43,76 @@ import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
*/
public class ShowVersionsHandler extends AbstractHistoryCommandHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
- boolean compareMode = Boolean.TRUE.toString().equals(
- event.getParameter(HistoryViewCommands.COMPARE_MODE_PARAM));
IStructuredSelection selection = getSelection(getPage());
- if (selection.size() < 1)
- return null;
Object input = getPage().getInputInternal().getSingleFile();
- if (input == null)
+ if (selection.size() < 1 || input == null)
return null;
- boolean errorOccurred = false;
- List<ObjectId> ids = new ArrayList<ObjectId>();
+
+ boolean compareMode = Boolean.parseBoolean(event
+ .getParameter(HistoryViewCommands.COMPARE_MODE_PARAM));
+ Repository repository = getRepository(event);
+
String gitPath = null;
- Repository repo = getRepository(event);
- ObjectId headCommit;
- try {
- headCommit = repo.resolve(Constants.HEAD);
- } catch (IOException e) {
- throw new ExecutionException(e.getMessage(), e);
- }
if (input instanceof IFile) {
IFile resource = (IFile) input;
- final RepositoryMapping map = RepositoryMapping
+ RepositoryMapping map = RepositoryMapping
.getMapping(resource);
gitPath = map.getRepoRelativePath(resource);
- Iterator<?> it = selection.iterator();
- while (it.hasNext()) {
- RevCommit commit = (RevCommit) it.next();
- IFileRevision rev = null;
+ } else if (input instanceof File) {
+ File fileInput = (File) input;
+ gitPath = getRepoRelativePath(repository, fileInput);
+ } else {
+ // Should be unreachable
+ return null;
+ }
+
+ boolean errorOccurred = false;
+ List<ObjectId> ids = new ArrayList<ObjectId>();
+
+ Iterator<?> it = selection.iterator();
+ while (it.hasNext()) {
+ RevCommit commit = (RevCommit) it.next();
+ IFileRevision revision = null;
+ try {
+ revision = CompareUtils.getFileRevision(gitPath, commit,
+ repository, null);
+ } catch (IOException e) {
+ Activator.logError(NLS.bind(
+ UIText.GitHistoryPage_errorLookingUpPath, gitPath,
+ commit.getId()), e);
+ errorOccurred = true;
+ }
+
+ if (revision == null)
+ ids.add(commit.getId());
+ else if (compareMode) {
try {
- rev = CompareUtils.getFileRevision(gitPath, commit, map
- .getRepository(), null);
+ IWorkbenchPage workBenchPage = HandlerUtil
+ .getActiveWorkbenchWindowChecked(event)
+ .getActivePage();
+ if (input instanceof IFile)
+ CompareUtils.compareWorkspaceWithRef(repository,
+ (IFile) input, commit.getId().getName(),
+ workBenchPage);
+ else
+ CompareUtils.compareLocalWithRef(repository,
+ (File) input, commit.getId().getName(),
+ workBenchPage);
} catch (IOException e) {
- Activator.logError(NLS.bind(
- UIText.GitHistoryPage_errorLookingUpPath, gitPath,
- commit.getId()), e);
+ Activator.logError(UIText.GitHistoryPage_openFailed, e);
errorOccurred = true;
}
- if (rev != null) {
- if (compareMode) {
- ITypedElement right = CompareUtils
- .getFileRevisionTypedElement(gitPath, commit,
- map.getRepository());
- ITypedElement ancestor = CompareUtils.
- getFileRevisionTypedElementForCommonAncestor(
- gitPath, headCommit, commit, repo);
- final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
- SaveableCompareEditorInput
- .createFileElement(resource), right, ancestor,
- null);
- try {
- openInCompare(event, in);
- } catch (Exception e) {
- errorOccurred = true;
- }
- } else {
- try {
- EgitUiEditorUtils.openEditor(getPart(event)
- .getSite().getPage(), rev,
- new NullProgressMonitor());
- } catch (CoreException e) {
- Activator.logError(
- UIText.GitHistoryPage_openFailed, e);
- errorOccurred = true;
- }
- }
- } else {
- ids.add(commit.getId());
- }
- }
- }
- if (input instanceof File) {
- File fileInput = (File) input;
- gitPath = getRepoRelativePath(repo, fileInput);
- Iterator<?> it = selection.iterator();
- while (it.hasNext()) {
- RevCommit commit = (RevCommit) it.next();
- IFileRevision rev = null;
+ } else {
try {
- rev = CompareUtils.getFileRevision(gitPath, commit, repo,
- null);
- } catch (IOException e) {
- Activator.logError(NLS.bind(
- UIText.GitHistoryPage_errorLookingUpPath, gitPath,
- commit.getId()), e);
+ EgitUiEditorUtils.openEditor(getPart(event).getSite()
+ .getPage(), revision, new NullProgressMonitor());
+ } catch (CoreException e) {
+ Activator.logError(UIText.GitHistoryPage_openFailed, e);
errorOccurred = true;
}
- if (rev != null) {
- if (compareMode) {
- try {
- ITypedElement left = CompareUtils
- .getFileRevisionTypedElement(gitPath,
- new RevWalk(repo).parseCommit(repo
- .resolve(Constants.HEAD)),
- repo);
- ITypedElement right = CompareUtils
- .getFileRevisionTypedElement(gitPath,
- commit, repo);
- ITypedElement ancestor = CompareUtils.
- getFileRevisionTypedElementForCommonAncestor(
- gitPath, headCommit, commit, repo);
- final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
- left, right, ancestor, null);
- openInCompare(event, in);
- } catch (IOException e) {
- errorOccurred = true;
- }
- } else {
- try {
- EgitUiEditorUtils.openEditor(getPart(event)
- .getSite().getPage(), rev,
- new NullProgressMonitor());
- } catch (CoreException e) {
- Activator.logError(
- UIText.GitHistoryPage_openFailed, e);
- errorOccurred = true;
- }
- }
- } else {
- ids.add(commit.getId());
- }
}
}
+
if (errorOccurred)
Activator.showError(UIText.GitHistoryPage_openFailed, null);
if (ids.size() > 0) {