From 1ea384ca3fba3306a28eb33f725e09569a66842e Mon Sep 17 00:00:00 2001 From: Olesya Smirnova Date: Mon, 14 Feb 2005 14:14:38 +0300 Subject: [PATCH] Show differences actions --- .../com/intellij/openapi/vcs/AbstractVcs.java | 5 + .../openapi/vcs/diff/DiffProvider.java | 14 +++ .../openapi/vcs/history/VcsFileContent.java | 11 ++ .../openapi/vcs/history/VcsFileRevision.java | 8 +- .../vcs/history/VcsRevisionNumber.java | 4 + resources/src/idea/ActionManager.xml | 3 + .../vcs/actions/AbstractShowDiffAction.java | 111 ++++++++++++++++++ .../vcs/actions/CompareWithLastVersion.java | 11 ++ .../CompareWithSelectedRevisionAction.java | 69 +++++++++++ .../CompareWithTheSameVersionAction.java | 11 ++ 10 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 openapi/src/com/intellij/openapi/vcs/diff/DiffProvider.java create mode 100644 openapi/src/com/intellij/openapi/vcs/history/VcsFileContent.java create mode 100644 source/com/intellij/openapi/vcs/actions/AbstractShowDiffAction.java create mode 100644 source/com/intellij/openapi/vcs/actions/CompareWithLastVersion.java create mode 100644 source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java create mode 100644 source/com/intellij/openapi/vcs/actions/CompareWithTheSameVersionAction.java diff --git a/openapi/src/com/intellij/openapi/vcs/AbstractVcs.java b/openapi/src/com/intellij/openapi/vcs/AbstractVcs.java index 46e13f778444..868d52e563f9 100644 --- a/openapi/src/com/intellij/openapi/vcs/AbstractVcs.java +++ b/openapi/src/com/intellij/openapi/vcs/AbstractVcs.java @@ -13,6 +13,7 @@ import com.intellij.openapi.vcs.fileView.FileViewEnvironment; import com.intellij.openapi.vcs.history.VcsHistoryProvider; import com.intellij.openapi.vcs.update.UpdateEnvironment; import com.intellij.openapi.vcs.annotate.AnnotationProvider; +import com.intellij.openapi.vcs.diff.DiffProvider; public abstract class AbstractVcs { @@ -135,4 +136,8 @@ public abstract class AbstractVcs { public AnnotationProvider getAnnotationProvider(){ return null; } + + public DiffProvider getDiffProvider() { + return null; + } } diff --git a/openapi/src/com/intellij/openapi/vcs/diff/DiffProvider.java b/openapi/src/com/intellij/openapi/vcs/diff/DiffProvider.java new file mode 100644 index 000000000000..3155c5ef3e91 --- /dev/null +++ b/openapi/src/com/intellij/openapi/vcs/diff/DiffProvider.java @@ -0,0 +1,14 @@ +package com.intellij.openapi.vcs.diff; + +import com.intellij.openapi.vcs.history.VcsFileContent; +import com.intellij.openapi.vcs.history.VcsRevisionNumber; +import com.intellij.openapi.vfs.VirtualFile; + +public interface DiffProvider { + + VcsRevisionNumber getCurrentRevision(VirtualFile file); + + VcsRevisionNumber getLastRevision(VirtualFile virtualFile); + + VcsFileContent createFileContent(VcsRevisionNumber revisionNumber, VirtualFile selectedFile); +} diff --git a/openapi/src/com/intellij/openapi/vcs/history/VcsFileContent.java b/openapi/src/com/intellij/openapi/vcs/history/VcsFileContent.java new file mode 100644 index 000000000000..12ff8ac3b3e7 --- /dev/null +++ b/openapi/src/com/intellij/openapi/vcs/history/VcsFileContent.java @@ -0,0 +1,11 @@ +package com.intellij.openapi.vcs.history; + +import com.intellij.openapi.vcs.VcsException; + +import java.io.IOException; + +public interface VcsFileContent { + void loadContent() throws VcsException; + + byte[] getContent() throws IOException; +} diff --git a/openapi/src/com/intellij/openapi/vcs/history/VcsFileRevision.java b/openapi/src/com/intellij/openapi/vcs/history/VcsFileRevision.java index d55e996481b0..b6f24ae73795 100644 --- a/openapi/src/com/intellij/openapi/vcs/history/VcsFileRevision.java +++ b/openapi/src/com/intellij/openapi/vcs/history/VcsFileRevision.java @@ -4,12 +4,9 @@ */ package com.intellij.openapi.vcs.history; -import com.intellij.openapi.vcs.VcsException; - -import java.io.IOException; import java.util.Date; -public interface VcsFileRevision { +public interface VcsFileRevision extends VcsFileContent { VcsFileRevision NULL = new VcsFileRevision() { public VcsRevisionNumber getRevisionNumber() { return VcsRevisionNumber.NULL; @@ -43,6 +40,5 @@ public interface VcsFileRevision { Date getRevisionDate(); String getAuthor(); String getCommitMessage(); - void loadContent() throws VcsException; - byte[] getContent() throws IOException; + } diff --git a/openapi/src/com/intellij/openapi/vcs/history/VcsRevisionNumber.java b/openapi/src/com/intellij/openapi/vcs/history/VcsRevisionNumber.java index e48bda8a7d32..ecb52e2a243e 100644 --- a/openapi/src/com/intellij/openapi/vcs/history/VcsRevisionNumber.java +++ b/openapi/src/com/intellij/openapi/vcs/history/VcsRevisionNumber.java @@ -48,6 +48,10 @@ public interface VcsRevisionNumber extends Comparable{ } return 0; } + + public long getLongValue() { + return myValue; + } } String asString(); diff --git a/resources/src/idea/ActionManager.xml b/resources/src/idea/ActionManager.xml index e014ebbc0590..93866019eb03 100644 --- a/resources/src/idea/ActionManager.xml +++ b/resources/src/idea/ActionManager.xml @@ -148,6 +148,9 @@ + + + diff --git a/source/com/intellij/openapi/vcs/actions/AbstractShowDiffAction.java b/source/com/intellij/openapi/vcs/actions/AbstractShowDiffAction.java new file mode 100644 index 000000000000..eadeb45d639e --- /dev/null +++ b/source/com/intellij/openapi/vcs/actions/AbstractShowDiffAction.java @@ -0,0 +1,111 @@ +package com.intellij.openapi.vcs.actions; + +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.diff.DiffManager; +import com.intellij.openapi.diff.DocumentContent; +import com.intellij.openapi.diff.SimpleContent; +import com.intellij.openapi.diff.SimpleDiffRequest; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.*; +import com.intellij.openapi.vcs.diff.DiffProvider; +import com.intellij.openapi.vcs.history.VcsFileContent; +import com.intellij.openapi.vcs.history.VcsRevisionNumber; +import com.intellij.openapi.vfs.VirtualFile; + +import java.io.IOException; + +public abstract class AbstractShowDiffAction extends AbstractVcsAction{ + + protected void update(VcsContext vcsContext, Presentation presentation) { + updateDiffAction(presentation, vcsContext); + } + + protected static void updateDiffAction(final Presentation presentation, final VcsContext vcsContext) { + presentation.setEnabled(isEnabled(vcsContext)); + presentation.setVisible(isVisible(vcsContext)); + } + + protected static boolean isVisible(final VcsContext vcsContext) { + final Project project = vcsContext.getProject(); + if (project == null) return false; + final VirtualFile[] selectedFilePaths = vcsContext.getSelectedFiles(); + if (selectedFilePaths == null || selectedFilePaths.length != 1) return false; + + final VirtualFile selectedFile = selectedFilePaths[0]; + + final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(selectedFile); + if (vcs == null) return false; + + final DiffProvider diffProvider = vcs.getDiffProvider(); + + if (diffProvider == null) return false; + + return true; + + } + + protected static boolean isEnabled(final VcsContext vcsContext) { + if (!(isVisible(vcsContext))) return false; + + final Project project = vcsContext.getProject(); + if (project == null) return false; + final VirtualFile[] selectedFilePaths = vcsContext.getSelectedFiles(); + if (selectedFilePaths == null || selectedFilePaths.length != 1) return false; + + final VirtualFile selectedFile = selectedFilePaths[0]; + + final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(selectedFile); + if (vcs == null) return false; + + final DiffProvider diffProvider = vcs.getDiffProvider(); + + if (diffProvider == null) return false; + + return vcs.fileExistsInVcs(new FilePathImpl(selectedFile)) ; + } + + + protected void actionPerformed(VcsContext vcsContext) { + final Project project = vcsContext.getProject(); + final VirtualFile selectedFile = vcsContext.getSelectedFiles()[0]; + + final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(selectedFile); + final DiffProvider diffProvider = vcs.getDiffProvider(); + + VcsRevisionNumber revisionNumber = getRevisionNumber(diffProvider, selectedFile); + + try { + final VcsFileContent fileRevision = diffProvider.createFileContent(revisionNumber, selectedFile); + fileRevision.loadContent(); + + final SimpleDiffRequest request = + new SimpleDiffRequest(project, "Compare local and " + revisionNumber.asString() + " versions"); + final SimpleContent content1 = new SimpleContent(new String(fileRevision.getContent(), selectedFile.getCharset().name()), selectedFile.getFileType()); + final DocumentContent content2 = new DocumentContent(project, FileDocumentManager.getInstance().getDocument(selectedFile)); + + final VcsRevisionNumber currentRevision = diffProvider.getCurrentRevision(selectedFile); + final VcsRevisionNumber lastRevision = diffProvider.getLastRevision(selectedFile); + + if (lastRevision.compareTo(currentRevision) > 0) { + request.setContents(content2, content1); + request.setContentTitles("Local", revisionNumber.asString()); + } else { + request.setContents(content1, content2); + request.setContentTitles(revisionNumber.asString(), "Local"); + } + + DiffManager.getInstance().getDiffTool().show(request); + } + catch (VcsException e) { + AbstractVcsHelper.getInstance(project).showError(e, "Diff"); + } + catch (IOException e) { + AbstractVcsHelper.getInstance(project).showError(new VcsException(e), "Diff"); + } + + + } + + protected abstract VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file); +} diff --git a/source/com/intellij/openapi/vcs/actions/CompareWithLastVersion.java b/source/com/intellij/openapi/vcs/actions/CompareWithLastVersion.java new file mode 100644 index 000000000000..cf830c1cc43d --- /dev/null +++ b/source/com/intellij/openapi/vcs/actions/CompareWithLastVersion.java @@ -0,0 +1,11 @@ +package com.intellij.openapi.vcs.actions; + +import com.intellij.openapi.vcs.history.VcsRevisionNumber; +import com.intellij.openapi.vcs.diff.DiffProvider; +import com.intellij.openapi.vfs.VirtualFile; + +public class CompareWithLastVersion extends AbstractShowDiffAction{ + protected VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file) { + return diffProvider.getLastRevision(file); + } +} diff --git a/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java b/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java new file mode 100644 index 000000000000..afbf2350233f --- /dev/null +++ b/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java @@ -0,0 +1,69 @@ +package com.intellij.openapi.vcs.actions; + +import com.intellij.openapi.actionSystem.ActionGroup; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.ProjectLevelVcsManager; +import com.intellij.openapi.vcs.AbstractVcs; +import com.intellij.openapi.vcs.FilePathImpl; +import com.intellij.openapi.vcs.VcsException; +import com.intellij.openapi.vcs.diff.DiffProvider; +import com.intellij.openapi.vcs.history.VcsHistoryProvider; +import com.intellij.openapi.vcs.history.VcsHistorySession; +import com.intellij.openapi.vcs.history.VcsFileRevision; +import com.intellij.openapi.vcs.history.VcsRevisionNumber; +import com.intellij.peer.PeerFactory; + +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; +import java.text.SimpleDateFormat; +import java.text.DateFormat; + +public class CompareWithSelectedRevisionAction extends ActionGroup{ + private static final DateFormat DATE_FORMAT = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT); + + public void update(AnActionEvent e) { + final VcsContext vcsContext = createVcsContext(e); + AbstractShowDiffAction.updateDiffAction(e.getPresentation(), vcsContext); + } + + private VcsContext createVcsContext(final AnActionEvent e) { + return PeerFactory.getInstance().getVcsContextFactory().createOn(e); + } + + public AnAction[] getChildren(AnActionEvent e) { + final VcsContext vcsContext = createVcsContext(e); + final VirtualFile file = vcsContext.getSelectedFiles()[0]; + final Project project = vcsContext.getProject(); + final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file); + final VcsHistoryProvider vcsHistoryProvider = vcs.getVcsHistoryProvider(); + final ArrayList actions = new ArrayList(); + + try { + final VcsHistorySession session = vcsHistoryProvider.createSessionFor(new FilePathImpl(file)); + final List revisions = session.getRevisionList(); + for (Iterator iterator = revisions.iterator(); iterator.hasNext();) { + final VcsFileRevision vcsFileRevision = iterator.next(); + actions.add(new AbstractShowDiffAction() { + protected VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file) { + return vcsFileRevision.getRevisionNumber(); + } + + protected void update(VcsContext vcsContext, Presentation presentation) { + presentation.setText(vcsFileRevision.getRevisionNumber().asString() +" " + DATE_FORMAT.format(vcsFileRevision.getRevisionDate()) + " " + vcsFileRevision.getAuthor()); + super.update(vcsContext, presentation); + } + }); + } + + } + catch (VcsException e1) { + + } + return actions.toArray(new AnAction[actions.size()]); + } +} diff --git a/source/com/intellij/openapi/vcs/actions/CompareWithTheSameVersionAction.java b/source/com/intellij/openapi/vcs/actions/CompareWithTheSameVersionAction.java new file mode 100644 index 000000000000..84ff613cfb0d --- /dev/null +++ b/source/com/intellij/openapi/vcs/actions/CompareWithTheSameVersionAction.java @@ -0,0 +1,11 @@ +package com.intellij.openapi.vcs.actions; + +import com.intellij.openapi.vcs.history.VcsRevisionNumber; +import com.intellij.openapi.vcs.diff.DiffProvider; +import com.intellij.openapi.vfs.VirtualFile; + +public class CompareWithTheSameVersionAction extends AbstractShowDiffAction{ + protected VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file) { + return diffProvider.getCurrentRevision(file); + } +}