From 86df77cf40f5361da0b10be78ef938ef6bc4854a Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 11 Dec 2018 15:47:56 +0300 Subject: [PATCH] git: optimize checking if a directory is a Git or Hg root Checking the File is expensive. If the file is already inside the VFS, checking it via the VFS is more efficient. IDEA-201588 --- .../src/com/intellij/openapi/vcs/VcsRootChecker.java | 7 +++++++ .../intellij/openapi/vcs/roots/VcsRootDetectorImpl.java | 3 +-- plugins/git4idea/src/git4idea/roots/GitRootChecker.java | 5 +++++ .../hg4idea/src/org/zmlx/hg4idea/roots/HgRootChecker.java | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/VcsRootChecker.java b/platform/vcs-api/src/com/intellij/openapi/vcs/VcsRootChecker.java index 930edfd06f00..23a965b2549b 100644 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/VcsRootChecker.java +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/VcsRootChecker.java @@ -36,6 +36,13 @@ public abstract class VcsRootChecker { return false; } + /** + * Checks if the given directory is a root of the supported VCS. + */ + public boolean isRoot(@NotNull VirtualFile maybeRoot) { + return isRoot(maybeRoot.getPath()); + } + /** * Returns the VCS supported by this checker. */ diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/roots/VcsRootDetectorImpl.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/roots/VcsRootDetectorImpl.java index fc4b1a6ec7fe..6b83d7a6146d 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/roots/VcsRootDetectorImpl.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/roots/VcsRootDetectorImpl.java @@ -143,9 +143,8 @@ public class VcsRootDetectorImpl implements VcsRootDetector { @Nullable private AbstractVcs getVcsFor(@NotNull VirtualFile maybeRoot, @Nullable VirtualFile dirToCheckForIgnore) { - String path = maybeRoot.getPath(); for (VcsRootChecker checker : myCheckers) { - if (checker.isRoot(path) && (dirToCheckForIgnore == null || !checker.isIgnored(maybeRoot, dirToCheckForIgnore))) { + if (checker.isRoot(maybeRoot) && (dirToCheckForIgnore == null || !checker.isIgnored(maybeRoot, dirToCheckForIgnore))) { return myVcsManager.findVcsByName(checker.getSupportedVcs().getName()); } } diff --git a/plugins/git4idea/src/git4idea/roots/GitRootChecker.java b/plugins/git4idea/src/git4idea/roots/GitRootChecker.java index 5d40640511d1..8f91dac7c68d 100644 --- a/plugins/git4idea/src/git4idea/roots/GitRootChecker.java +++ b/plugins/git4idea/src/git4idea/roots/GitRootChecker.java @@ -39,6 +39,11 @@ public class GitRootChecker extends VcsRootChecker { return GitUtil.isGitRoot(path); } + @Override + public boolean isRoot(@NotNull VirtualFile maybeRoot) { + return GitUtil.findGitDir(maybeRoot) != null; + } + @Override @NotNull public VcsKey getSupportedVcs() { diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/roots/HgRootChecker.java b/plugins/hg4idea/src/org/zmlx/hg4idea/roots/HgRootChecker.java index 887568a6a15d..fb2143d01c4d 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/roots/HgRootChecker.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/roots/HgRootChecker.java @@ -17,6 +17,8 @@ package org.zmlx.hg4idea.roots; import com.intellij.openapi.vcs.VcsKey; import com.intellij.openapi.vcs.VcsRootChecker; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.zmlx.hg4idea.HgVcs; import org.zmlx.hg4idea.util.HgUtil; @@ -33,6 +35,11 @@ public class HgRootChecker extends VcsRootChecker { return new File(path, HgUtil.DOT_HG).exists(); } + @Override + public boolean isRoot(@NotNull VirtualFile maybeRoot) { + return VfsUtil.refreshAndFindChild(maybeRoot, HgUtil.DOT_HG) != null; + } + @NotNull @Override public VcsKey getSupportedVcs() {