From 2d899e3ab05a2a6c4f9947a832fad91512d50e0d Mon Sep 17 00:00:00 2001 From: Alexandr Evstigneev Date: Fri, 10 Feb 2017 15:24:50 +0300 Subject: [PATCH 1/2] implemented VcsFileUtil.addFilesToVcsWithConfirmation method IDEA-CR-18124 --- .../src/messages/VcsBundle.properties | 2 + .../src/com/intellij/vcsUtil/VcsFileUtil.java | 92 ++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/platform/platform-resources-en/src/messages/VcsBundle.properties b/platform/platform-resources-en/src/messages/VcsBundle.properties index eeef9e62167d..24f92ac5d9fa 100644 --- a/platform/platform-resources-en/src/messages/VcsBundle.properties +++ b/platform/platform-resources-en/src/messages/VcsBundle.properties @@ -543,6 +543,8 @@ dialog.enable.version.control.integration.select.vcs.label.text=Select a version dialog.enable.version.control.integration.hint.text=Version control settings can be configured in 'Settings | Version Control' confirmation.text.add.file=Do you want to schedule the following file for addition to VCS?\n{0} confirmation.title.add.file=Schedule for Addition +confirmation.title.add.file.to=Add file to {0}? +confirmation.title.add.files.to=Add files to {0}? confirmation.title.add.multiple.files=Select Files to Add to VCS waiting.changelists.update.for.show.commit.dialog.message=Commit dialog add.confirmation.label.text=When files are created with {0}: diff --git a/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java b/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java index 98a3d84eb6c2..d8848fbb51a8 100644 --- a/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java +++ b/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java @@ -15,15 +15,18 @@ */ package com.intellij.vcsUtil; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.ThrowableNotNullFunction; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vcs.FilePath; -import com.intellij.openapi.vcs.VcsException; +import com.intellij.openapi.vcs.*; import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager; +import com.intellij.openapi.vcs.checkin.CheckinEnvironment; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ThrowableConsumer; @@ -33,6 +36,7 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -379,4 +383,88 @@ public class VcsFileUtil { return null; } + + public static void addFilesToVcsWithConfirmation(@NotNull Project project, VirtualFile... virtualFiles) { + addFilesToVcsWithConfirmation(project, Arrays.asList(virtualFiles)); + } + + public static void addFilesToVcsWithConfirmation(@NotNull Project project, @NotNull Collection virtualFiles) { + addFilesToVcsWithConfirmation(project, virtualFiles, null); + } + + /** + * Finds all VCSs related to the passed files, suggests user to add files to the respected VCSs honoring addition and silence settings + * and adds them if user or settings confirmed addition + * + * @param project project we work in + * @param virtualFiles collection of virtual files to add; directories being added recursively + * @param exceptionsProcessor optional exceptions processor + */ + public static void addFilesToVcsWithConfirmation(@NotNull Project project, + @NotNull Collection virtualFiles, + @Nullable VcsExceptionsProcessor exceptionsProcessor) { + if (virtualFiles.isEmpty()) { + return; + } + ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project); + Multimap vcsMap = ArrayListMultimap.create(); + for (VirtualFile createdFile : virtualFiles) { + AbstractVcs vcs = vcsManager.getVcsFor(createdFile); + if (vcs == null) { + continue; + } + VfsUtil.processFileRecursivelyWithoutIgnored(createdFile, (virtualFile) -> vcsMap.put(vcs, virtualFile)); + } + + for (AbstractVcs vcs : vcsMap.keySet()) { + VcsShowConfirmationOption addOption = + vcsManager.getStandardConfirmation(VcsConfiguration.StandardConfirmation.ADD, vcs); + if (addOption.getValue() == VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY) return; + List filesList = new ArrayList<>(vcsMap.get(vcs)); + if (addOption.getValue() == VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY) { + performAdding(vcs, filesList, exceptionsProcessor); + } + else { + AbstractVcsHelper helper = AbstractVcsHelper.getInstance(project); + + ApplicationManager.getApplication().invokeLater(() -> { + Collection filesToAdd = + helper + .selectFilesToProcess( + new ArrayList<>(filesList), + VcsBundle.message("confirmation.title.add.files.to", vcs.getDisplayName()), + null, + VcsBundle.message("confirmation.title.add.file.to", vcs.getDisplayName()), + null, + addOption); + + if (filesToAdd != null) { + ApplicationManager.getApplication().executeOnPooledThread(() -> { + performAdding(vcs, new ArrayList<>(filesToAdd), exceptionsProcessor); + }); + } + }); + } + } + } + + private static void performAdding(@NotNull AbstractVcs vcs, + @NotNull List value, + @Nullable VcsExceptionsProcessor exceptionsProcessor) { + CheckinEnvironment checkinEnvironment = vcs.getCheckinEnvironment(); + if (checkinEnvironment == null) { + return; + } + List exceptions = checkinEnvironment.scheduleUnversionedFilesForAddition(value); + if (exceptions == null || exceptionsProcessor == null) { + return; + } + for (VcsException exception : exceptions) { + exceptionsProcessor.process(vcs, exception); + } + } + + public interface VcsExceptionsProcessor { + void process(@NotNull AbstractVcs vcs, @NotNull VcsException exception); + } } From 6412fe2de02ca164edf544eb15c89d6b0547f006 Mon Sep 17 00:00:00 2001 From: Alexandr Evstigneev Date: Mon, 13 Feb 2017 12:37:39 +0300 Subject: [PATCH 2/2] adjusted addFilesToVcsWithConfirmation according to IDEA-CR-18124 - Method is now synchronous - Got rid of redundant (for now) exceptionsProcessor - Threading is now more straightforward (recommended to execute on pooled thread) --- .../src/com/intellij/vcsUtil/VcsFileUtil.java | 69 +++++++------------ 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java b/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java index d8848fbb51a8..98461e6818ca 100644 --- a/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java +++ b/platform/vcs-impl/src/com/intellij/vcsUtil/VcsFileUtil.java @@ -21,6 +21,7 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.ThrowableNotNullFunction; import com.intellij.openapi.util.io.FileUtil; @@ -388,21 +389,16 @@ public class VcsFileUtil { addFilesToVcsWithConfirmation(project, Arrays.asList(virtualFiles)); } - public static void addFilesToVcsWithConfirmation(@NotNull Project project, @NotNull Collection virtualFiles) { - addFilesToVcsWithConfirmation(project, virtualFiles, null); - } - /** * Finds all VCSs related to the passed files, suggests user to add files to the respected VCSs honoring addition and silence settings - * and adds them if user or settings confirmed addition + * and adds them if user or settings confirmed addition. Because of potentially long operation of collecting files it's highly recommended + * to invoke it on the pooled thread. Method works synchronously. * - * @param project project we work in - * @param virtualFiles collection of virtual files to add; directories being added recursively - * @param exceptionsProcessor optional exceptions processor + * @param project project we work in + * @param virtualFiles collection of virtual files to add; directories being added recursively */ public static void addFilesToVcsWithConfirmation(@NotNull Project project, - @NotNull Collection virtualFiles, - @Nullable VcsExceptionsProcessor exceptionsProcessor) { + @NotNull Collection virtualFiles) { if (virtualFiles.isEmpty()) { return; } @@ -422,49 +418,36 @@ public class VcsFileUtil { if (addOption.getValue() == VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY) return; List filesList = new ArrayList<>(vcsMap.get(vcs)); if (addOption.getValue() == VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY) { - performAdding(vcs, filesList, exceptionsProcessor); + performAdditions(vcs, filesList); } else { AbstractVcsHelper helper = AbstractVcsHelper.getInstance(project); - ApplicationManager.getApplication().invokeLater(() -> { - Collection filesToAdd = - helper - .selectFilesToProcess( - new ArrayList<>(filesList), - VcsBundle.message("confirmation.title.add.files.to", vcs.getDisplayName()), - null, - VcsBundle.message("confirmation.title.add.file.to", vcs.getDisplayName()), - null, - addOption); + Ref> filesToAdd = Ref.create(); - if (filesToAdd != null) { - ApplicationManager.getApplication().executeOnPooledThread(() -> { - performAdding(vcs, new ArrayList<>(filesToAdd), exceptionsProcessor); - }); - } - }); + ApplicationManager.getApplication().invokeAndWait(() -> filesToAdd.set( + helper + .selectFilesToProcess( + new ArrayList<>(filesList), + VcsBundle.message("confirmation.title.add.files.to", vcs.getDisplayName()), + null, + VcsBundle.message("confirmation.title.add.file.to", vcs.getDisplayName()), + null, + addOption)) + ); + + if (!filesToAdd.isNull()) { + performAdditions(vcs, new ArrayList<>(filesToAdd.get())); + } } } } - private static void performAdding(@NotNull AbstractVcs vcs, - @NotNull List value, - @Nullable VcsExceptionsProcessor exceptionsProcessor) { + private static void performAdditions(@NotNull AbstractVcs vcs, + @NotNull List value) { CheckinEnvironment checkinEnvironment = vcs.getCheckinEnvironment(); - if (checkinEnvironment == null) { - return; + if (checkinEnvironment != null) { + checkinEnvironment.scheduleUnversionedFilesForAddition(value); } - List exceptions = checkinEnvironment.scheduleUnversionedFilesForAddition(value); - if (exceptions == null || exceptionsProcessor == null) { - return; - } - for (VcsException exception : exceptions) { - exceptionsProcessor.process(vcs, exception); - } - } - - public interface VcsExceptionsProcessor { - void process(@NotNull AbstractVcs vcs, @NotNull VcsException exception); } }