From e6c8b18c256d3896ee9b910be905e62f9cc55d78 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Fri, 12 May 2017 19:46:12 +0300 Subject: [PATCH] Reuse more correct method to find the Git root directory This fixes IDEA-159506 --- plugins/git4idea/src/git4idea/GitUtil.java | 30 ++++++---------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/plugins/git4idea/src/git4idea/GitUtil.java b/plugins/git4idea/src/git4idea/GitUtil.java index 87e8a8db6d09..038ac6e059b4 100644 --- a/plugins/git4idea/src/git4idea/GitUtil.java +++ b/plugins/git4idea/src/git4idea/GitUtil.java @@ -35,7 +35,6 @@ import com.intellij.openapi.vcs.changes.ChangeListManager; import com.intellij.openapi.vcs.changes.ChangeListManagerEx; import com.intellij.openapi.vcs.changes.ContentRevision; import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList; -import com.intellij.openapi.vcs.vfs.AbstractVcsVirtualFile; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; @@ -328,15 +327,10 @@ public class GitUtil { public static Set gitRootsForPaths(final Collection roots) { HashSet rc = new HashSet<>(); for (VirtualFile root : roots) { - VirtualFile f = root; - do { - if (f.findFileByRelativePath(DOT_GIT) != null) { - rc.add(f); - break; - } - f = f.getParent(); + VirtualFile gitRoot = getGitRootOrNull(VcsUtil.getFilePath(root)); + if (gitRoot != null) { + rc.add(gitRoot); } - while (f != null); } return rc; } @@ -373,10 +367,12 @@ public class GitUtil { @Nullable public static VirtualFile getGitRootOrNull(@NotNull final FilePath filePath) { File root = filePath.getIOFile(); - while (root != null && (!root.exists() || !root.isDirectory() || !new File(root, DOT_GIT).exists())) { + while (root != null) { + File gitDir = findGitDir(root); + if (gitDir != null) return LocalFileSystem.getInstance().findFileByIoFile(root); root = root.getParentFile(); } - return root == null ? null : LocalFileSystem.getInstance().findFileByIoFile(root); + return null; } public static boolean isGitRoot(@NotNull File folder) { @@ -414,17 +410,7 @@ public class GitUtil { */ @Nullable public static VirtualFile gitRootOrNull(final VirtualFile file) { - if (file instanceof AbstractVcsVirtualFile) { - return getGitRootOrNull(VcsUtil.getFilePath(file.getPath())); - } - VirtualFile root = file; - while (root != null) { - if (root.findFileByRelativePath(DOT_GIT) != null) { - return root; - } - root = root.getParent(); - } - return root; + return getGitRootOrNull(VcsUtil.getFilePath(file.getPath())); } /**