Access Repository Informations for PsjFiles

已回答

Hello,

I want to access the Annotations from a GIt or SVN to a PsiFiles? Is this possible and how can I do this?

regards

Chris

0

What do you want to do with these annotations? 

For example, you can load them like this (though, it's better not to access vcs from EDT or while holding readLock - so file content might be already changed)
You can use `UpToDateLineNumberProviderImpl` to convert "local line numbers" to "annotated line numbers".

 val virtualFile = psiFile.virtualFile
val vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(virtualFile)
val annotationsProvider = vcs?.annotationProvider
if (annotationsProvider != null) {
val fileAnnotations = annotationsProvider.annotate(virtualFile)
try {
val affectedInRevision = fileAnnotations.getLineRevisionNumber(12)
doStuff(fileAnnotations)
}
finally {
fileAnnotations.dispose()
}
}
0

Hello, I want to know the author of the File by counting the Lines by Author.

0

This part of platform is not really extendable, but the code snippet below should work.
(Keep in mind, that `annotationsProvider.annotate` is a potentially slow operation, up to 30s for big/old files and Win machines.)

val virtualFile = psiFile.virtualFile
val vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(virtualFile) ?: throw Exception("File is not under VCS")
val annotationsProvider = vcs.annotationProvider ?: throw Exception("VCS does not support annotations")
val fileAnnotations = annotationsProvider.annotate(virtualFile)
try {
val authors = HashMultiset.create<String>()
val authorsMapping = fileAnnotations.authorsMappingProvider?.authors ?: throw Exception("VCS does not support extended annotations")
for (i in 0 until fileAnnotations.lineCount) {
val revision = fileAnnotations.getLineRevisionNumber(i)
val author = authorsMapping[revision]
if (author != null) {
authors.add(author)
}
}
return authors
}
finally {
fileAnnotations.dispose()
}

 

0

请先登录再写评论。