Search in sources :

Example 1 with EmptyTreeIterator

use of org.eclipse.jgit.treewalk.EmptyTreeIterator 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 2 with EmptyTreeIterator

use of org.eclipse.jgit.treewalk.EmptyTreeIterator in project compiler by boalang.

the class GitCommit method getChangeFiles.

private void getChangeFiles(final RevCommit parent, final RevCommit rc, final HashMap<String, String> rChangedPaths, final HashMap<String, String> rRemovedPaths, final HashMap<String, String> rAddedPaths) {
    final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE);
    df.setRepository(repository);
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setDetectRenames(true);
    try {
        final AbstractTreeIterator parentIter;
        if (parent == null)
            parentIter = new EmptyTreeIterator();
        else
            parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree());
        for (final DiffEntry diff : df.scan(parentIter, new CanonicalTreeParser(null, repository.newObjectReader(), rc.getTree()))) {
            if (diff.getChangeType() == ChangeType.MODIFY || diff.getChangeType() == ChangeType.COPY || diff.getChangeType() == ChangeType.RENAME) {
                if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB && diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
                    String path = diff.getNewPath();
                    rChangedPaths.put(path, diff.getOldPath());
                    filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
                }
            } else if (diff.getChangeType() == ChangeType.ADD) {
                if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
                    String path = diff.getNewPath();
                    rAddedPaths.put(path, null);
                    filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
                }
            } else if (diff.getChangeType() == ChangeType.DELETE) {
                if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) {
                    rRemovedPaths.put(diff.getOldPath(), diff.getOldPath());
                }
            }
        }
    } catch (final IOException e) {
        if (debug)
            System.err.println("Git Error getting commit diffs: " + e.getMessage());
    }
    df.close();
}
Also used : AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) IOException(java.io.IOException) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 3 with EmptyTreeIterator

use of org.eclipse.jgit.treewalk.EmptyTreeIterator in project MGit by maks.

the class CommitDiffTask method getCommitDiff.

public boolean getCommitDiff() {
    try {
        Repository repo = mRepo.getGit().getRepository();
        mDiffOutput = new ByteArrayOutputStream();
        mDiffFormatter = new DiffFormatter(mDiffOutput);
        mDiffFormatter.setRepository(repo);
        AbstractTreeIterator mOldCommitTreeIterator = mRepo.isInitialCommit(mNewCommit) ? new EmptyTreeIterator() : getTreeIterator(repo, mOldCommit);
        AbstractTreeIterator mNewCommitTreeIterator = getTreeIterator(repo, mNewCommit);
        mDiffEntries = mDiffFormatter.scan(mOldCommitTreeIterator, mNewCommitTreeIterator);
        if (mShowDescription) {
            ObjectId newCommitId = repo.resolve(mNewCommit);
            mCommits = mRepo.getGit().log().add(newCommitId).setMaxCount(1).call();
        } else {
            mCommits = new ArrayList<RevCommit>();
        }
        return true;
    } catch (GitAPIException e) {
        setException(e);
    } catch (IncorrectObjectTypeException e) {
        setException(e, R.string.error_diff_failed);
    } catch (AmbiguousObjectException e) {
        setException(e, R.string.error_diff_failed);
    } catch (IOException e) {
        setException(e, R.string.error_diff_failed);
    } catch (IllegalStateException e) {
        setException(e, R.string.error_diff_failed);
    } catch (NullPointerException e) {
        setException(e, R.string.error_diff_failed);
    } catch (StopTaskException e) {
    }
    return false;
}
Also used : AmbiguousObjectException(org.eclipse.jgit.errors.AmbiguousObjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) Repository(org.eclipse.jgit.lib.Repository) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) StopTaskException(me.sheimi.sgit.exception.StopTaskException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with EmptyTreeIterator

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

the class DiffServlet method getTreeIterator.

private static AbstractTreeIterator getTreeIterator(RevWalk walk, ObjectId id) throws IOException {
    if (!id.equals(ObjectId.zeroId())) {
        CanonicalTreeParser p = new CanonicalTreeParser();
        p.reset(walk.getObjectReader(), walk.parseTree(id));
        return p;
    }
    return new EmptyTreeIterator();
}
Also used : EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 5 with EmptyTreeIterator

use of org.eclipse.jgit.treewalk.EmptyTreeIterator in project egit by eclipse.

the class FileDiff method compute.

/**
 * Computer file diffs for specified tree walk and commit
 *
 * @param repository
 * @param walk
 * @param commit
 * @param parents
 * @param markTreeFilters
 *            optional filters for marking entries, see
 *            {@link #isMarked(int)}
 * @return non-null but possibly empty array of file diffs
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 * @throws CorruptObjectException
 * @throws IOException
 */
public static FileDiff[] compute(final Repository repository, final TreeWalk walk, final RevCommit commit, final RevCommit[] parents, final TreeFilter... markTreeFilters) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    final ArrayList<FileDiff> r = new ArrayList<>();
    if (parents.length > 0) {
        walk.reset(trees(commit, parents));
    } else {
        walk.reset();
        walk.addTree(new EmptyTreeIterator());
        walk.addTree(commit.getTree());
    }
    if (walk.getTreeCount() <= 2) {
        List<DiffEntry> entries = DiffEntry.scan(walk, false, markTreeFilters);
        List<DiffEntry> xentries = new LinkedList<>(entries);
        RenameDetector detector = new RenameDetector(repository);
        detector.addAll(entries);
        List<DiffEntry> renames = detector.compute(walk.getObjectReader(), org.eclipse.jgit.lib.NullProgressMonitor.INSTANCE);
        for (DiffEntry m : renames) {
            final FileDiff d = new FileDiff(commit, m);
            r.add(d);
            for (Iterator<DiffEntry> i = xentries.iterator(); i.hasNext(); ) {
                DiffEntry n = i.next();
                if (m.getOldPath().equals(n.getOldPath()))
                    i.remove();
                else if (m.getNewPath().equals(n.getNewPath()))
                    i.remove();
            }
        }
        for (DiffEntry m : xentries) {
            final FileDiff d = new FileDiff(commit, m);
            r.add(d);
        }
    } else {
        // DiffEntry does not support walks with more than two trees
        final int nTree = walk.getTreeCount();
        final int myTree = nTree - 1;
        TreeFilterMarker treeFilterMarker = new TreeFilterMarker(markTreeFilters);
        while (walk.next()) {
            if (matchAnyParent(walk, myTree))
                continue;
            int treeFilterMarks = treeFilterMarker.getMarks(walk);
            final FileDiffForMerges d = new FileDiffForMerges(commit, treeFilterMarks);
            d.path = walk.getPathString();
            int m0 = 0;
            for (int i = 0; i < myTree; i++) m0 |= walk.getRawMode(i);
            final int m1 = walk.getRawMode(myTree);
            d.change = ChangeType.MODIFY;
            if (m0 == 0 && m1 != 0)
                d.change = ChangeType.ADD;
            else if (m0 != 0 && m1 == 0)
                d.change = ChangeType.DELETE;
            else if (m0 != m1 && walk.idEqual(0, myTree))
                // there is no ChangeType.TypeChanged
                d.change = ChangeType.MODIFY;
            d.blobs = new ObjectId[nTree];
            d.modes = new FileMode[nTree];
            for (int i = 0; i < nTree; i++) {
                d.blobs[i] = walk.getObjectId(i);
                d.modes[i] = walk.getFileMode(i);
            }
            r.add(d);
        }
    }
    final FileDiff[] tmp = new FileDiff[r.size()];
    r.toArray(tmp);
    return tmp;
}
Also used : TreeFilterMarker(org.eclipse.jgit.treewalk.filter.TreeFilterMarker) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) RenameDetector(org.eclipse.jgit.diff.RenameDetector) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)16 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Git (org.eclipse.jgit.api.Git)6 Test (org.junit.Test)6 ObjectId (org.eclipse.jgit.lib.ObjectId)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 IOException (java.io.IOException)4 DiffEntry (org.eclipse.jgit.diff.DiffEntry)4 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)4 ArrayList (java.util.ArrayList)3 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 Repository (org.eclipse.jgit.lib.Repository)2 RevTree (org.eclipse.jgit.revwalk.RevTree)2 AbstractTreeIterator (org.eclipse.jgit.treewalk.AbstractTreeIterator)2 RefModel (com.gitblit.models.RefModel)1