Search in sources :

Example 6 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project egit by eclipse.

the class GitCommitsModelCache method getChangedObjects.

private static Map<String, Change> getChangedObjects(Repository repo, RevCommit commit, RevCommit parentCommit, TreeFilter pathFilter, final int direction) throws IOException {
    final Map<String, Change> result = new HashMap<String, GitCommitsModelCache.Change>();
    try (final TreeWalk tw = new TreeWalk(repo)) {
        int commitIndex = addTree(tw, commit);
        int parentCommitIndex = addTree(tw, parentCommit);
        tw.setRecursive(true);
        if (pathFilter == null)
            tw.setFilter(ANY_DIFF);
        else
            tw.setFilter(AndTreeFilter.create(ANY_DIFF, pathFilter));
        final AbbreviatedObjectId commitId = getAbbreviatedObjectId(commit);
        final AbbreviatedObjectId parentCommitId = getAbbreviatedObjectId(parentCommit);
        MutableObjectId idBuf = new MutableObjectId();
        while (tw.next()) {
            Change change = new Change();
            change.commitId = commitId;
            change.remoteCommitId = parentCommitId;
            change.name = tw.getNameString();
            tw.getObjectId(idBuf, commitIndex);
            change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
            tw.getObjectId(idBuf, parentCommitIndex);
            change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);
            calculateAndSetChangeKind(direction, change);
            result.put(tw.getPathString(), change);
        }
    }
    return result.size() > 0 ? result : null;
}
Also used : MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) HashMap(java.util.HashMap) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 7 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project fuse-karaf by jboss-fuse.

the class DiffUtils method diff.

private static void diff(Git git, ObjectReader reader, DiffEntry diff, Writer result) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DiffFormatter df = new DiffFormatter(baos);
    df.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM));
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setRepository(git.getRepository());
    df.format(diff);
    // System.out.println(new String(baos.toByteArray()));
    AbbreviatedObjectId id1 = diff.getOldId();
    AbbreviatedObjectId id2 = diff.getNewId();
    byte[] bytes1 = reader.open(id1.toObjectId()).getBytes();
    byte[] bytes2 = reader.open(id2.toObjectId()).getBytes();
    RawText rt1 = new RawText(bytes1);
    RawText rt2 = new RawText(bytes2);
    EditList edits = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM).diff(RawTextComparator.DEFAULT, rt1, rt2);
    int aCur = 0;
    for (Edit curEdit : edits) {
        boolean prolog = aCur < curEdit.getBeginA();
        if (prolog) {
            result.write("<div class=\"edit unchanged\">");
        }
        while (aCur < curEdit.getBeginA()) {
            result.write(html(rt1.getString(aCur++)) + "\n");
        }
        if (prolog) {
            result.write("</div>");
        }
        if (curEdit.getType() == Edit.Type.INSERT) {
            result.write("<div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                result.write(html(rt2.getString(i)) + "\n");
            }
            result.write("</div>");
        }
        if (curEdit.getType() == Edit.Type.REPLACE) {
            result.write("<div class=\"edit changed\"><div class=\"edit removed\">");
            for (int i = curEdit.getBeginA(); i < curEdit.getEndA(); i++) {
                result.write(html(rt1.getString(i)) + "\n");
            }
            result.write("</div><div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                result.write(html(rt2.getString(i)) + "\n");
            }
            aCur = curEdit.getEndA();
            result.write("</div></div>");
        }
        if (curEdit.getType() == Edit.Type.DELETE) {
            result.write("<div class=\"edit changed\"><div class=\"edit removed\">");
            for (int i = curEdit.getBeginA(); i < curEdit.getEndA(); i++) {
                result.write(html(rt1.getString(i)) + "\n");
            }
            aCur = curEdit.getEndA();
            result.write("</div></div>");
        }
    }
    boolean prolog = aCur < rt1.size();
    if (prolog) {
        result.write("<div class=\"edit unchanged\">");
    }
    while (aCur < rt1.size()) {
        result.write(html(rt1.getString(aCur++)) + "\n");
    }
    if (prolog) {
        result.write("</div>");
    }
}
Also used : AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) Edit(org.eclipse.jgit.diff.Edit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EditList(org.eclipse.jgit.diff.EditList) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) RawText(org.eclipse.jgit.diff.RawText)

Aggregations

AbbreviatedObjectId (org.eclipse.jgit.lib.AbbreviatedObjectId)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)2 Edit (org.eclipse.jgit.diff.Edit)2 EditList (org.eclipse.jgit.diff.EditList)2 RawText (org.eclipse.jgit.diff.RawText)2 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IResource (org.eclipse.core.resources.IResource)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1