Skip to content

Instantly share code, notes, and snippets.

@AMPivovarov
Last active July 19, 2022 05:27
Show Gist options
  • Save AMPivovarov/f7c9a77ea3543b5ca1c0 to your computer and use it in GitHub Desktop.
Save AMPivovarov/f7c9a77ea3543b5ca1c0 to your computer and use it in GitHub Desktop.
IDE Script #2.md
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.Function
import com.intellij.openapi.progress.Task
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.vcs.VcsNotifier
def action = new DumbAwareAction() {
{
getTemplatePresentation().setText("Git Reset HEAD~1 --hard");
}
public void update(AnActionEvent e) {
def project = e.getData(CommonDataKeys.PROJECT)
def file = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (project == null || file == null) {
e.getPresentation().setEnabled(false);
return
}
def repo = git4idea.GitUtil.getRepositoryManager(project).getRepositoryForFile(file);
e.getPresentation().setEnabled(repo != null);
}
public void actionPerformed(AnActionEvent e) {
Project project = e.getData(CommonDataKeys.PROJECT);
def file = e.getData(CommonDataKeys.VIRTUAL_FILE);
def repo = git4idea.GitUtil.getRepositoryManager(project).getRepositoryForFile(file);
new Task.Backgroundable(project, "Git reset", true) {
public void run(ProgressIndicator indicator) {
def commit = git4idea.history.GitHistoryUtils.history(project, repo.getRoot(), "HEAD~1", "-n 1");
if (commit.size() != 1) {
def message = StringUtil.join(commit, new Function<git4idea.GitCommit, String>() {
public String fun(git4idea.GitCommit each) {
return each.getId().toString()
}
}, ",")
VcsNotifier.getInstance(project).notifyError("Can't get last commit", message);
return
}
def map = Collections.singletonMap(repo, commit.get(0));
new git4idea.reset.GitResetOperation(project, map, git4idea.reset.GitResetMode.HARD, indicator).execute();
}
public void onSuccess() {
VcsNotifier.getInstance(project).notifySuccess("Success");
}
}.queue();
}
}
ActionManager.getInstance().unregisterAction("IDEScriptAction#2");
ActionManager.getInstance().registerAction("IDEScriptAction#2", action);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment