/** * Returns the existing git repositories for the given file path, following the given traversal * rule. * * @param path expected format /file/{Workspace}/{projectName}[/{path}] * @return a map of all git repositories found, or <code>null</code> if the provided path format * doesn't match the expected format. * @throws CoreException */ public static Map<IPath, File> getGitDirs(IPath path, Traverse traverse) throws CoreException { IPath p = path.removeFirstSegments(1); // remove /file IFileStore fileStore = NewFileServlet.getFileStore(null, p); if (fileStore == null) return null; Map<IPath, File> result = new HashMap<IPath, File>(); File file = fileStore.toLocalFile(EFS.NONE, null); // jgit can only handle a local file if (file == null) return result; switch (traverse) { case CURRENT: if (RepositoryCache.FileKey.isGitRepository(file, FS.DETECTED)) { result.put(new Path(""), file); // $NON-NLS-1$ } else if (RepositoryCache.FileKey.isGitRepository( new File(file, Constants.DOT_GIT), FS.DETECTED)) { result.put(new Path(""), new File(file, Constants.DOT_GIT)); // $NON-NLS-1$ } break; case GO_UP: getGitDirsInParents(file, result); break; case GO_DOWN: getGitDirsInChildren(file, p, result); break; } return result; }
/** * Returns the file representing the Git repository directory for the given file path or any of * its parent in the filesystem. If the file doesn't exits, is not a Git repository or an error * occurred while transforming the given path into a store <code>null</code> is returned. * * @param path expected format /file/{Workspace}/{projectName}[/{path}] * @return the .git folder if found or <code>null</code> the give path cannot be resolved to a * file or it's not under control of a git repository * @throws CoreException */ public static File getGitDir(IPath path) throws CoreException { Map<IPath, File> gitDirs = GitUtils.getGitDirs(path, Traverse.GO_UP); if (gitDirs == null) return null; Collection<File> values = gitDirs.values(); if (values.isEmpty()) return null; return values.toArray(new File[] {})[0]; }
private static void getGitDirsInParents(File file, Map<IPath, File> gitDirs) { int levelUp = 0; File workspaceRoot = Activator.getDefault().getPlatformLocation().toFile(); while (file != null && !file.getAbsolutePath().equals(workspaceRoot.getAbsolutePath())) { if (file.exists()) { if (RepositoryCache.FileKey.isGitRepository(file, FS.DETECTED)) { gitDirs.put(getPathForLevelUp(levelUp), file); return; } else if (RepositoryCache.FileKey.isGitRepository( new File(file, Constants.DOT_GIT), FS.DETECTED)) { gitDirs.put(getPathForLevelUp(levelUp), new File(file, Constants.DOT_GIT)); return; } } file = file.getParentFile(); levelUp++; } return; }
/** Recursively walks down a directory tree and collects the paths of all git repositories. */ private static void getGitDirsInChildren(File localFile, IPath path, Map<IPath, File> gitDirs) throws CoreException { if (localFile.exists() && localFile.isDirectory()) { if (RepositoryCache.FileKey.isGitRepository(localFile, FS.DETECTED)) { gitDirs.put(path.addTrailingSeparator(), localFile); return; } else if (RepositoryCache.FileKey.isGitRepository( new File(localFile, Constants.DOT_GIT), FS.DETECTED)) { gitDirs.put(path.addTrailingSeparator(), new File(localFile, Constants.DOT_GIT)); return; } File[] folders = localFile.listFiles( new FileFilter() { public boolean accept(File file) { return file.isDirectory() && !file.getName().equals(Constants.DOT_GIT); } }); for (File folder : folders) { getGitDirsInChildren(folder, path.append(folder.getName()), gitDirs); } return; } }