Search in sources :

Example 1 with RawTextComparator

use of org.eclipse.jgit.diff.RawTextComparator in project gitblit by gitblit.

the class DiffUtils method getCommitPatch.

/**
	 * Returns the diff between the two commits for the specified file or folder
	 * formatted as a patch.
	 *
	 * @param repository
	 * @param baseCommit
	 *            if base commit is unspecified, the patch is generated against
	 *            the primary parent of the specified commit.
	 * @param commit
	 * @param path
	 *            if path is specified, the patch is generated only for the
	 *            specified file or folder. if unspecified, the patch is
	 *            generated for the entire diff between the two commits.
	 * @return patch as a string
	 */
public static String getCommitPatch(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
    String diff = null;
    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        RawTextComparator cmp = RawTextComparator.DEFAULT;
        PatchFormatter df = new PatchFormatter(os);
        df.setRepository(repository);
        df.setDiffComparator(cmp);
        df.setDetectRenames(true);
        RevTree commitTree = commit.getTree();
        RevTree baseTree;
        if (baseCommit == null) {
            if (commit.getParentCount() > 0) {
                final RevWalk rw = new RevWalk(repository);
                RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
                baseTree = parent.getTree();
            } else {
                // FIXME initial commit. no parent?!
                baseTree = commitTree;
            }
        } else {
            baseTree = baseCommit.getTree();
        }
        List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
        if (path != null && path.length() > 0) {
            for (DiffEntry diffEntry : diffEntries) {
                if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
                    df.format(diffEntry);
                    break;
                }
            }
        } else {
            df.format(diffEntries);
        }
        diff = df.getPatch(commit);
        df.flush();
    } catch (Throwable t) {
        LOGGER.error("failed to generate commit diff!", t);
    }
    return diff;
}
Also used : RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 2 with RawTextComparator

use of org.eclipse.jgit.diff.RawTextComparator in project gitblit by gitblit.

the class DiffUtils method getDiffStat.

/**
	 * Returns the diffstat between the two commits for the specified file or folder.
	 *
	 * @param repository
	 * @param baseCommit
	 *            if base commit is unspecified, the diffstat is generated against
	 *            the primary parent of the specified commit.
	 * @param commit
	 * @param path
	 *            if path is specified, the diffstat is generated only for the
	 *            specified file or folder. if unspecified, the diffstat is
	 *            generated for the entire diff between the two commits.
	 * @return patch as a string
	 */
public static DiffStat getDiffStat(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
    DiffStat stat = null;
    try {
        RawTextComparator cmp = RawTextComparator.DEFAULT;
        DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
        df.setRepository(repository);
        df.setDiffComparator(cmp);
        df.setDetectRenames(true);
        RevTree commitTree = commit.getTree();
        RevTree baseTree;
        if (baseCommit == null) {
            if (commit.getParentCount() > 0) {
                final RevWalk rw = new RevWalk(repository);
                RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
                baseTree = parent.getTree();
            } else {
                // FIXME initial commit. no parent?!
                baseTree = commitTree;
            }
        } else {
            baseTree = baseCommit.getTree();
        }
        List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
        if (path != null && path.length() > 0) {
            for (DiffEntry diffEntry : diffEntries) {
                if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
                    df.format(diffEntry);
                    break;
                }
            }
        } else {
            df.format(diffEntries);
        }
        stat = df.getDiffStat();
        df.flush();
    } catch (Throwable t) {
        LOGGER.error("failed to generate commit diff!", t);
    }
    return stat;
}
Also used : RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 3 with RawTextComparator

use of org.eclipse.jgit.diff.RawTextComparator in project gerrit by GerritCodeReview.

the class PatchListLoader method readPatchList.

public PatchList readPatchList(Repository repo, RevWalk rw, ObjectInserter ins) throws IOException, PatchListNotAvailableException {
    ObjectReader reader = rw.getObjectReader();
    checkArgument(reader.getCreatedFromInserter() == ins);
    RawTextComparator cmp = comparatorFor(key.getWhitespace());
    try (DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
        RevCommit b = rw.parseCommit(key.getNewId());
        RevObject a = aFor(key, repo, rw, ins, b);
        if (a == null) {
            // TODO(sop) Remove this case.
            // This is an octopus merge commit which should be compared against the
            // auto-merge. However since we don't support computing the auto-merge
            // for octopus merge commits, we fall back to diffing against the first
            // parent, even though this wasn't what was requested.
            //
            ComparisonType comparisonType = ComparisonType.againstParent(1);
            PatchListEntry[] entries = new PatchListEntry[2];
            entries[0] = newCommitMessage(cmp, reader, null, b);
            entries[1] = newMergeList(cmp, reader, null, b, comparisonType);
            return new PatchList(a, b, true, comparisonType, entries);
        }
        ComparisonType comparisonType = getComparisonType(a, b);
        RevCommit aCommit = a instanceof RevCommit ? (RevCommit) a : null;
        RevTree aTree = rw.parseTree(a);
        RevTree bTree = b.getTree();
        df.setReader(reader, repo.getConfig());
        df.setDiffComparator(cmp);
        df.setDetectRenames(true);
        List<DiffEntry> diffEntries = df.scan(aTree, bTree);
        Set<String> paths = null;
        if (key.getOldId() != null && b.getParentCount() == 1) {
            PatchListKey newKey = PatchListKey.againstDefaultBase(key.getNewId(), key.getWhitespace());
            PatchListKey oldKey = PatchListKey.againstDefaultBase(key.getOldId(), key.getWhitespace());
            paths = Stream.concat(patchListCache.get(newKey, project).getPatches().stream(), patchListCache.get(oldKey, project).getPatches().stream()).map(PatchListEntry::getNewName).collect(toSet());
        }
        int cnt = diffEntries.size();
        List<PatchListEntry> entries = new ArrayList<>();
        entries.add(newCommitMessage(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b));
        boolean isMerge = b.getParentCount() > 1;
        if (isMerge) {
            entries.add(newMergeList(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b, comparisonType));
        }
        for (int i = 0; i < cnt; i++) {
            DiffEntry e = diffEntries.get(i);
            if (paths == null || paths.contains(e.getNewPath()) || paths.contains(e.getOldPath())) {
                FileHeader fh = toFileHeader(key, df, e);
                long oldSize = getFileSize(reader, e.getOldMode(), e.getOldPath(), aTree);
                long newSize = getFileSize(reader, e.getNewMode(), e.getNewPath(), bTree);
                entries.add(newEntry(aTree, fh, newSize, newSize - oldSize));
            }
        }
        return new PatchList(a, b, isMerge, comparisonType, entries.toArray(new PatchListEntry[entries.size()]));
    }
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ArrayList(java.util.ArrayList) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) FileHeader(org.eclipse.jgit.patch.FileHeader) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

DiffEntry (org.eclipse.jgit.diff.DiffEntry)3 RawTextComparator (org.eclipse.jgit.diff.RawTextComparator)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevTree (org.eclipse.jgit.revwalk.RevTree)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 FileHeader (org.eclipse.jgit.patch.FileHeader)1 RevObject (org.eclipse.jgit.revwalk.RevObject)1