Search in sources :

Example 1 with VcsHistorySession

use of com.intellij.openapi.vcs.history.VcsHistorySession in project intellij-community by JetBrains.

the class HgHistoryTest method testCurrentAndPreviousRevisions.

/**
   * 1. Make two versions of a file (create, add, commit, modify, commit).
   * 2. Get the revisions history.
   * 3. Verify versions' contents and the current version.
   */
@Test
public void testCurrentAndPreviousRevisions() throws Exception {
    int versions = 0;
    fillFile(myProjectDir, new String[] { AFILE }, INITIAL_FILE_CONTENT);
    addAll();
    commitAll("initial content");
    versions++;
    fillFile(myProjectDir, new String[] { AFILE }, UPDATED_FILE_CONTENT);
    commitAll("updated content");
    versions++;
    final VcsHistorySession session = getHistorySession(AFILE);
    final List<VcsFileRevision> revisions = session.getRevisionList();
    for (VcsFileRevision rev : revisions) {
        rev.loadContent();
    }
    assertEquals(revisions.size(), versions);
    assertTrue(session.isCurrentRevision(revisions.get(0).getRevisionNumber()));
    assertEquals(revisions.get(0).getContent(), UPDATED_FILE_CONTENT.getBytes());
    assertEquals(revisions.get(1).getContent(), INITIAL_FILE_CONTENT.getBytes());
}
Also used : VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Test(org.testng.annotations.Test)

Example 2 with VcsHistorySession

use of com.intellij.openapi.vcs.history.VcsHistorySession in project intellij-community by JetBrains.

the class AnnotateStackTraceAction method actionPerformed.

@Override
public void actionPerformed(final AnActionEvent e) {
    myIsLoading = true;
    ProgressManager.getInstance().run(new Task.Backgroundable(myEditor.getProject(), "Getting File History", true) {

        private final Object LOCK = new Object();

        private final MergingUpdateQueue myUpdateQueue = new MergingUpdateQueue("AnnotateStackTraceAction", 200, true, null);

        private MyActiveAnnotationGutter myGutter;

        @Override
        public void onCancel() {
            myEditor.getGutter().closeAllAnnotations();
        }

        @Override
        public void onFinished() {
            myIsLoading = false;
            Disposer.dispose(myUpdateQueue);
        }

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            MultiMap<VirtualFile, Integer> files2lines = new MultiMap<>();
            Map<Integer, LastRevision> revisions = ContainerUtil.newHashMap();
            ApplicationManager.getApplication().runReadAction(() -> {
                for (int line = 0; line < myEditor.getDocument().getLineCount(); line++) {
                    indicator.checkCanceled();
                    VirtualFile file = getHyperlinkVirtualFile(myHyperlinks.findAllHyperlinksOnLine(line));
                    if (file == null)
                        continue;
                    files2lines.putValue(file, line);
                }
            });
            files2lines.entrySet().forEach(entry -> {
                indicator.checkCanceled();
                VirtualFile file = entry.getKey();
                Collection<Integer> lines = entry.getValue();
                LastRevision revision = getLastRevision(file);
                if (revision == null)
                    return;
                synchronized (LOCK) {
                    for (Integer line : lines) {
                        revisions.put(line, revision);
                    }
                }
                myUpdateQueue.queue(new Update("update") {

                    @Override
                    public void run() {
                        updateGutter(indicator, revisions);
                    }
                });
            });
            // myUpdateQueue can be disposed before the last revisions are passed to the gutter
            ApplicationManager.getApplication().invokeLater(() -> updateGutter(indicator, revisions));
        }

        @CalledInAwt
        private void updateGutter(@NotNull ProgressIndicator indicator, @NotNull Map<Integer, LastRevision> revisions) {
            if (indicator.isCanceled())
                return;
            if (myGutter == null) {
                myGutter = new MyActiveAnnotationGutter(getProject(), myHyperlinks, indicator);
                myEditor.getGutter().registerTextAnnotation(myGutter, myGutter);
            }
            Map<Integer, LastRevision> revisionsCopy;
            synchronized (LOCK) {
                revisionsCopy = ContainerUtil.newHashMap(revisions);
            }
            myGutter.updateData(revisionsCopy);
            ((EditorGutterComponentEx) myEditor.getGutter()).revalidateMarkup();
        }

        @Nullable
        private LastRevision getLastRevision(@NotNull VirtualFile file) {
            try {
                AbstractVcs vcs = VcsUtil.getVcsFor(myEditor.getProject(), file);
                if (vcs == null)
                    return null;
                VcsHistoryProvider historyProvider = vcs.getVcsHistoryProvider();
                if (historyProvider == null)
                    return null;
                FilePath filePath = VcsContextFactory.SERVICE.getInstance().createFilePathOn(file);
                if (historyProvider instanceof VcsHistoryProviderEx) {
                    VcsFileRevision revision = ((VcsHistoryProviderEx) historyProvider).getLastRevision(filePath);
                    if (revision == null)
                        return null;
                    return LastRevision.create(revision);
                } else {
                    VcsHistorySession session = historyProvider.createSessionFor(filePath);
                    if (session == null)
                        return null;
                    List<VcsFileRevision> list = session.getRevisionList();
                    if (list == null || list.isEmpty())
                        return null;
                    return LastRevision.create(list.get(0));
                }
            } catch (VcsException ignored) {
                LOG.warn(ignored);
                return null;
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) DateFormatUtil(com.intellij.util.text.DateFormatUtil) java.util(java.util) AllIcons(com.intellij.icons.AllIcons) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileHyperlinkInfo(com.intellij.execution.filters.FileHyperlinkInfo) EditorHyperlinkSupport(com.intellij.execution.impl.EditorHyperlinkSupport) EditorFontType(com.intellij.openapi.editor.colors.EditorFontType) ContainerUtil(com.intellij.util.containers.ContainerUtil) ActiveAnnotationGutter(com.intellij.openapi.vcs.actions.ActiveAnnotationGutter) ShowAllAffectedGenericAction(com.intellij.openapi.vcs.annotate.ShowAllAffectedGenericAction) VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) Task(com.intellij.openapi.progress.Task) ColorKey(com.intellij.openapi.editor.colors.ColorKey) VcsContextFactory(com.intellij.openapi.vcs.actions.VcsContextFactory) AnnotationSource(com.intellij.openapi.vcs.annotate.AnnotationSource) CalledWithReadLock(org.jetbrains.annotations.CalledWithReadLock) Disposer(com.intellij.openapi.util.Disposer) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) EditorGutterComponentEx(com.intellij.openapi.editor.ex.EditorGutterComponentEx) com.intellij.openapi.vcs(com.intellij.openapi.vcs) MultiMap(com.intellij.util.containers.MultiMap) CalledInAwt(org.jetbrains.annotations.CalledInAwt) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) ProgressManager(com.intellij.openapi.progress.ProgressManager) VcsUtil(com.intellij.vcsUtil.VcsUtil) StringUtil(com.intellij.openapi.util.text.StringUtil) AnAction(com.intellij.openapi.actionSystem.AnAction) VcsHistoryProvider(com.intellij.openapi.vcs.history.VcsHistoryProvider) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Editor(com.intellij.openapi.editor.Editor) VcsHistoryProviderEx(com.intellij.vcs.history.VcsHistoryProviderEx) MergingUpdateQueue(com.intellij.util.ui.update.MergingUpdateQueue) java.awt(java.awt) HyperlinkInfo(com.intellij.execution.filters.HyperlinkInfo) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) Update(com.intellij.util.ui.update.Update) List(java.util.List) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ApplicationManager(com.intellij.openapi.application.ApplicationManager) XmlStringUtil(com.intellij.xml.util.XmlStringUtil) NotNull(org.jetbrains.annotations.NotNull) Task(com.intellij.openapi.progress.Task) CalledInAwt(org.jetbrains.annotations.CalledInAwt) VcsHistoryProvider(com.intellij.openapi.vcs.history.VcsHistoryProvider) Update(com.intellij.util.ui.update.Update) MultiMap(com.intellij.util.containers.MultiMap) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsHistoryProviderEx(com.intellij.vcs.history.VcsHistoryProviderEx) VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) List(java.util.List) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) MergingUpdateQueue(com.intellij.util.ui.update.MergingUpdateQueue) MultiMap(com.intellij.util.containers.MultiMap) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with VcsHistorySession

use of com.intellij.openapi.vcs.history.VcsHistorySession in project intellij-community by JetBrains.

the class AnnotateRevisionAction method getFileRevision.

@Nullable
@Override
protected VcsFileRevision getFileRevision(@NotNull AnActionEvent e) {
    VcsHistorySession historySession = e.getData(VcsDataKeys.HISTORY_SESSION);
    if (historySession == null)
        return null;
    VcsFileRevision revision = e.getData(VcsDataKeys.VCS_FILE_REVISION);
    if (!historySession.isContentAvailable(revision))
        return null;
    return revision;
}
Also used : VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with VcsHistorySession

use of com.intellij.openapi.vcs.history.VcsHistorySession in project intellij-community by JetBrains.

the class SelectedBlockHistoryAction method actionPerformed.

public void actionPerformed(@NotNull final VcsContext context) {
    try {
        final Project project = context.getProject();
        assert project != null;
        final VcsSelection selection = VcsSelectionUtil.getSelection(context);
        assert selection != null;
        final VirtualFile file = FileDocumentManager.getInstance().getFile(selection.getDocument());
        assert file != null;
        final AbstractVcs activeVcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file);
        assert activeVcs != null;
        final VcsHistoryProvider provider = activeVcs.getVcsBlockHistoryProvider();
        assert provider != null;
        final int selectionStart = selection.getSelectionStartLineNumber();
        final int selectionEnd = selection.getSelectionEndLineNumber();
        new VcsHistoryProviderBackgroundableProxy(activeVcs, provider, activeVcs.getDiffProvider()).createSessionFor(activeVcs.getKeyInstanceMethod(), VcsUtil.getFilePath(file), new Consumer<VcsHistorySession>() {

            public void consume(VcsHistorySession session) {
                if (session == null)
                    return;
                final VcsSelectionHistoryDialog vcsHistoryDialog = new VcsSelectionHistoryDialog(project, file, selection.getDocument(), provider, session, activeVcs, Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd), selection.getDialogTitle());
                vcsHistoryDialog.show();
            }
        }, VcsBackgroundableActions.HISTORY_FOR_SELECTION, false, null);
    } catch (Exception exception) {
        reportError(exception);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) VcsSelection(com.intellij.vcsUtil.VcsSelection) VcsSelectionHistoryDialog(com.intellij.openapi.vcs.history.impl.VcsSelectionHistoryDialog) VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) VcsHistoryProvider(com.intellij.openapi.vcs.history.VcsHistoryProvider) VcsHistoryProviderBackgroundableProxy(com.intellij.openapi.vcs.history.VcsHistoryProviderBackgroundableProxy) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs)

Example 5 with VcsHistorySession

use of com.intellij.openapi.vcs.history.VcsHistorySession in project intellij-community by JetBrains.

the class SrcFileAnnotator method loadFromVersionControl.

@Nullable
private byte[] loadFromVersionControl(long date, VirtualFile f) {
    try {
        final AbstractVcs vcs = VcsUtil.getVcsFor(myProject, f);
        if (vcs == null)
            return null;
        final VcsHistoryProvider historyProvider = vcs.getVcsHistoryProvider();
        if (historyProvider == null)
            return null;
        final FilePath filePath = VcsContextFactory.SERVICE.getInstance().createFilePathOn(f);
        final VcsHistorySession session = historyProvider.createSessionFor(filePath);
        if (session == null)
            return null;
        final List<VcsFileRevision> list = session.getRevisionList();
        if (list != null) {
            for (VcsFileRevision revision : list) {
                final Date revisionDate = revision.getRevisionDate();
                if (revisionDate == null) {
                    return null;
                }
                if (revisionDate.getTime() < date) {
                    return revision.loadContent();
                }
            }
        }
    } catch (Exception e) {
        LOG.info(e);
        return null;
    }
    return null;
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) VcsHistoryProvider(com.intellij.openapi.vcs.history.VcsHistoryProvider) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VcsHistorySession (com.intellij.openapi.vcs.history.VcsHistorySession)8 VcsFileRevision (com.intellij.openapi.vcs.history.VcsFileRevision)7 VcsHistoryProvider (com.intellij.openapi.vcs.history.VcsHistoryProvider)3 Nullable (org.jetbrains.annotations.Nullable)3 Project (com.intellij.openapi.project.Project)2 AbstractVcs (com.intellij.openapi.vcs.AbstractVcs)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Test (org.testng.annotations.Test)2 FileHyperlinkInfo (com.intellij.execution.filters.FileHyperlinkInfo)1 HyperlinkInfo (com.intellij.execution.filters.HyperlinkInfo)1 EditorHyperlinkSupport (com.intellij.execution.impl.EditorHyperlinkSupport)1 AllIcons (com.intellij.icons.AllIcons)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 ColorKey (com.intellij.openapi.editor.colors.ColorKey)1 EditorFontType (com.intellij.openapi.editor.colors.EditorFontType)1 EditorGutterComponentEx (com.intellij.openapi.editor.ex.EditorGutterComponentEx)1