Skip to content

Instantly share code, notes, and snippets.

@coodix
Created December 13, 2019 08:18
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 coodix/d8b3b5d999fdd1b5bb0ebb85f00e188b to your computer and use it in GitHub Desktop.
Save coodix/d8b3b5d999fdd1b5bb0ebb85f00e188b to your computer and use it in GitHub Desktop.
Ide script for showing git diff dialog between any two commits
import com.intellij.vcsUtil.VcsUtil
import git4idea.GitUtil
import git4idea.GitContentRevision
import git4idea.GitRevisionNumber
import com.intellij.openapi.vcs.history.VcsDiffUtil
import git4idea.changes.GitChangeUtils
import git4idea.history.GitHistoryUtils
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.ide.DataManager
import com.intellij.dvcs.DvcsUtil
import com.intellij.openapi.vcs.VcsException
import java.util.Collections
import com.intellij.openapi.vfs.LocalFileSystem
import java.io.File
import com.intellij.openapi.project.ProjectManager
val branchToCompare = "HEAD~20"
val head = "HEAD"
val dataContext = DataManager.getInstance().dataContextFromFocusAsync.blockingGet(1000)!!
// val project = CommonDataKeys.PROJECT.getData(dataContext)!!
val project = ProjectManager.getInstance().getOpenProjects()[0]
// val selectedFile = LocalFileSystem.getInstance().findFileByIoFile(File("/Users/iisakov/Work/intellij-idea-badoo-components/"))
val selectedFile = project.getBaseDir()
// val selectedFile = DvcsUtil.getSelectedFile(project)
var filePath = VcsUtil.getFilePath(selectedFile)
if (selectedFile != null) {
var gitRepository = GitUtil.getRepositoryManager(project).getRepositoryForFile(selectedFile);
if (gitRepository == null) {
throw VcsException("Couldn't find Git Repository for " + selectedFile.getName())
}
var gitRepositoryRoot = gitRepository.getRoot();
var compareRevisionNumber = GitRevisionNumber(branchToCompare);
var changes = GitChangeUtils.getDiffWithWorkingDir(project, gitRepositoryRoot, branchToCompare, Collections.singletonList(filePath), false);
// if (changes.isEmpty() && GitHistoryUtils.getCurrentRevision(project, filePath, branchToCompare) == null) {
// throw VcsException("fileDoesntExistInBranchError")
// }
// if (changes.isEmpty() && !filePath.isDirectory()) {
// changes = createChangesWithCurrentContentForFile(filePath, GitContentRevision.createRevision(filePath, compareRevisionNumber, project))
// }
if (changes != null) {
VcsDiffUtil.showDiffFor(project, changes, VcsDiffUtil.getRevisionTitle(branchToCompare, false), VcsDiffUtil.getRevisionTitle(head, true), VcsUtil.getFilePath(selectedFile));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment