Search in sources :

Example 1 with CanonicalTreeParser

use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project che by eclipse.

the class JGitDiffPage method commitToWorkingTree.

/**
     * Show changes between specified revision and working tree.
     *
     * @param commitId
     *            id of commit
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> commitToWorkingTree(String commitId, DiffFormatter formatter) throws IOException {
    ObjectId commitA = repository.resolve(commitId);
    if (commitA == null) {
        File heads = new File(repository.getWorkTree().getPath() + "/.git/refs/heads");
        if (heads.exists() && heads.list().length == 0) {
            return Collections.emptyList();
        }
        throw new IllegalArgumentException("Invalid commit id " + commitId);
    }
    RevTree treeA;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        treeA = revWalkA.parseTree(commitA);
    }
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        CanonicalTreeParser iterA = new CanonicalTreeParser();
        iterA.reset(reader, treeA);
        FileTreeIterator iterB = new FileTreeIterator(repository);
        // Seems bug in DiffFormatter when work with working. Disable detect
        // renames by formatter and do it later.
        formatter.setDetectRenames(false);
        diff = formatter.scan(iterA, iterB);
        if (!params.isNoRenames()) {
            // Detect renames.
            RenameDetector renameDetector = createRenameDetector();
            ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
            renameDetector.addAll(diff);
            diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
        }
    }
    return diff;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) ContentSource(org.eclipse.jgit.diff.ContentSource) RenameDetector(org.eclipse.jgit.diff.RenameDetector) ObjectReader(org.eclipse.jgit.lib.ObjectReader) File(java.io.File) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) RevTree(org.eclipse.jgit.revwalk.RevTree) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 2 with CanonicalTreeParser

use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project che by eclipse.

the class JGitDiffPage method emptyToCommit.

/**
     * Show changes between specified revision and empty tree.
     *
     * @param commitId
     *            id of commit
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> emptyToCommit(String commitId, DiffFormatter formatter) throws IOException {
    ObjectId commit = repository.resolve(commitId);
    checkArgument(commit != null, "Invalid commit id " + commitId);
    RevTree tree;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        tree = revWalkA.parseTree(commit);
    }
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        CanonicalTreeParser iterator = new CanonicalTreeParser();
        iterator.reset(reader, tree);
        diff = formatter.scan(new EmptyTreeIterator(), iterator);
    }
    return diff;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 3 with CanonicalTreeParser

use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project gitblit by gitblit.

the class JGitUtils method getTreeEntries.

/**
	 * Returns all tree entries that do not match the ignore paths.
	 *
	 * @param db
	 * @param ignorePaths
	 * @param dcBuilder
	 * @throws IOException
	 */
public static List<DirCacheEntry> getTreeEntries(Repository db, String branch, Collection<String> ignorePaths) throws IOException {
    List<DirCacheEntry> list = new ArrayList<DirCacheEntry>();
    TreeWalk tw = null;
    try {
        ObjectId treeId = db.resolve(branch + "^{tree}");
        if (treeId == null) {
            // branch does not exist yet
            return list;
        }
        tw = new TreeWalk(db);
        int hIdx = tw.addTree(treeId);
        tw.setRecursive(true);
        while (tw.next()) {
            String path = tw.getPathString();
            CanonicalTreeParser hTree = null;
            if (hIdx != -1) {
                hTree = tw.getTree(hIdx, CanonicalTreeParser.class);
            }
            if (!ignorePaths.contains(path)) {
                // add all other tree entries
                if (hTree != null) {
                    final DirCacheEntry entry = new DirCacheEntry(path);
                    entry.setObjectId(hTree.getEntryObjectId());
                    entry.setFileMode(hTree.getEntryFileMode());
                    list.add(entry);
                }
            }
        }
    } finally {
        if (tw != null) {
            tw.close();
        }
    }
    return list;
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 4 with CanonicalTreeParser

use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project gocd by gocd.

the class ConfigRepository method contentFromTree.

private String contentFromTree(RevTree tree) {
    try {
        final ObjectReader reader = gitRepo.newObjectReader();
        CanonicalTreeParser parser = new CanonicalTreeParser();
        parser.reset(reader, tree);
        String lastPath = null;
        while (true) {
            final String path = parser.getEntryPathString();
            parser = parser.next();
            if (path.equals(lastPath)) {
                break;
            }
            lastPath = path;
            if (path.equals(CRUISE_CONFIG_XML)) {
                final ObjectId id = parser.getEntryObjectId();
                final ObjectLoader loader = reader.open(id);
                return new String(loader.getBytes());
            }
        }
        return null;
    } catch (IOException e) {
        LOGGER.error("Could not fetch content from the config repository found at path '{}'", workingDir.getAbsolutePath(), e);
        throw new RuntimeException("Error while fetching content from the config repository.", e);
    }
}
Also used : IOException(java.io.IOException) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 5 with CanonicalTreeParser

use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project gitiles by GerritCodeReview.

the class PathServlet method getOnlyChildSubtree.

private CanonicalTreeParser getOnlyChildSubtree(ObjectReader reader, ObjectId id, byte[] prefix) throws IOException {
    CanonicalTreeParser p = new CanonicalTreeParser(prefix, reader, id);
    if (p.eof() || p.getEntryFileMode() != FileMode.TREE) {
        return null;
    }
    p.next(1);
    return p.eof() ? p : null;
}
Also used : CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Aggregations

CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)15 DiffEntry (org.eclipse.jgit.diff.DiffEntry)8 ObjectId (org.eclipse.jgit.lib.ObjectId)7 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 ArrayList (java.util.ArrayList)4 RevTree (org.eclipse.jgit.revwalk.RevTree)4 EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)4 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)4 IOException (java.io.IOException)3 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 DirCache (org.eclipse.jgit.dircache.DirCache)3 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)3 DirCacheBuilder (org.eclipse.jgit.dircache.DirCacheBuilder)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)2 AndTreeFilter (org.eclipse.jgit.treewalk.filter.AndTreeFilter)2 TreeFilter (org.eclipse.jgit.treewalk.filter.TreeFilter)2 RefModel (com.gitblit.models.RefModel)1 CodeIndexDocument (com.searchcode.app.dto.CodeIndexDocument)1