Search in sources :

Example 26 with VcsFileRevision

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

the class GitFileAnnotation method getPreviousFileRevisionProvider.

@Nullable
@Override
public PreviousFileRevisionProvider getPreviousFileRevisionProvider() {
    return new PreviousFileRevisionProvider() {

        @Nullable
        @Override
        public VcsFileRevision getPreviousRevision(int lineNumber) {
            LineInfo lineInfo = getLineInfo(lineNumber);
            if (lineInfo == null)
                return null;
            VcsFileRevision previousFileRevision = lineInfo.getPreviousFileRevision();
            if (previousFileRevision != null)
                return previousFileRevision;
            GitRevisionNumber revisionNumber = lineInfo.getRevisionNumber();
            if (myRevisions != null && myRevisionMap != null && myRevisionMap.contains(revisionNumber)) {
                int index = myRevisionMap.get(revisionNumber);
                if (index + 1 < myRevisions.size()) {
                    return myRevisions.get(index + 1);
                }
            }
            return null;
        }

        @Nullable
        @Override
        public VcsFileRevision getLastRevision() {
            if (myBaseRevision instanceof GitRevisionNumber) {
                return new GitFileRevision(myProject, VcsUtil.getFilePath(myFile), (GitRevisionNumber) myBaseRevision);
            } else {
                return ContainerUtil.getFirstItem(getRevisions());
            }
        }
    };
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitFileRevision(git4idea.GitFileRevision) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with VcsFileRevision

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

the class GitDiffProvider method createFileContent.

/**
   * {@inheritDoc}
   */
@Nullable
public ContentRevision createFileContent(VcsRevisionNumber revisionNumber, VirtualFile selectedFile) {
    if (selectedFile.isDirectory()) {
        return null;
    }
    final String path = selectedFile.getPath();
    if (GitUtil.gitRootOrNull(selectedFile) == null) {
        return null;
    }
    // faster, if there were no renames
    FilePath filePath = VcsUtil.getFilePath(path);
    try {
        final CommittedChangesProvider committedChangesProvider = GitVcs.getInstance(myProject).getCommittedChangesProvider();
        final Pair<CommittedChangeList, FilePath> pair = committedChangesProvider.getOneList(selectedFile, revisionNumber);
        if (pair != null) {
            return GitContentRevision.createRevision(pair.getSecond(), revisionNumber, myProject, selectedFile.getCharset());
        }
    } catch (VcsException e) {
        GitVcs.getInstance(myProject).showErrors(Collections.singletonList(e), GitBundle.message("diff.find.error", path));
    }
    try {
        for (VcsFileRevision f : GitHistoryUtils.history(myProject, filePath)) {
            GitFileRevision gitRevision = (GitFileRevision) f;
            if (f.getRevisionNumber().equals(revisionNumber)) {
                return GitContentRevision.createRevision(gitRevision.getPath(), revisionNumber, myProject, selectedFile.getCharset());
            }
        }
        GitContentRevision candidate = (GitContentRevision) GitContentRevision.createRevision(filePath, revisionNumber, myProject, selectedFile.getCharset());
        try {
            candidate.getContent();
            return candidate;
        } catch (VcsException e) {
        // file does not exists
        }
    } catch (VcsException e) {
        GitVcs.getInstance(myProject).showErrors(Collections.singletonList(e), GitBundle.message("diff.find.error", path));
    }
    return null;
}
Also used : GitFileRevision(git4idea.GitFileRevision) GitContentRevision(git4idea.GitContentRevision) CommittedChangeList(com.intellij.openapi.vcs.versionBrowser.CommittedChangeList) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with VcsFileRevision

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

the class GithubOpenInBrowserAction method getDataFromHistory.

@Nullable
private static CommitData getDataFromHistory(AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);
    FilePath filePath = e.getData(VcsDataKeys.FILE_PATH);
    VcsFileRevision fileRevision = e.getData(VcsDataKeys.VCS_FILE_REVISION);
    if (project == null || filePath == null || fileRevision == null)
        return null;
    if (!(fileRevision instanceof GitFileRevision))
        return null;
    GitRepository repository = GitUtil.getRepositoryManager(project).getRepositoryForFile(filePath);
    if (repository == null || !GithubUtil.isRepositoryOnGitHub(repository))
        return null;
    return new CommitData(project, repository, fileRevision.getRevisionNumber().asString());
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) Project(com.intellij.openapi.project.Project) GitRepository(git4idea.repo.GitRepository) GitFileRevision(git4idea.GitFileRevision) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with VcsFileRevision

use of com.intellij.openapi.vcs.history.VcsFileRevision 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 30 with VcsFileRevision

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

the class AnnotateVcsVirtualFileAction method extractData.

@Nullable
private static AnnotationData extractData(@NotNull Project project, @NotNull VirtualFile file) {
    FilePath filePath = null;
    VcsRevisionNumber revisionNumber = null;
    if (file instanceof VcsVirtualFile) {
        filePath = VcsUtil.getFilePath(file.getPath());
        VcsFileRevision revision = ((VcsVirtualFile) file).getFileRevision();
        revisionNumber = revision != null ? revision.getRevisionNumber() : null;
    } else if (file instanceof ContentRevisionVirtualFile) {
        ContentRevision revision = ((ContentRevisionVirtualFile) file).getContentRevision();
        filePath = revision.getFile();
        revisionNumber = revision.getRevisionNumber();
    }
    if (filePath == null || revisionNumber == null)
        return null;
    AbstractVcs vcs = VcsUtil.getVcsFor(project, filePath);
    return vcs != null ? new AnnotationData(vcs, filePath, revisionNumber) : null;
}
Also used : VcsVirtualFile(com.intellij.openapi.vcs.vfs.VcsVirtualFile) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) ContentRevisionVirtualFile(com.intellij.openapi.vcs.vfs.ContentRevisionVirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VcsFileRevision (com.intellij.openapi.vcs.history.VcsFileRevision)44 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Nullable (org.jetbrains.annotations.Nullable)13 FilePath (com.intellij.openapi.vcs.FilePath)12 Project (com.intellij.openapi.project.Project)10 VcsHistoryProvider (com.intellij.openapi.vcs.history.VcsHistoryProvider)8 VcsHistorySession (com.intellij.openapi.vcs.history.VcsHistorySession)7 VcsRevisionNumber (com.intellij.openapi.vcs.history.VcsRevisionNumber)7 VcsKey (com.intellij.openapi.vcs.VcsKey)5 VcsAbstractHistorySession (com.intellij.openapi.vcs.history.VcsAbstractHistorySession)5 VcsAppendableHistorySessionPartner (com.intellij.openapi.vcs.history.VcsAppendableHistorySessionPartner)5 Semaphore (com.intellij.util.concurrency.Semaphore)5 GitFileRevision (git4idea.GitFileRevision)5 Test (org.junit.Test)5 VcsVirtualFile (com.intellij.openapi.vcs.vfs.VcsVirtualFile)4 NotNull (org.jetbrains.annotations.NotNull)4 Annotation (com.intellij.cvsSupport2.cvsoperations.cvsAnnotate.Annotation)3 AbstractVcs (com.intellij.openapi.vcs.AbstractVcs)3 FileAnnotation (com.intellij.openapi.vcs.annotate.FileAnnotation)3 HashMap (com.intellij.util.containers.HashMap)3