/** * 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; }
@Test public void testCloneOverSshWithPassphraseProtectedKey() throws Exception { Assume.assumeTrue(sshRepo2 != null); Assume.assumeTrue(privateKey != null); Assume.assumeTrue(passphrase != null); URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); URIish uri = new URIish(sshRepo2); WebRequest request = new PostGitCloneRequest() .setURIish(uri) .setFilePath(clonePath) .setKnownHosts(knownHosts2) .setPrivateKey(privateKey) .setPublicKey(publicKey) .setPassphrase(passphrase) .getWebRequest(); String contentLocation = clone(request); File file = getRepositoryForContentLocation(contentLocation).getDirectory().getParentFile(); assertTrue(file.exists()); assertTrue(file.isDirectory()); assertTrue( RepositoryCache.FileKey.isGitRepository(new File(file, Constants.DOT_GIT), FS.DETECTED)); }
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; } }
public static boolean isRepo(File directory) { File gitDir = new File(directory, ".git"); return gitDir.exists() && RepositoryCache.FileKey.isGitRepository(gitDir, FS.DETECTED); }