From 50d7b18b3c48cbb343dda263da6b9ddd5f5e47e9 Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Sun, 10 May 2020 03:06:31 +0300 Subject: [PATCH] [git-index] add revert action to revert unstaged changes GitOrigin-RevId: ee037cde6013a23bc95d318f3fb0cb70ad4f3dcb --- .../git4idea/resources/META-INF/stage-plugin.xml | 2 ++ .../resources/messages/GitBundle.properties | 3 +++ .../index/actions/GitFileStatusNodeAction.kt | 14 ++++++++++++++ .../git4idea/src/git4idea/util/GitFileUtils.java | 9 +++++++++ 4 files changed, 28 insertions(+) diff --git a/plugins/git4idea/resources/META-INF/stage-plugin.xml b/plugins/git4idea/resources/META-INF/stage-plugin.xml index c2aab8fa8bc3..e78d407cdab2 100644 --- a/plugins/git4idea/resources/META-INF/stage-plugin.xml +++ b/plugins/git4idea/resources/META-INF/stage-plugin.xml @@ -18,6 +18,7 @@ use-shortcut-of="Refresh"/> + @@ -26,6 +27,7 @@ + diff --git a/plugins/git4idea/resources/messages/GitBundle.properties b/plugins/git4idea/resources/messages/GitBundle.properties index 9e9c7f0c6be9..552609fbe675 100644 --- a/plugins/git4idea/resources/messages/GitBundle.properties +++ b/plugins/git4idea/resources/messages/GitBundle.properties @@ -570,6 +570,9 @@ stage.loading.status=Loading changes... stage.reset.action.text=Reset stage.reset.process=Resetting Files... stage.reset.error.title=Error Resetting Files +stage.revert.action.text=Revert +stage.revert.process=Reverting Back Files... +stage.revert.error.title=Error Reverting Files branches.selected.branches.updated.title=Selected {0,choice,1#branch|2#branches} Updated branches.update.failed=Update Failed diff --git a/plugins/git4idea/src/git4idea/index/actions/GitFileStatusNodeAction.kt b/plugins/git4idea/src/git4idea/index/actions/GitFileStatusNodeAction.kt index 4db70047aef8..5419687a67e9 100644 --- a/plugins/git4idea/src/git4idea/index/actions/GitFileStatusNodeAction.kt +++ b/plugins/git4idea/src/git4idea/index/actions/GitFileStatusNodeAction.kt @@ -93,6 +93,20 @@ class GitResetAction : GitFileStatusNodeAction(GitBundle.messagePointer("stage.r } } +class GitRevertAction : GitFileStatusNodeAction(GitBundle.messagePointer("stage.revert.action.text")) { + override fun matches(statusNode: GitFileStatusNode) = statusNode.kind == NodeKind.UNSTAGED + + override fun processPaths(project: Project, root: VirtualFile, paths: List) { + GitFileUtils.revertUnstagedPaths(project, root, paths) + } + + override fun progressTitle() = GitBundle.message("stage.revert.process") + + override fun showErrorMessage(project: Project, exceptions: Collection) { + showErrorMessage(project, GitBundle.message("stage.revert.error.title"), exceptions) + } +} + fun runProcess(project: Project, title: @NlsContexts.ProgressTitle String, canBeCancelled: Boolean, process: () -> T): T { return ProgressManager.getInstance().runProcessWithProgressSynchronously(ThrowableComputable { process() }, title, canBeCancelled, project) diff --git a/plugins/git4idea/src/git4idea/util/GitFileUtils.java b/plugins/git4idea/src/git4idea/util/GitFileUtils.java index 1f5952bcc651..59d1d34bd3dd 100644 --- a/plugins/git4idea/src/git4idea/util/GitFileUtils.java +++ b/plugins/git4idea/src/git4idea/util/GitFileUtils.java @@ -228,6 +228,15 @@ public class GitFileUtils { } } + public static void revertUnstagedPaths(@NotNull Project project, @NotNull VirtualFile root, @NotNull List files) throws VcsException { + for (List paths : VcsFileUtil.chunkPaths(root, files)) { + GitLineHandler handler = new GitLineHandler(project, root, GitCommand.CHECKOUT); + handler.endOptions(); + handler.addParameters(paths); + Git.getInstance().runCommand(handler).throwOnError(); + } + } + /** * Get file content for the specific revision *