From 1d23281fe7697e3536c2ec83284c5793d2e85b2b Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Mon, 18 Aug 2014 18:24:11 +0400 Subject: [PATCH] [git] IDEA-87092 handle local changes overwritten error on unstash Unstashing is currently a modal process => show a dialog instead of a notification. --- .../src/git4idea/ui/GitUnstashDialog.java | 7 +++ .../LocalChangesWouldBeOverwrittenHelper.java | 45 ++++++++++++------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java index eccaa446b0ed..44e4f1fab4b9 100644 --- a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java +++ b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java @@ -51,6 +51,7 @@ import git4idea.merge.GitConflictResolver; import git4idea.repo.GitRepository; import git4idea.stash.GitStashUtils; import git4idea.util.GitUIUtil; +import git4idea.util.LocalChangesWouldBeOverwrittenHelper; import git4idea.util.UntrackedFilesNotifier; import git4idea.validators.GitBranchNameValidator; import org.jetbrains.annotations.NotNull; @@ -68,6 +69,8 @@ import java.util.HashSet; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import static git4idea.commands.GitLocalChangesWouldBeOverwrittenDetector.Operation.MERGE; + /** * The unstash dialog */ @@ -349,7 +352,9 @@ public class GitUnstashDialog extends DialogWrapper { } }); GitUntrackedFilesOverwrittenByOperationDetector untrackedFilesDetector = new GitUntrackedFilesOverwrittenByOperationDetector(root); + GitLocalChangesWouldBeOverwrittenDetector localChangesDetector = new GitLocalChangesWouldBeOverwrittenDetector(root, MERGE); h.addLineListener(untrackedFilesDetector); + h.addLineListener(localChangesDetector); GitUtil.workingTreeChangeStarted(myProject); try { @@ -371,6 +376,8 @@ public class GitUnstashDialog extends DialogWrapper { } else if (untrackedFilesDetector.wasMessageDetected()) { UntrackedFilesNotifier.notifyUntrackedFilesOverwrittenBy(myProject, root, untrackedFilesDetector.getRelativeFilePaths(), "unstash", null); + } else if (localChangesDetector.wasMessageDetected()) { + LocalChangesWouldBeOverwrittenHelper.showErrorDialog(myProject, root, "unstash", localChangesDetector.getRelativeFilePaths()); } else if (!res.success()) { GitUIUtil.showOperationErrors(myProject, h.errors(), h.printableCommandLine()); } diff --git a/plugins/git4idea/src/git4idea/util/LocalChangesWouldBeOverwrittenHelper.java b/plugins/git4idea/src/git4idea/util/LocalChangesWouldBeOverwrittenHelper.java index 021ce824b9a7..7659c50e362a 100644 --- a/plugins/git4idea/src/git4idea/util/LocalChangesWouldBeOverwrittenHelper.java +++ b/plugins/git4idea/src/git4idea/util/LocalChangesWouldBeOverwrittenHelper.java @@ -35,12 +35,12 @@ import java.util.List; public class LocalChangesWouldBeOverwrittenHelper { @NotNull - public static String getErrorNotificationDescription() { + private static String getErrorNotificationDescription() { return getErrorDescription(true); } @NotNull - public static String getErrorDialogDescription() { + private static String getErrorDialogDescription() { return getErrorDescription(false); } @@ -56,7 +56,7 @@ public class LocalChangesWouldBeOverwrittenHelper { } } - public static void showErrorNotification(@NotNull final Project project, @NotNull VirtualFile root, @NotNull final String operationName, + public static void showErrorNotification(@NotNull final Project project, @NotNull final VirtualFile root, @NotNull final String operationName, @NotNull final Collection relativeFilePaths) { final Collection absolutePaths = GitUtil.toAbsolute(root, relativeFilePaths); final List changes = GitUtil.findLocalChangesForPaths(project, root, absolutePaths, false); @@ -66,20 +66,33 @@ public class LocalChangesWouldBeOverwrittenHelper { @Override protected void hyperlinkActivated(@NotNull Notification notification, @NotNull HyperlinkEvent e) { - String title = "Local Changes Prevent from " + StringUtil.capitalize(operationName); - String description = getErrorDialogDescription(); - if (changes.isEmpty()) { - GitUtil.showPathsInDialog(project, absolutePaths, title, description); - } - else { - DialogBuilder builder = new DialogBuilder(project); - builder.setNorthPanel(new MultiLineLabel(description)); - builder.setCenterPanel(new ChangesBrowserWithRollback(project, changes)); - builder.addOkAction(); - builder.setTitle(title); - builder.show(); - } + showErrorDialog(project, operationName, changes, absolutePaths); } }); } + + public static void showErrorDialog(@NotNull Project project, @NotNull VirtualFile root, @NotNull String operationName, + @NotNull Collection relativeFilePaths) { + Collection absolutePaths = GitUtil.toAbsolute(root, relativeFilePaths); + List changes = GitUtil.findLocalChangesForPaths(project, root, absolutePaths, false); + showErrorDialog(project, operationName, changes, absolutePaths); + } + + private static void showErrorDialog(@NotNull Project project, @NotNull String operationName, @NotNull List changes, + @NotNull Collection absolutePaths) { + String title = "Local Changes Prevent from " + StringUtil.capitalize(operationName); + String description = getErrorDialogDescription(); + if (changes.isEmpty()) { + GitUtil.showPathsInDialog(project, absolutePaths, title, description); + } + else { + DialogBuilder builder = new DialogBuilder(project); + builder.setNorthPanel(new MultiLineLabel(description)); + builder.setCenterPanel(new ChangesBrowserWithRollback(project, changes)); + builder.addOkAction(); + builder.setTitle(title); + builder.show(); + } + } + }