Search in sources :

Example 56 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project zeppelin by apache.

the class GitNotebookRepo method get.

/**
 * the idea is to:
 * 1. stash current changes
 * 2. remember head commit and checkout to the desired revision
 * 3. get note and checkout back to the head
 * 4. apply stash on top and remove it
 */
@Override
public synchronized Note get(String noteId, String notePath, String revId, AuthenticationInfo subject) throws IOException {
    Note note = null;
    RevCommit stash = null;
    String noteFileName = buildNoteFileName(noteId, notePath);
    try {
        List<DiffEntry> gitDiff = git.diff().setPathFilter(PathFilter.create(noteFileName)).call();
        boolean modified = !gitDiff.isEmpty();
        if (modified) {
            // stash changes
            stash = git.stashCreate().call();
            Collection<RevCommit> stashes = git.stashList().call();
            LOGGER.debug("Created stash : {}, stash size : {}", stash, stashes.size());
        }
        ObjectId head = git.getRepository().resolve(Constants.HEAD);
        // checkout to target revision
        git.checkout().setStartPoint(revId).addPath(noteFileName).call();
        // get the note
        note = super.get(noteId, notePath, subject);
        // checkout back to head
        git.checkout().setStartPoint(head.getName()).addPath(noteFileName).call();
        if (modified && stash != null) {
            // unstash changes
            ObjectId applied = git.stashApply().setStashRef(stash.getName()).call();
            ObjectId dropped = git.stashDrop().setStashRef(0).call();
            Collection<RevCommit> stashes = git.stashList().call();
            LOGGER.debug("Stash applied as : {}, and dropped : {}, stash size: {}", applied, dropped, stashes.size());
        }
    } catch (GitAPIException e) {
        LOGGER.error("Failed to return note from revision \"{}\"", revId, e);
    }
    return note;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ObjectId(org.eclipse.jgit.lib.ObjectId) Note(org.apache.zeppelin.notebook.Note) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 57 with DiffEntry

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

the class JGitUtils method getFilesInCommit.

/**
 * Returns the list of files changed in a specified commit. If the
 * repository does not exist or is empty, an empty list is returned.
 *
 * @param repository
 * @param commit
 *            if null, HEAD is assumed.
 * @param calculateDiffStat
 *            if true, each PathChangeModel will have insertions/deletions
 * @return list of files changed in a commit
 */
public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit, boolean calculateDiffStat) {
    List<PathChangeModel> list = new ArrayList<PathChangeModel>();
    if (!hasCommits(repository)) {
        return list;
    }
    RevWalk rw = new RevWalk(repository);
    try {
        if (commit == null) {
            ObjectId object = getDefaultBranch(repository);
            commit = rw.parseCommit(object);
        }
        if (commit.getParentCount() == 0) {
            TreeWalk tw = new TreeWalk(repository);
            tw.reset();
            tw.setRecursive(true);
            tw.addTree(commit.getTree());
            while (tw.next()) {
                long size = 0;
                FilestoreModel filestoreItem = null;
                ObjectId objectId = tw.getObjectId(0);
                try {
                    if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
                        size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB);
                        if (isPossibleFilestoreItem(size)) {
                            filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId));
                        }
                    }
                } catch (Throwable t) {
                    error(t, null, "failed to retrieve blob size for " + tw.getPathString());
                }
                list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), filestoreItem, size, tw.getRawMode(0), objectId.getName(), commit.getId().getName(), ChangeType.ADD));
            }
            tw.close();
        } else {
            RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
            DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
            df.setRepository(repository);
            df.setDiffComparator(RawTextComparator.DEFAULT);
            df.setDetectRenames(true);
            List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
            for (DiffEntry diff : diffs) {
                // create the path change model
                PathChangeModel pcm = PathChangeModel.from(diff, commit.getName(), repository);
                if (calculateDiffStat) {
                    // update file diffstats
                    df.format(diff);
                    PathChangeModel pathStat = df.getDiffStat().getPath(pcm.path);
                    if (pathStat != null) {
                        pcm.insertions = pathStat.insertions;
                        pcm.deletions = pathStat.deletions;
                    }
                }
                list.add(pcm);
            }
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to determine files in commit!");
    } finally {
        rw.dispose();
    }
    return list;
}
Also used : PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) FilestoreModel(com.gitblit.models.FilestoreModel) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 58 with DiffEntry

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

the class JGitUtils method getFilesInRange.

/**
 * Returns the list of files changed in a specified commit. If the
 * repository does not exist or is empty, an empty list is returned.
 *
 * @param repository
 * @param startCommit
 *            earliest commit
 * @param endCommit
 *            most recent commit. if null, HEAD is assumed.
 * @return list of files changed in a commit range
 */
public static List<PathChangeModel> getFilesInRange(Repository repository, RevCommit startCommit, RevCommit endCommit) {
    List<PathChangeModel> list = new ArrayList<PathChangeModel>();
    if (!hasCommits(repository)) {
        return list;
    }
    try {
        DiffFormatter df = new DiffFormatter(null);
        df.setRepository(repository);
        df.setDiffComparator(RawTextComparator.DEFAULT);
        df.setDetectRenames(true);
        List<DiffEntry> diffEntries = df.scan(startCommit.getTree(), endCommit.getTree());
        for (DiffEntry diff : diffEntries) {
            PathChangeModel pcm = PathChangeModel.from(diff, endCommit.getName(), repository);
            list.add(pcm);
        }
        Collections.sort(list);
    } catch (Throwable t) {
        error(t, repository, "{0} failed to determine files in range {1}..{2}!", startCommit, endCommit);
    }
    return list;
}
Also used : PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) ArrayList(java.util.ArrayList) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 59 with DiffEntry

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

Example 60 with DiffEntry

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

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