svn: Refactor "SvnVcs" - simplify "synchronize to native change lists" logic

GitOrigin-RevId: b51cfeddcab8e210c9a1298269650895f33841cf
This commit is contained in:
Konstantin Kolosovsky
2020-04-24 15:29:10 +00:00
committed by intellij-monorepo-bot
parent 714f0c099b
commit 5fa10bf8f5
5 changed files with 44 additions and 67 deletions
@@ -32,7 +32,7 @@ public abstract class ProjectLevelVcsManager {
/**
* Returns the instance for the specified project.
*/
public static ProjectLevelVcsManager getInstance(Project project) {
public static ProjectLevelVcsManager getInstance(@NotNull Project project) {
return project.getService(ProjectLevelVcsManager.class);
}
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.vcsUtil;
import com.intellij.ide.util.PropertiesComponent;
@@ -120,11 +120,11 @@ public class VcsUtil {
* File is considered to be a valid vcs file if it resides under the content
* root controlled by the given vcs.
*/
public static boolean isFileForVcs(@NotNull VirtualFile file, Project project, AbstractVcs host) {
public static boolean isFileForVcs(@NotNull VirtualFile file, @NotNull Project project, @Nullable AbstractVcs host) {
return getVcsFor(project, file) == host;
}
public static boolean isFileForVcs(FilePath path, Project project, AbstractVcs host) {
public static boolean isFileForVcs(@NotNull FilePath path, @NotNull Project project, @Nullable AbstractVcs host) {
return getVcsFor(project, path) == host;
}
@@ -41,6 +41,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.idea.svn.actions.CleanupWorker;
import org.jetbrains.idea.svn.actions.ExclusiveBackgroundVcsAction;
import org.jetbrains.idea.svn.actions.SvnMergeProvider;
import org.jetbrains.idea.svn.annotate.SvnAnnotationProvider;
import org.jetbrains.idea.svn.api.*;
@@ -73,9 +74,12 @@ import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static com.intellij.openapi.vcs.changes.ChangesUtil.getAfterPath;
import static com.intellij.openapi.vcs.changes.ChangesUtil.getBeforePath;
import static com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile;
import static com.intellij.util.containers.ContainerUtil.*;
import static com.intellij.vcsUtil.VcsUtil.getFilePath;
import static com.intellij.vcsUtil.VcsUtil.isFileForVcs;
import static java.util.Collections.emptyList;
import static java.util.function.Function.identity;
@@ -232,7 +236,7 @@ public final class SvnVcs extends AbstractVcs {
ChangeListManager.getInstance(myProject).setReadOnly(LocalChangeList.getDefaultName(), true);
if (!myConfiguration.changeListsSynchronized()) {
processChangeLists(lists);
ExclusiveBackgroundVcsAction.run(myProject, () -> synchronizeToNativeChangeLists(lists));
}
}
catch (ProcessCanceledException e) {
@@ -246,45 +250,26 @@ public final class SvnVcs extends AbstractVcs {
});
}
public void processChangeLists(final List<? extends LocalChangeList> lists) {
final ProjectLevelVcsManager plVcsManager = ProjectLevelVcsManager.getInstance(myProject);
plVcsManager.startBackgroundVcsOperation();
try {
for (LocalChangeList list : lists) {
if (!list.isDefault()) {
final Collection<Change> changes = list.getChanges();
for (Change change : changes) {
correctListForRevision(plVcsManager, change.getBeforeRevision(), list.getName());
correctListForRevision(plVcsManager, change.getAfterRevision(), list.getName());
}
}
}
}
finally {
final Application appManager = ApplicationManager.getApplication();
if (appManager.isDispatchThread()) {
appManager.executeOnPooledThread(() -> plVcsManager.stopBackgroundVcsOperation());
}
else {
plVcsManager.stopBackgroundVcsOperation();
public void synchronizeToNativeChangeLists(@NotNull List<? extends LocalChangeList> lists) {
for (LocalChangeList list : lists) {
if (list.isDefault()) continue;
for (Change change : list.getChanges()) {
setNativeChangeList(getBeforePath(change), list.getName());
setNativeChangeList(getAfterPath(change), list.getName());
}
}
}
private void correctListForRevision(@NotNull final ProjectLevelVcsManager plVcsManager,
@Nullable final ContentRevision revision,
@NotNull final String name) {
if (revision != null) {
final FilePath path = revision.getFile();
final AbstractVcs vcs = plVcsManager.getVcsFor(path);
if (vcs != null && VCS_NAME.equals(vcs.getName())) {
try {
getFactory(path.getIOFile()).createChangeListClient().add(name, path.getIOFile(), null);
}
catch (VcsException e) {
// left in default list
}
}
private void setNativeChangeList(@Nullable FilePath path, @NotNull String changeListName) {
if (path == null) return;
if (!isFileForVcs(path, myProject, this)) return;
try {
getFactory(path.getIOFile()).createChangeListClient().add(changeListName, path.getIOFile(), null);
}
catch (VcsException e) {
// left in default list
}
}
@@ -1,40 +1,28 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.idea.svn.actions;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import org.jetbrains.annotations.NotNull;
import static com.intellij.openapi.application.ApplicationManager.getApplication;
public class ExclusiveBackgroundVcsAction {
private ExclusiveBackgroundVcsAction() {
}
public static void run(final Project project, final Runnable action) {
final ProjectLevelVcsManager plVcsManager = ProjectLevelVcsManager.getInstance(project);
plVcsManager.startBackgroundVcsOperation();
public static void run(@NotNull Project project, @NotNull Runnable action) {
ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
vcsManager.startBackgroundVcsOperation();
try {
action.run();
} finally {
final Application application = ApplicationManager.getApplication();
if (application.isDispatchThread()) {
application.executeOnPooledThread(() -> plVcsManager.stopBackgroundVcsOperation());
} else {
plVcsManager.stopBackgroundVcsOperation();
}
finally {
if (getApplication().isDispatchThread()) {
getApplication().executeOnPooledThread(() -> vcsManager.stopBackgroundVcsOperation());
}
else {
vcsManager.stopBackgroundVcsOperation();
}
}
}
@@ -17,6 +17,7 @@ import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.SvnUtil;
import org.jetbrains.idea.svn.SvnVcs;
import org.jetbrains.idea.svn.WorkingCopyFormat;
import org.jetbrains.idea.svn.actions.ExclusiveBackgroundVcsAction;
import org.jetbrains.idea.svn.api.EventAction;
import org.jetbrains.idea.svn.api.ProgressEvent;
import org.jetbrains.idea.svn.api.ProgressTracker;
@@ -105,7 +106,10 @@ public class SvnFormatWorker extends Task.Backgroundable {
// to map to native
if (supportsChangelists) {
SvnVcs.getInstance(myProject).processChangeLists(myBeforeChangeLists);
ExclusiveBackgroundVcsAction.run(
myProject,
() -> SvnVcs.getInstance(myProject).synchronizeToNativeChangeLists(myBeforeChangeLists)
);
}
BackgroundTaskUtil.syncPublisher(SvnVcs.WC_CONVERTED).run();