Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active January 7, 2018 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/5984577 to your computer and use it in GitHub Desktop.
Save dkandalov/5984577 to your computer and use it in GitHub Desktop.
Mini-plugin to get files from last N commits. (This code is for https://github.com/dkandalov/live-plugin)
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vcs.FilePathImpl
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.VcsRoot
import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList
import com.intellij.openapi.vfs.VirtualFile
import static liveplugin.PluginUtil.*
if (isIdeStartup) return
static List<VcsRoot> vcsRootsIn(Project project) {
ProjectRootManager.getInstance(project).contentSourceRoots
.collect{ ProjectLevelVcsManager.getInstance(project).getVcsRootObjectFor(it) }
.findAll{ it.path != null }.unique()
}
static List<CommittedChangeList> lastCommitsIn(VcsRoot vcsRoot, int numberOfCommitsToLoad = 10) {
def changesProvider = vcsRoot.vcs.committedChangesProvider
def location = changesProvider.getLocationFor(FilePathImpl.create(vcsRoot.path))
def settings = changesProvider.createDefaultSettings()
changesProvider.getCommittedChanges(settings, location, numberOfCommitsToLoad)
}
static Collection<VirtualFile> virtualFilesIn(Collection<CommittedChangeList> commits) {
commits.collectMany{it.changes.collect{it.virtualFile}}
}
def vcsRoot = vcsRootsIn(project).first() // simplification asuuming that normally there is one vcs root in project
show(virtualFilesIn(lastCommitsIn(vcsRoot)).join("\n"))
@delbao
Copy link

delbao commented Jan 7, 2018

I got this exception

java.util.NoSuchElementException: Cannot access first() element from an empty List
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.first(DefaultGroovyMethods.java:8932)
	at org.codehaus.groovy.runtime.dgm$222.invoke(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
	at plugin.run(plugin.groovy:31)
	at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:603)
	at liveplugin.pluginrunner.GroovyPluginRunner$runGroovyScript$1.invoke(GroovyPluginRunner.kt:58)
	at liveplugin.pluginrunner.GroovyPluginRunner$runGroovyScript$1.invoke(GroovyPluginRunner.kt:15)
	at liveplugin.pluginrunner.RunPluginActionKt$sam$Runnable$523b18a0.run(RunPluginAction.kt)
	at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:314)
	at com.intellij.openapi.application.impl.LaterInvocator$1.run(LaterInvocator.java:163)
	at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.a(LaterInvocator.java:416)
	at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:399)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
	at java.awt.EventQueue.access$500(EventQueue.java:98)
	at java.awt.EventQueue$3.run(EventQueue.java:715)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
	at com.intellij.ide.IdeEventQueue.d(IdeEventQueue.java:821)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:649)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:365)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment