Search in sources :

Example 6 with RevFilter

use of org.eclipse.jgit.revwalk.filter.RevFilter in project gitblit by gitblit.

the class JGitUtils method searchRevlogs.

/**
 * Search the commit history for a case-insensitive match to the value.
 * Search results require a specified SearchType of AUTHOR, COMMITTER, or
 * COMMIT. Results may be paginated using offset and maxCount. If the
 * repository does not exist or is empty, an empty list is returned.
 *
 * @param repository
 * @param objectId
 *            if unspecified, HEAD is assumed.
 * @param value
 * @param type
 *            AUTHOR, COMMITTER, COMMIT
 * @param offset
 * @param maxCount
 *            if < 0, all matches are returned
 * @return matching list of commits
 */
public static List<RevCommit> searchRevlogs(Repository repository, String objectId, String value, final com.gitblit.Constants.SearchType type, int offset, int maxCount) {
    List<RevCommit> list = new ArrayList<RevCommit>();
    if (StringUtils.isEmpty(value)) {
        return list;
    }
    if (maxCount == 0) {
        return list;
    }
    if (!hasCommits(repository)) {
        return list;
    }
    final String lcValue = value.toLowerCase();
    try {
        // resolve branch
        ObjectId branchObject;
        if (StringUtils.isEmpty(objectId)) {
            branchObject = getDefaultBranch(repository);
        } else {
            branchObject = repository.resolve(objectId);
        }
        RevWalk rw = new RevWalk(repository);
        rw.setRevFilter(new RevFilter() {

            @Override
            public RevFilter clone() {
                // This is part of JGit design and unrelated to Cloneable.
                return this;
            }

            @Override
            public boolean include(RevWalk walker, RevCommit commit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
                boolean include = false;
                switch(type) {
                    case AUTHOR:
                        include = (commit.getAuthorIdent().getName().toLowerCase().indexOf(lcValue) > -1) || (commit.getAuthorIdent().getEmailAddress().toLowerCase().indexOf(lcValue) > -1);
                        break;
                    case COMMITTER:
                        include = (commit.getCommitterIdent().getName().toLowerCase().indexOf(lcValue) > -1) || (commit.getCommitterIdent().getEmailAddress().toLowerCase().indexOf(lcValue) > -1);
                        break;
                    case COMMIT:
                        include = commit.getFullMessage().toLowerCase().indexOf(lcValue) > -1;
                        break;
                }
                return include;
            }
        });
        rw.markStart(rw.parseCommit(branchObject));
        Iterable<RevCommit> revlog = rw;
        if (offset > 0) {
            int count = 0;
            for (RevCommit rev : revlog) {
                count++;
                if (count > offset) {
                    list.add(rev);
                    if (maxCount > 0 && list.size() == maxCount) {
                        break;
                    }
                }
            }
        } else {
            for (RevCommit rev : revlog) {
                list.add(rev);
                if (maxCount > 0 && list.size() == maxCount) {
                    break;
                }
            }
        }
        rw.dispose();
    } catch (Throwable t) {
        error(t, repository, "{0} failed to {1} search revlogs for {2}", type.name(), value);
    }
    return list;
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) CommitTimeRevFilter(org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter) RevFilter(org.eclipse.jgit.revwalk.filter.RevFilter) StopWalkException(org.eclipse.jgit.errors.StopWalkException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 RevFilter (org.eclipse.jgit.revwalk.filter.RevFilter)6 IOException (java.io.IOException)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5 ArrayList (java.util.ArrayList)3 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)3 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)3 StopWalkException (org.eclipse.jgit.errors.StopWalkException)2 Ref (org.eclipse.jgit.lib.Ref)2 CommitTimeRevFilter (org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter)2 RefNames.isConfigRef (com.google.gerrit.entities.RefNames.isConfigRef)1 Branch (com.google.gerrit.reviewdb.client.Branch)1 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)1 MagicBranch (com.google.gerrit.server.util.MagicBranch)1 CmdLineParser (com.google.gerrit.util.cli.CmdLineParser)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 PatchIdDiffFormatter (org.eclipse.jgit.diff.PatchIdDiffFormatter)1