org.gitistics.visitor.commit.TreeWalkVisitorRoot.java Source code

Java tutorial

Introduction

Here is the source code for org.gitistics.visitor.commit.TreeWalkVisitorRoot.java

Source

/*******************************************************************************
 * Copyright (C) 2013 Steven Rowlands
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.gitistics.visitor.commit;

import org.eclipse.jgit.diff.DiffAlgorithm;
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.diff.EditList;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.gitistics.treewalk.TreeWalkUtils;
import org.gitistics.visitor.commit.filechange.FileChange;
import org.gitistics.visitor.commit.filechange.FileChanges;
import org.gitistics.visitor.commit.filechange.FileEdits;

public class TreeWalkVisitorRoot extends AbstractCommitVisitor {

    private DiffAlgorithm algorithm = DiffAlgorithm.getAlgorithm(SupportedAlgorithm.HISTOGRAM);

    private ObjectReader reader;

    private Repository repository;

    public TreeWalkVisitorRoot(Repository repository) {
        this.repository = repository;
        this.reader = repository.newObjectReader();
    }

    public void visit(RevCommit commit) {
        if (commit.getParentCount() != 0)
            return;

        TreeWalk walk = TreeWalkUtils.treeWalkForCommit(repository, commit);
        try {
            FileChanges changes = new FileChanges(commit);
            while (walk.next()) {
                EditList edits = handleEdits(commit, walk);
                FileChange file = new FileChange();
                FileEdits fileEdits = new FileEdits();
                file.addEdit(fileEdits);
                file.setPath(walk.getPathString());
                fileEdits.setChangeType(ChangeType.ADD);
                fileEdits.setEdits(edits);
                file.setCommit(commit);
                changes.addChange(file.getPath(), file);
            }
            callback(changes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            walk.release();
        }
    }

    public EditList handleEdits(RevCommit commit, TreeWalk walk) {
        if (!hasCallbacks())
            return null;

        try {
            ObjectLoader loader = reader.open(walk.getObjectId(walk.getTreeCount() - 1));

            RawText a = new RawText(loader.getBytes());
            RawText b = new RawText(new byte[0]);

            return algorithm.diff(RawTextComparator.DEFAULT, b, a);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            reader.release();
        }
    }
}