Search in sources :

Example 16 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project che by eclipse.

the class JGitConnection method showFileContent.

@Override
public ShowFileContentResponse showFileContent(String file, String version) throws GitException {
    String content;
    ObjectId revision;
    try {
        revision = getRepository().resolve(version);
        try (RevWalk revWalk = new RevWalk(getRepository())) {
            RevCommit revCommit = revWalk.parseCommit(revision);
            RevTree tree = revCommit.getTree();
            try (TreeWalk treeWalk = new TreeWalk(getRepository())) {
                treeWalk.addTree(tree);
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create(file));
                if (!treeWalk.next()) {
                    throw new GitException("fatal: Path '" + file + "' does not exist in '" + version + "'" + lineSeparator());
                }
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                content = new String(loader.getBytes());
            }
        }
    } catch (IOException exception) {
        throw new GitException(exception.getMessage());
    }
    return newDto(ShowFileContentResponse.class).withContent(content);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) GitException(org.eclipse.che.api.git.exception.GitException) ShowFileContentResponse(org.eclipse.che.api.git.shared.ShowFileContentResponse) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 17 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project che by eclipse.

the class JGitDiffPage method commitToIndex.

/**
     * Show changes between specified revision and index. If
     * <code>commitId == null</code> then view changes between HEAD and index.
     *
     * @param commitId
     *            id of commit, pass <code>null</code> is the same as pass HEAD
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
    if (commitId == null) {
        commitId = Constants.HEAD;
    }
    ObjectId commitA = repository.resolve(commitId);
    if (commitA == null) {
        throw new IllegalArgumentException("Invalid commit id " + commitId);
    }
    RevTree treeA;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        treeA = revWalkA.parseTree(commitA);
    }
    DirCache dirCache = null;
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        dirCache = repository.lockDirCache();
        CanonicalTreeParser iterA = new CanonicalTreeParser();
        iterA.reset(reader, treeA);
        DirCacheIterator iterB = new DirCacheIterator(dirCache);
        if (!params.isNoRenames()) {
            // Use embedded RenameDetector it works well with index and
            // revision history.
            formatter.setDetectRenames(true);
            int renameLimit = params.getRenameLimit();
            if (renameLimit > 0) {
                formatter.getRenameDetector().setRenameLimit(renameLimit);
            }
        }
        diff = formatter.scan(iterA, iterB);
    } finally {
        if (dirCache != null) {
            dirCache.unlock();
        }
    }
    return diff;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 18 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project gitblit by gitblit.

the class JGitUtils method getNotesOnCommit.

/**
	 * Returns the list of notes entered about the commit from the refs/notes
	 * namespace. If the repository does not exist or is empty, an empty list is
	 * returned.
	 *
	 * @param repository
	 * @param commit
	 * @return list of notes
	 */
public static List<GitNote> getNotesOnCommit(Repository repository, RevCommit commit) {
    List<GitNote> list = new ArrayList<GitNote>();
    if (!hasCommits(repository)) {
        return list;
    }
    List<RefModel> noteBranches = getNoteBranches(repository, true, -1);
    for (RefModel notesRef : noteBranches) {
        RevTree notesTree = JGitUtils.getCommit(repository, notesRef.getName()).getTree();
        // flat notes list
        String notePath = commit.getName();
        String text = getStringContent(repository, notesTree, notePath);
        if (!StringUtils.isEmpty(text)) {
            List<RevCommit> history = getRevLog(repository, notesRef.getName(), notePath, 0, -1);
            RefModel noteRef = new RefModel(notesRef.displayName, null, history.get(history.size() - 1));
            GitNote gitNote = new GitNote(noteRef, text);
            list.add(gitNote);
            continue;
        }
        // folder structure
        StringBuilder sb = new StringBuilder(commit.getName());
        sb.insert(2, '/');
        notePath = sb.toString();
        text = getStringContent(repository, notesTree, notePath);
        if (!StringUtils.isEmpty(text)) {
            List<RevCommit> history = getRevLog(repository, notesRef.getName(), notePath, 0, -1);
            RefModel noteRef = new RefModel(notesRef.displayName, null, history.get(history.size() - 1));
            GitNote gitNote = new GitNote(noteRef, text);
            list.add(gitNote);
        }
    }
    return list;
}
Also used : RefModel(com.gitblit.models.RefModel) ArrayList(java.util.ArrayList) GitNote(com.gitblit.models.GitNote) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with RevTree

use of org.eclipse.jgit.revwalk.RevTree 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 20 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project gitblit by gitblit.

the class DiffUtils method getDiff.

/**
	 * Returns the diff between two commits for the specified file.
	 *
	 * @param repository
	 * @param baseCommit
	 *            if base commit is null the diff is to the primary parent of
	 *            the commit.
	 * @param commit
	 * @param path
	 *            if the path is specified, the diff is restricted to that file
	 *            or folder. if unspecified, the diff is for the entire commit.
	 * @param comparator
	 * @param outputType
	 * @param handler
	 *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
	 *            May be {@code null}, resulting in the default behavior.
	 * @param tabLength
	 * @return the diff
	 */
public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path, DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler, int tabLength) {
    DiffStat stat = null;
    String diff = null;
    try {
        ByteArrayOutputStream os = null;
        DiffFormatter df;
        switch(outputType) {
            case HTML:
                df = new GitBlitDiffFormatter(commit.getName(), repository, path, handler, tabLength);
                break;
            case PLAIN:
            default:
                os = new ByteArrayOutputStream();
                df = new DiffFormatter(os);
                break;
        }
        df.setRepository(repository);
        df.setDiffComparator((comparator == null ? DiffComparator.SHOW_WHITESPACE : comparator).textComparator);
        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());
                rw.dispose();
                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);
        }
        df.flush();
        if (df instanceof GitBlitDiffFormatter) {
            // workaround for complex private methods in DiffFormatter
            diff = ((GitBlitDiffFormatter) df).getHtml();
            stat = ((GitBlitDiffFormatter) df).getDiffStat();
        } else {
            diff = os.toString();
        }
    } catch (Throwable t) {
        LOGGER.error("failed to generate commit diff!", t);
    }
    return new DiffOutput(outputType, diff, stat);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

RevTree (org.eclipse.jgit.revwalk.RevTree)34 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 ObjectId (org.eclipse.jgit.lib.ObjectId)16 RevCommit (org.eclipse.jgit.revwalk.RevCommit)16 ObjectReader (org.eclipse.jgit.lib.ObjectReader)10 DiffEntry (org.eclipse.jgit.diff.DiffEntry)8 IOException (java.io.IOException)7 Repository (org.eclipse.jgit.lib.Repository)7 RevObject (org.eclipse.jgit.revwalk.RevObject)6 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)5 File (java.io.File)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)4 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)4 ArrayList (java.util.ArrayList)3 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 RawTextComparator (org.eclipse.jgit.diff.RawTextComparator)3 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)3 Ref (org.eclipse.jgit.lib.Ref)3 RevBlob (org.eclipse.jgit.revwalk.RevBlob)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2