Search in sources :

Example 1 with HunkHeader

use of org.eclipse.jgit.patch.HunkHeader in project repairnator by Spirals-Team.

the class CommitPatch method computePatchStats.

private void computePatchStats(Git git, RevCommit headRev, RevCommit commit) {
    try {
        ObjectReader reader = git.getRepository().newObjectReader();
        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        oldTreeIter.reset(reader, headRev.getTree());
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        newTreeIter.reset(reader, commit.getTree());
        DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
        diffFormatter.setRepository(git.getRepository());
        diffFormatter.setContext(0);
        List<DiffEntry> entries = diffFormatter.scan(newTreeIter, oldTreeIter);
        int nbLineAdded = 0;
        int nbLineDeleted = 0;
        Set<String> changedFiles = new HashSet<>();
        for (DiffEntry entry : entries) {
            String path;
            if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) {
                path = entry.getOldPath();
            } else {
                path = entry.getNewPath();
            }
            if (!path.contains("repairnator")) {
                changedFiles.add(path);
                FileHeader fileHeader = diffFormatter.toFileHeader(entry);
                List<? extends HunkHeader> hunks = fileHeader.getHunks();
                for (HunkHeader hunk : hunks) {
                    nbLineAdded += hunk.getOldImage().getLinesAdded();
                    nbLineDeleted += hunk.getOldImage().getLinesDeleted();
                }
            }
        }
        Metrics metric = this.getInspector().getJobStatus().getMetrics();
        metric.setPatchAddedLines(nbLineAdded);
        metric.setPatchDeletedLines(nbLineDeleted);
        metric.setPatchChangedFiles(changedFiles.size());
    } catch (IOException e) {
        this.getLogger().error("Error while computing stat on the patch", e);
    }
}
Also used : IOException(java.io.IOException) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) Metrics(fr.inria.spirals.repairnator.process.inspectors.Metrics) HunkHeader(org.eclipse.jgit.patch.HunkHeader) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) FileHeader(org.eclipse.jgit.patch.FileHeader) DiffEntry(org.eclipse.jgit.diff.DiffEntry) HashSet(java.util.HashSet)

Aggregations

Metrics (fr.inria.spirals.repairnator.process.inspectors.Metrics)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 FileHeader (org.eclipse.jgit.patch.FileHeader)1 HunkHeader (org.eclipse.jgit.patch.HunkHeader)1 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)1