/** * 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; }
public static File getGitDir(File file) { if (file.exists()) { while (file != null) { if (RepositoryCache.FileKey.isGitRepository(file, FS.DETECTED)) { return file; } else if (RepositoryCache.FileKey.isGitRepository( new File(file, Constants.DOT_GIT), FS.DETECTED)) { return new File(file, Constants.DOT_GIT); } file = file.getParentFile(); } } return null; }
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; } }