Example usage for org.eclipse.jgit.revwalk RevWalk sort

List of usage examples for org.eclipse.jgit.revwalk RevWalk sort

Introduction

In this page you can find the example usage for org.eclipse.jgit.revwalk RevWalk sort.

Prototype

public void sort(RevSort s, boolean use) 

Source Link

Document

Add or remove a sorting strategy for the returned commits.

Usage

From source file:com.google.gerrit.server.git.TagSet.java

License:Apache License

void prepare(TagMatcher m) {
    @SuppressWarnings("resource")
    RevWalk rw = null;/* w w  w  . j  a v  a  2 s  .c  o  m*/
    try {
        for (Ref currentRef : m.include) {
            if (currentRef.isSymbolic()) {
                continue;
            }
            if (currentRef.getObjectId() == null) {
                continue;
            }

            CachedRef savedRef = refs.get(currentRef.getName());
            if (savedRef == null) {
                // If the reference isn't known to the set, return null
                // and force the caller to rebuild the set in a new copy.
                m.newRefs.add(currentRef);
                continue;
            }

            // The reference has not been moved. It can be used as-is.
            ObjectId savedObjectId = savedRef.get();
            if (currentRef.getObjectId().equals(savedObjectId)) {
                m.mask.set(savedRef.flag);
                continue;
            }

            // Check on-the-fly to see if the branch still reaches the tag.
            // This is very likely for a branch that fast-forwarded.
            try {
                if (rw == null) {
                    rw = new RevWalk(m.db);
                    rw.setRetainBody(false);
                }

                RevCommit savedCommit = rw.parseCommit(savedObjectId);
                RevCommit currentCommit = rw.parseCommit(currentRef.getObjectId());
                if (rw.isMergedInto(savedCommit, currentCommit)) {
                    // Fast-forward. Safely update the reference in-place.
                    savedRef.compareAndSet(savedObjectId, currentRef.getObjectId());
                    m.mask.set(savedRef.flag);
                    continue;
                }

                // The branch rewound. Walk the list of commits removed from
                // the reference. If any matches to a tag, this has to be removed.
                boolean err = false;
                rw.reset();
                rw.markStart(savedCommit);
                rw.markUninteresting(currentCommit);
                rw.sort(RevSort.TOPO, true);
                RevCommit c;
                while ((c = rw.next()) != null) {
                    Tag tag = tags.get(c);
                    if (tag != null && tag.refFlags.get(savedRef.flag)) {
                        m.lostRefs.add(new TagMatcher.LostRef(tag, savedRef.flag));
                        err = true;
                    }
                }
                if (!err) {
                    // All of the tags are still reachable. Update in-place.
                    savedRef.compareAndSet(savedObjectId, currentRef.getObjectId());
                    m.mask.set(savedRef.flag);
                }

            } catch (IOException err) {
                // Defer a cache update until later. No conclusion can be made
                // based on an exception reading from the repository storage.
                log.warn("Error checking tags of " + projectName, err);
            }
        }
    } finally {
        if (rw != null) {
            rw.close();
        }
    }
}

From source file:eu.mihosoft.vrl.io.VersionedFile.java

License:Open Source License

/**
 * Returns a list containing commit objects of all versions. This method can
 * be used to show the version messages, e.g., for creating a ui that does
 * allow the selection of the version that shall be checked out.
 *
 * @return a list containing commit objects of all versions
 * @throws IOException/*from   ww  w  . jav a 2s. c  o  m*/
 * @throws IllegalStateException if this file is currently not open
 */
@Override
public ArrayList<RevCommit> getVersions() throws IOException {

    // use cached results if possible
    if (commits != null) {
        return commits;
    }

    // file has to be opened
    if (!isOpened()) {
        throw new IllegalStateException("File\"" + getFile().getPath() + "\" not opened!");
    }

    RevWalk walk = null;

    Git git = null;

    try {

        // open the git repository
        git = Git.open(tmpFolder);
        walk = new RevWalk(git.getRepository());

        // retrieve the object id of the current HEAD version
        // (latest/youngest version)
        ObjectId headId = git.getRepository().resolve(Constants.HEAD);

        // tell the walk to start from HEAD
        walk.markStart(walk.parseCommit(headId));

        // change sorting order
        walk.sort(RevSort.TOPO, true);
        walk.sort(RevSort.REVERSE, true);

        commits = new ArrayList<RevCommit>();

        // walk through all versions and add them to the list
        for (RevCommit commit : walk) {
            commits.add(commit);
        }

        closeGit(git);

    } catch (IOException ex) {
        throw new IOException("Git exception", ex);
    } finally {
        closeGit(git);
        // we are responsible for disposing the walk object
        if (walk != null) {
            walk.dispose();
        }
    }

    return commits;
}

From source file:org.apache.maven.scm.provider.git.jgit.command.JGitUtils.java

License:Apache License

/**
 * Get a list of commits between two revisions.
 *
 * @param repo     the repository to work on
 * @param sortings sorting/*from  www. ja v a2  s .c o  m*/
 * @param fromRev  start revision
 * @param toRev    if null, falls back to head
 * @param fromDate from which date on
 * @param toDate   until which date
 * @param maxLines max number of lines
 * @return a list of commits, might be empty, but never <code>null</code>
 * @throws IOException
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 */
public static List<RevCommit> getRevCommits(Repository repo, RevSort[] sortings, String fromRev, String toRev,
        final Date fromDate, final Date toDate, int maxLines)
        throws IOException, MissingObjectException, IncorrectObjectTypeException {

    List<RevCommit> revs = new ArrayList<RevCommit>();
    RevWalk walk = new RevWalk(repo);

    ObjectId fromRevId = fromRev != null ? repo.resolve(fromRev) : null;
    ObjectId toRevId = toRev != null ? repo.resolve(toRev) : null;

    if (sortings == null || sortings.length == 0) {
        sortings = new RevSort[] { RevSort.TOPO, RevSort.COMMIT_TIME_DESC };
    }

    for (final RevSort s : sortings) {
        walk.sort(s, true);
    }

    if (fromDate != null && toDate != null) {
        //walk.setRevFilter( CommitTimeRevFilter.between( fromDate, toDate ) );
        walk.setRevFilter(new RevFilter() {
            @Override
            public boolean include(RevWalk walker, RevCommit cmit) throws StopWalkException,
                    MissingObjectException, IncorrectObjectTypeException, IOException {
                int cmtTime = cmit.getCommitTime();

                return (cmtTime >= (fromDate.getTime() / 1000)) && (cmtTime <= (toDate.getTime() / 1000));
            }

            @Override
            public RevFilter clone() {
                return this;
            }
        });
    } else {
        if (fromDate != null) {
            walk.setRevFilter(CommitTimeRevFilter.after(fromDate));
        }
        if (toDate != null) {
            walk.setRevFilter(CommitTimeRevFilter.before(toDate));
        }
    }

    if (fromRevId != null) {
        RevCommit c = walk.parseCommit(fromRevId);
        c.add(RevFlag.UNINTERESTING);
        RevCommit real = walk.parseCommit(c);
        walk.markUninteresting(real);
    }

    if (toRevId != null) {
        RevCommit c = walk.parseCommit(toRevId);
        c.remove(RevFlag.UNINTERESTING);
        RevCommit real = walk.parseCommit(c);
        walk.markStart(real);
    } else {
        final ObjectId head = repo.resolve(Constants.HEAD);
        if (head == null) {
            throw new RuntimeException("Cannot resolve " + Constants.HEAD);
        }
        RevCommit real = walk.parseCommit(head);
        walk.markStart(real);
    }

    int n = 0;
    for (final RevCommit c : walk) {
        n++;
        if (maxLines != -1 && n > maxLines) {
            break;
        }

        revs.add(c);
    }
    return revs;
}

From source file:org.eclipse.egit.internal.relengtools.ShowInfoHandler.java

License:Open Source License

public static RevCommit getLatestCommitFor(RepositoryMapping mapping, Repository repo, IProject project)
        throws Exception {
    final RevWalk walk = new RevWalk(repo);
    walk.reset();/*from  ww w  .java2 s.c om*/
    walk.sort(RevSort.TOPO, true);
    walk.sort(RevSort.COMMIT_TIME_DESC, true);
    walk.setTreeFilter(
            AndTreeFilter.create(TreeFilter.ANY_DIFF, PathFilter.create(mapping.getRepoRelativePath(project))));
    final ObjectId start = repo.resolve(Constants.HEAD);
    walk.markStart(walk.parseCommit(start));

    // I should only be able to see commits that contain files
    // where project is a path prefix
    final RevCommit commit = walk.next();
    return commit;
}

From source file:org.eclipse.egit.ui.internal.actions.TagAction.java

License:Open Source License

private RevWalk getRevCommits() {
    RevWalk revWalk = new RevWalk(repo);
    revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
    revWalk.sort(RevSort.BOUNDARY, true);

    try {/*www.j a  va  2s. com*/
        AnyObjectId headId = repo.resolve(Constants.HEAD);
        if (headId != null)
            revWalk.markStart(revWalk.parseCommit(headId));
    } catch (IOException e) {
        ErrorDialog.openError(getShell(), UIText.TagAction_errorDuringTagging,
                UIText.TagAction_errorWhileGettingRevCommits,
                new Status(IStatus.ERROR, Activator.getPluginId(), e.getMessage(), e));
    }

    return revWalk;
}

From source file:org.eclipse.egit.ui.internal.dialogs.CreateTagDialog.java

License:Open Source License

private void getRevCommits(Collection<RevCommit> commits) {
    RevWalk revWalk = new RevWalk(repo);
    revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
    revWalk.sort(RevSort.BOUNDARY, true);

    try {/*from  w w w. ja v a 2s  .  c  o m*/
        AnyObjectId headId = repo.resolve(Constants.HEAD);
        if (headId != null)
            revWalk.markStart(revWalk.parseCommit(headId));
    } catch (IOException e) {
        Activator.logError(UIText.TagAction_errorWhileGettingRevCommits, e);
        setErrorMessage(UIText.TagAction_errorWhileGettingRevCommits);
    }
    // do the walk to get the commits
    for (RevCommit commit : revWalk)
        commits.add(commit);
}

From source file:org.jboss.as.controller.persistence.AbstractGitPersistenceResourceTestCase.java

License:Apache License

private List<String> listTags(Git git) throws IOException, GitAPIException {
    List<String> tags = new ArrayList<>();
    for (Ref tag : git.tagList().call()) {
        RevWalk revWalk = new RevWalk(git.getRepository());
        revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
        try {/*from www .  ja v a  2  s . c  o m*/
            RevTag annotatedTag = revWalk.parseTag(tag.getObjectId());
            tags.add(annotatedTag.getTagName() + " : " + annotatedTag.getFullMessage());
        } catch (IncorrectObjectTypeException ex) {
            tags.add(tag.getName().substring("refs/tags/".length()));
        }
    }
    return tags;
}

From source file:org.jboss.as.server.controller.git.GitConfigurationPersister.java

License:Apache License

@Override
public SnapshotInfo listSnapshots() {
    try (Git git = gitRepository.getGit()) {
        final List<String> snapshots = new ArrayList<>();
        for (Ref ref : git.tagList().call()) {
            RevWalk revWalk = new RevWalk(git.getRepository());
            revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
            try {
                RevTag annotatedTag = revWalk.parseTag(ref.getObjectId());
                snapshots.add(annotatedTag.getTagName() + " : " + annotatedTag.getFullMessage());
            } catch (IncorrectObjectTypeException ex) {
                snapshots.add(ref.getName());
            }/*from   w w  w  .  j  a  v  a 2s  .c om*/
            snapshots.add(ref.getName());
        }
        return new SnapshotInfo() {
            @Override
            public String getSnapshotDirectory() {
                return "";
            }

            @Override
            public List<String> names() {
                return snapshots;
            }
        };
    } catch (GitAPIException ex) {
        MGMT_OP_LOGGER.failedToListConfigurationSnapshot(ex, mainFile.getName());
    } catch (IOException ex) {
        MGMT_OP_LOGGER.failedToListConfigurationSnapshot(ex, mainFile.getName());
    }
    return NULL_SNAPSHOT_INFO;
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.SubdirectoryClone.java

License:Apache License

private RevWalk createRevWalk(final Repository repository, final ObjectReader reader)
        throws MissingObjectException, IncorrectObjectTypeException, IOException {
    final RevWalk revWalk = new RevWalk(reader);
    final List<RevCommit> branchTips = getBranchCommits(repository, revWalk);
    // So that we traverse all branch histories at once
    revWalk.markStart(branchTips);//from   ww w.  j a  v  a2 s.c o m

    // Gets parents before children
    revWalk.sort(RevSort.TOPO, true);
    revWalk.sort(RevSort.REVERSE, true);

    revWalk.setRevFilter(RevFilter.ALL);
    revWalk.setTreeFilter(TreeFilter.ALL);

    return revWalk;
}