Search in sources :

Example 11 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project gitiles by GerritCodeReview.

the class CommitJsonData method toJsonData.

private static List<Diff> toJsonData(DiffList dl) {
    if (dl.entries == null) {
        return ImmutableList.of();
    }
    List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
    for (DiffEntry de : dl.entries) {
        Diff d = new Diff();
        d.type = de.getChangeType().name().toLowerCase();
        d.oldId = de.getOldId().name();
        d.oldMode = de.getOldMode().getBits();
        d.oldPath = de.getOldPath();
        d.newId = de.getNewId().name();
        d.newMode = de.getNewMode().getBits();
        d.newPath = de.getNewPath();
        switch(de.getChangeType()) {
            case COPY:
            case RENAME:
                d.score = de.getScore();
                break;
            case ADD:
            case DELETE:
            case MODIFY:
            default:
                break;
        }
        result.add(d);
    }
    return result;
}
Also used : DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 12 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project gitiles by GerritCodeReview.

the class CommitSoyData method toSoyData.

private static Object toSoyData(GitilesView view, DiffList dl) {
    if (dl.oldRevision == null) {
        return NullData.INSTANCE;
    }
    GitilesView.Builder diffUrl = GitilesView.diff().copyFrom(view).setOldRevision(dl.oldRevision).setRevision(dl.revision).setPathPart("");
    List<Object> result = Lists.newArrayListWithCapacity(dl.entries.size());
    for (DiffEntry e : dl.entries) {
        Map<String, Object> entry = Maps.newHashMapWithExpectedSize(5);
        ChangeType type = e.getChangeType();
        if (type != DELETE) {
            entry.put("path", e.getNewPath());
            entry.put("url", GitilesView.path().copyFrom(view).setRevision(dl.revision).setPathPart(e.getNewPath()).toUrl());
        } else {
            entry.put("path", e.getOldPath());
            entry.put("url", GitilesView.path().copyFrom(view).setRevision(dl.oldRevision).setPathPart(e.getOldPath()).toUrl());
        }
        entry.put("diffUrl", diffUrl.setAnchor("F" + result.size()).toUrl());
        entry.put("changeType", e.getChangeType().toString());
        if (type == COPY || type == RENAME) {
            entry.put("oldPath", e.getOldPath());
        }
        result.add(entry);
    }
    return result;
}
Also used : ChangeType(org.eclipse.jgit.diff.DiffEntry.ChangeType) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 13 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project egit by eclipse.

the class CreatePatchOperation method createDiffFormatter.

private DiffFormatter createDiffFormatter(final ByteArrayOutputStream outputStream, IProgressMonitor monitor) {
    DiffFormatter diffFmt = new DiffFormatter(outputStream) {

        private IProject project;

        @Override
        public void format(DiffEntry ent) throws IOException, CorruptObjectException, MissingObjectException {
            // changes
            if (DiffHeaderFormat.WORKSPACE == headerFormat) {
                IProject p = getProject(ent);
                if (p != null && !p.equals(project)) {
                    project = p;
                    getOutputStream().write(// $NON-NLS-1$ //$NON-NLS-2$
                    encodeASCII("#P " + project.getName() + "\n"));
                }
            }
            super.format(ent);
        }
    };
    diffFmt.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
    return diffFmt;
}
Also used : EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) IProject(org.eclipse.core.resources.IProject) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 14 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry 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)

Example 15 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project egit by eclipse.

the class CompareCoreUtils method getChangeDiffEntry.

/**
 * Get the {@link DiffEntry} corresponding to a change in file path. If the
 * file was renamed, the resulting {@link DiffEntry} will contain the old
 * path and blob ID. If the file was only added, null will be returned.
 *
 * @param repository
 * @param newPath
 *            path of the file in new commit
 * @param newCommit
 *            new commit
 * @param oldCommit
 *            old commit, e.g. parent commit of newCommit
 * @param objectReader
 *            reader for the repository
 * @return the diff entry corresponding to the change for path, or null if
 *         none could be found
 * @throws IOException
 */
public static DiffEntry getChangeDiffEntry(Repository repository, String newPath, RevCommit newCommit, RevCommit oldCommit, ObjectReader objectReader) throws IOException {
    try (TreeWalk walk = new TreeWalk(objectReader)) {
        walk.setRecursive(true);
        walk.addTree(oldCommit.getTree());
        walk.addTree(newCommit.getTree());
        List<DiffEntry> entries = DiffEntry.scan(walk);
        for (DiffEntry diff : entries) {
            if (diff.getChangeType() == ChangeType.MODIFY && newPath.equals(diff.getNewPath()))
                return diff;
        }
        if (entries.size() < 2)
            return null;
        RenameDetector detector = new RenameDetector(repository);
        detector.addAll(entries);
        List<DiffEntry> renames = detector.compute(walk.getObjectReader(), NullProgressMonitor.INSTANCE);
        for (DiffEntry diff : renames) {
            if (diff.getChangeType() == ChangeType.RENAME && newPath.equals(diff.getNewPath()))
                return diff;
        }
        return null;
    }
}
Also used : RenameDetector(org.eclipse.jgit.diff.RenameDetector) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

DiffEntry (org.eclipse.jgit.diff.DiffEntry)66 RevCommit (org.eclipse.jgit.revwalk.RevCommit)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)22 ObjectReader (org.eclipse.jgit.lib.ObjectReader)21 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)20 IOException (java.io.IOException)19 Git (org.eclipse.jgit.api.Git)18 ObjectId (org.eclipse.jgit.lib.ObjectId)15 ArrayList (java.util.ArrayList)14 File (java.io.File)13 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 RevTree (org.eclipse.jgit.revwalk.RevTree)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Path (java.nio.file.Path)8 Test (org.junit.Test)8 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)7 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)7 Ref (org.eclipse.jgit.lib.Ref)6 Repository (org.eclipse.jgit.lib.Repository)6