/** * Open a tree walk and filter to exactly one path. * <p> * The returned tree walk is already positioned on the requested path, so * the caller should not need to invoke {@link #next()} unless they are * looking for a possible directory/file name conflict. * * @param reader * the reader the walker will obtain tree data from. * @param path * single path to advance the tree walk instance into. * @param trees * one or more trees to walk through, all with the same root. * @return a new tree walk configured for exactly this one path; null if no * path was found in any of the trees. * @throws java.io.IOException * reading a pack file or loose object failed. * @throws org.eclipse.jgit.errors.CorruptObjectException * an tree object could not be read as its data stream did not * appear to be a tree, or could not be inflated. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException * an object we expected to be a tree was not a tree. * @throws org.eclipse.jgit.errors.MissingObjectException * a tree object was not found. */ public static TreeWalk forPath(final ObjectReader reader, final String path, final AnyObjectId... trees) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { return forPath(null, reader, path, trees); }
/** * Open a tree walk and filter to exactly one path. * <p> * The returned tree walk is already positioned on the requested path, so * the caller should not need to invoke {@link #next()} unless they are * looking for a possible directory/file name conflict. * * @param db * repository to read tree object data from. * @param path * single path to advance the tree walk instance into. * @param tree * the single tree to walk through. * @return a new tree walk configured for exactly this one path; null if no * path was found in any of the trees. * @throws java.io.IOException * reading a pack file or loose object failed. * @throws org.eclipse.jgit.errors.CorruptObjectException * an tree object could not be read as its data stream did not * appear to be a tree, or could not be inflated. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException * an object we expected to be a tree was not a tree. * @throws org.eclipse.jgit.errors.MissingObjectException * a tree object was not found. */ public static TreeWalk forPath(final Repository db, final String path, final RevTree tree) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { return forPath(db, path, new ObjectId[] { tree }); }
private static TreeWalk buildTreeWalk(Repository repository, RevTree tree, final String path) throws IOException { TreeWalk treeWalk = TreeWalk.forPath(repository, path, tree); if(treeWalk == null) { throw new FileNotFoundException("Did not find expected file '" + path + "' in tree '" + tree.getName() + "'"); } return treeWalk; } }
private static TreeWalk buildTreeWalk(Repository repository, RevTree tree, final String path) throws IOException { TreeWalk treeWalk = TreeWalk.forPath(repository, path, tree); if(treeWalk == null) { throw new FileNotFoundException("Did not find expected file '" + path + "' in tree '" + tree.getName() + "'"); } return treeWalk; } }
IncorrectObjectTypeException, CorruptObjectException, IOException { try (ObjectReader reader = db.newObjectReader()) { return forPath(db, reader, path, trees);
private TreeWalk newTreeWalk(String refName) throws IOException { if (commit == null) { return null; } return TreeWalk.forPath(reader, pathName(refName), commit.getTree()); }
private static byte[] read(Repository db, AnyObjectId treeish, String path) throws MissingObjectException, IncorrectObjectTypeException, IOException { try (ObjectReader or = db.newObjectReader()) { TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish)); if (tree == null) throw new FileNotFoundException(MessageFormat.format(JGitText .get().entryNotFoundByPath, path)); return read(or, tree.getObjectId(0)); } }
/** * Get attributes for given path and commit * * @param db * the repository * @param path * the path to find attributes for * @param commit * the commit to inspect. * @return the {@link Attributes} for the given path. * @throws IOException * in case of an error */ public static Attributes getAttributesForPath(Repository db, String path, RevCommit commit) throws IOException { if (commit == null) { return getAttributesForPath(db, path); } try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) { Attributes attr = walk == null ? null : walk.getAttributes(); if (attr == null) { throw new IOException(MessageFormat .format(JGitText.get().noPathAttributesFound, path)); } return attr; } }
private static CanonicalTreeParser createParserAtPath(ObjectReader reader, AnyObjectId srcId, String prefix) throws IOException { ObjectId root = toTree(reader, srcId); if (prefix.isEmpty()) { return new CanonicalTreeParser(BINARY_R_REFS, reader, root); } String dir = RefTree.refPath(Paths.stripTrailingSeparator(prefix)); TreeWalk tw = TreeWalk.forPath(reader, dir, root); if (tw == null || !tw.isSubtree()) { return null; } ObjectId id = tw.getObjectId(0); return new CanonicalTreeParser(encode(prefix), reader, id); }
return tree.copy(); TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), new String(revChars, i + 1, revChars.length - i - 1), tree);
@Override public RemoteFile readFileWithMode(String uri, String ref, String path) throws GitAPIException, IOException { File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$ try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir) .setURI(uri).call()) { Repository repo = git.getRepository(); ObjectId refCommitId = sha1(uri, ref); if (refCommitId == null) { throw new InvalidRefNameException(MessageFormat .format(JGitText.get().refNotResolved, ref)); } RevCommit commit = repo.parseCommit(refCommitId); TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); // TODO(ifrade): Cope better with big files (e.g. using // InputStream instead of byte[]) return new RemoteFile( tw.getObjectReader().open(tw.getObjectId(0)) .getCachedBytes(Integer.MAX_VALUE), tw.getFileMode(0)); } finally { FileUtils.delete(dir, FileUtils.RECURSIVE); } } }
@Nullable public static TreeWalk forPath(String path, AnyObjectId tree, ObjectReader reader) throws IOException { return TreeWalk.forPath(reader, normalizeNodePath(path), tree); }
@Nullable public static TreeWalk forPath(String path, AnyObjectId tree, ObjectReader reader) throws IOException { return TreeWalk.forPath(reader, normalizeNodePath(path), tree); }
private TreeWalk newTreeWalk(String refName) throws IOException { if (commit == null) { return null; } return TreeWalk.forPath(reader, pathName(refName), commit.getTree()); }
private TreeWalk newTreeWalk(String refName) throws IOException { if (commit == null) { return null; } return TreeWalk.forPath(reader, pathName(refName), commit.getTree()); }
@NotNull public static String loadRepositoryId(@NotNull ObjectReader objectReader, ObjectId commit) throws IOException { RevWalk revWalk = new RevWalk(objectReader); TreeWalk treeWalk = TreeWalk.forPath(objectReader, ENTRY_UUID, revWalk.parseCommit(commit).getTree()); if (treeWalk != null) { return GitRepository.loadContent(objectReader, treeWalk.getObjectId(0)); } throw new FileNotFoundException(ENTRY_UUID); }
@Override public void collect(int doc) throws IOException { if (hits.size() < query.getCount() && !Thread.currentThread().isInterrupted()) { Preconditions.checkState(blobPathValues.advanceExact(doc)); String blobPath = blobPathValues.binaryValue().utf8ToString(); if (!checkedBlobPaths.contains(blobPath)) { TreeWalk treeWalk = TreeWalk.forPath(project.getRepository(), blobPath, revTree); if (treeWalk != null) query.collect(searcher, treeWalk, hits); checkedBlobPaths.add(blobPath); } } }
private static byte[] read(Repository db, AnyObjectId treeish, String path) throws MissingObjectException, IncorrectObjectTypeException, IOException { try (ObjectReader or = db.newObjectReader()) { TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish)); if (tree == null) throw new FileNotFoundException(MessageFormat.format(JGitText .get().entryNotFoundByPath, path)); return read(or, tree.getObjectId(0)); } }
private static byte[] read(Repository db, AnyObjectId treeish, String path) throws MissingObjectException, IncorrectObjectTypeException, IOException { try (ObjectReader or = db.newObjectReader()) { TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish)); if (tree == null) throw new FileNotFoundException(MessageFormat.format(JGitText .get().entryNotFoundByPath, path)); return read(or, tree.getObjectId(0)); } }
private static CanonicalTreeParser createParserAtPath(ObjectReader reader, AnyObjectId srcId, String prefix) throws IOException { ObjectId root = toTree(reader, srcId); if (prefix.isEmpty()) { return new CanonicalTreeParser(BINARY_R_REFS, reader, root); } String dir = RefTree.refPath(Paths.stripTrailingSeparator(prefix)); TreeWalk tw = TreeWalk.forPath(reader, dir, root); if (tw == null || !tw.isSubtree()) { return null; } ObjectId id = tw.getObjectId(0); return new CanonicalTreeParser(encode(prefix), reader, id); }