/** * Set tree filter * * @param filter * a {@link org.eclipse.jgit.treewalk.filter.TreeFilter} object. * @return this generator */ public SubmoduleWalk setFilter(TreeFilter filter) { walk.setFilter(filter); return this; }
private void seek(String path) throws IOException { if (!path.equals(current)) { iterator.reset(); tw.reset(); tw.addTree(iterator); tw.setFilter(PathFilter.create(path)); current = path; if (!tw.next()) throw new FileNotFoundException(path); ptr = tw.getTree(0, WorkingTreeIterator.class); if (ptr == null) throw new FileNotFoundException(path); } } }
/** * Create a filter for the first phase of a parent-rewriting limited revision * walk. * <p> * This filter is ANDed to evaluate before all other filters and ties the * configured {@link TreeFilter} into the revision walking process. * <p> * If no interesting tree entries are found the commit is colored with * {@code rewriteFlag}, allowing a later pass implemented by * {@link RewriteGenerator} to remove those colored commits from the DAG. * * @see RewriteGenerator * * @param walker * walker used for reading trees. * @param t * filter to compare against any changed paths in each commit. If a * {@link FollowFilter}, will be replaced with a new filter * following new paths after a rename. * @param rewriteFlag * flag to color commits to be removed from the simplified DAT. */ TreeRevFilter(final RevWalk walker, final TreeFilter t, final int rewriteFlag) { pathFilter = new TreeWalk(walker.reader); pathFilter.setFilter(t); pathFilter.setRecursive(t.shouldBeRecursive()); this.rewriteFlag = rewriteFlag; }
private static void printDirectory(Repository repository, RevTree tree) throws IOException { // look at directory, this has FileMode.TREE try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("src")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected folder 'src'"); } // FileMode now indicates that this is a directory, i.e. FileMode.TREE.equals(fileMode) holds true FileMode fileMode = treeWalk.getFileMode(0); System.out.println("src: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode); } }
private static void printDirectory(Repository repository, RevTree tree) throws IOException { // look at directory, this has FileMode.TREE try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("src")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected folder 'src'"); } // FileMode now indicates that this is a directory, i.e. FileMode.TREE.equals(fileMode) holds true FileMode fileMode = treeWalk.getFileMode(0); System.out.println("src: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode); } }
private static int countLinesOfFileInCommit(Repository repository, ObjectId commitID, String name) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(commitID); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(name)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // load the content of the file into a stream ByteArrayOutputStream stream = new ByteArrayOutputStream(); loader.copyTo(stream); revWalk.dispose(); return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8").size(); } } } }
private static int countLinesOfFileInCommit(Repository repository, ObjectId commitID, String name) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(commitID); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(name)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // load the content of the file into a stream ByteArrayOutputStream stream = new ByteArrayOutputStream(); loader.copyTo(stream); revWalk.dispose(); return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8").size(); } } } }
private boolean find(RevCommit commit, PathFilter path) throws IOException { treeWalk.setFilter(path); treeWalk.reset(commit.getTree()); if (treeWalk.next() && isFile(treeWalk.getRawMode(0))) { treeWalk.getObjectId(idBuf, 0); return true; } return false; }
File repoDir = new File("test-git"); // open the repository Repository repository = new Repository(repoDir); // find the HEAD ObjectId lastCommitId = repository.resolve(Constants.HEAD); // now we have to get the commit RevWalk revWalk = new RevWalk(repository); RevCommit commit = revWalk.parseCommit(lastCommitId); // and using commit's tree find the path RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) { return null; } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // and then one can use either InputStream in = loader.openStream() // or loader.copyTo(out)
private static void printFile(Repository repository, RevTree tree) throws IOException { // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } }
private static void printFile(Repository repository, RevTree tree) throws IOException { // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } }
private DiffEntry findRename(RevCommit parent, RevCommit commit, PathFilter path) throws IOException { if (renameDetector == null) return null; treeWalk.setFilter(TreeFilter.ANY_DIFF); treeWalk.reset(parent.getTree(), commit.getTree()); renameDetector.reset(); renameDetector.addAll(DiffEntry.scan(treeWalk)); for (DiffEntry ent : renameDetector.compute()) { if (isRename(ent) && ent.getNewPath().equals(path.getPath())) return ent; } return null; }
private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { TreeWalk tw = pathFilter; FollowFilter oldFilter = (FollowFilter) tw.getFilter(); tw.setFilter(TreeFilter.ANY_DIFF); tw.reset(trees); List<DiffEntry> files = DiffEntry.scan(tw); RenameDetector rd = new RenameDetector(tw.getObjectReader(), cfg); rd.addAll(files); files = rd.compute(); TreeFilter newFilter = oldFilter; for (DiffEntry ent : files) { if (isRename(ent) && ent.getNewPath().equals(oldFilter.getPath())) { newFilter = FollowFilter.create(ent.getOldPath(), cfg); RenameCallback callback = oldFilter.getRenameCallback(); if (callback != null) { callback.renamed(ent); // forward the callback to the new follow filter ((FollowFilter) newFilter).setRenameCallback(callback); } break; } } tw.setFilter(newFilter); }
walk.addTree(new FileTreeIterator(db)); PathFilter f = PathFilter.create(path); walk.setFilter(f); walk.setRecursive(false); Attributes attr = null;
treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'");
treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'");
if (paths.isEmpty()) return; walk.setFilter(PathFilterGroup.createFromStrings(paths));
treeWalk.setRecursive(true); if (!checkoutAllPaths) treeWalk.setFilter(PathFilterGroup.createFromStrings(paths)); if (isCheckoutIndex()) checkoutPathsFromIndex(treeWalk, dc);
else tw.addTree(new EmptyTreeIterator()); tw.setFilter(PathFilterGroup.createFromStrings(filepaths)); tw.setRecursive(true);
treeWalk.setFilter(TreeFilter.ANY_DIFF);