Example usage for org.eclipse.jgit.api LogCommand setMaxCount

List of usage examples for org.eclipse.jgit.api LogCommand setMaxCount

Introduction

In this page you can find the example usage for org.eclipse.jgit.api LogCommand setMaxCount.

Prototype

public LogCommand setMaxCount(int maxCount) 

Source Link

Document

Limit the number of commits to output.

Usage

From source file:com.microsoft.gittf.core.tasks.CheckinHeadCommitTask.java

License:Open Source License

/**
 * Builds the commit comment to use when checking in
 * //from ww  w. j  a v  a  2s.c  om
 * @param commitDelta
 * @return
 */
private String buildCommitComment(CommitDelta commitDelta) {
    /* In deep mode the task will use the ToCommit message */
    if (deep) {
        /*
         * If the meta data flag was set to true, then build the meta data
         * string
         */
        if (includeMetaDataInComment) {
            return buildCommitComment(commitDelta.getToCommit());
        }
        /* Otherwise just use the full message */
        else {
            return commitDelta.getToCommit().getFullMessage();
        }
    }

    /*
     * In Shallow mode use the log command to identify all the commit
     * included in the delta
     */
    try {
        /*
         * TODO: Need to replace this code to use better logic to figure out
         * the included commits topologically and not chronologically
         */

        LogCommand logCommand = new Git(repository).log();
        logCommand.addRange(commitDelta.getFromCommit().getId(), commitDelta.getToCommit().getId());
        logCommand.setMaxCount(OutputConstants.DEFAULT_MAXCOMMENTROLLUP + 1);
        Iterable<RevCommit> commits = logCommand.call();

        int commitCounter = 0;

        StringBuilder comment = new StringBuilder();

        comment.append(Messages.formatString("CheckinHeadCommitTask.ShallowCheckinRollupFormat", //$NON-NLS-1$
                ObjectIdUtil.abbreviate(repository, commitDelta.getToCommit().getId()),
                ObjectIdUtil.abbreviate(repository, commitDelta.getFromCommit().getId()))
                + OutputConstants.NEW_LINE);
        comment.append(OutputConstants.NEW_LINE);

        for (RevCommit commit : commits) {
            commitCounter++;

            if (commitCounter > OutputConstants.DEFAULT_MAXCOMMENTROLLUP) {
                comment.append(Messages.formatString(
                        "CheckinHeadCommitTask.ShallowCheckinCommentDisplayTruncatedFormat", //$NON-NLS-1$
                        OutputConstants.DEFAULT_MAXCOMMENTROLLUP,
                        ObjectIdUtil.abbreviate(repository, commit.getId()),
                        ObjectIdUtil.abbreviate(repository, commitDelta.getFromCommit().getId())));

                break;
            }

            comment.append(buildCommitComment(commit) + OutputConstants.NEW_LINE);
        }

        if (commitCounter == 1) {
            return buildCommitComment(commitDelta.getToCommit());
        }

        return comment.toString();
    } catch (Exception e) {
        // if we fail execute the log command we default to the destination
        // commit full message

        return buildCommitComment(commitDelta.getToCommit());
    }
}

From source file:net.vexelon.jdevlog.vcs.git.GitSource.java

License:Open Source License

@Override
public Iterator<?> getLastHistory(long maxEntries) throws SCMException {

    Iterable<RevCommit> log = null;

    try {/* w  w  w .  j  a v  a2  s.  c  o m*/
        Git git = new Git(repository);
        LogCommand cmd = git.log();

        cmd.setMaxCount((int) maxEntries);
        //         cmd.addPath(Constants.MASTER);

        try {
            cmd.add(repository.resolve(Constants.MASTER));
        } catch (Exception e) {
            throw new SCMException("Error getting log messages!", e);
        }

        log = cmd.call();
    } catch (NoHeadException e) {
        throw new SCMException("Error getting log messages!", e);
    }

    return log.iterator();
}

From source file:org.ajoberstar.gradle.git.tasks.GitLog.java

License:Apache License

/**
 * Gets the commit log and stores it in a project property as configured.
 *//*from  ww  w .j a  v  a 2 s .c  om*/
@TaskAction
public void reset() {
    final LogCommand cmd = getGit().log();
    Repository repo = getGit().getRepository();
    if (includes != null) {
        for (String include : includes) {
            try {
                ObjectId commit = repo.resolve(include);
                if (commit == null) {
                    throw new GradleException("No commit found for revision string: " + include);
                } else {
                    cmd.add(commit);
                }
            } catch (AmbiguousObjectException e) {
                throw new GradleException("Revision string is ambiguous: " + include, e);
            } catch (MissingObjectException e) {
                throw new GradleException("Commit could not be found in repository: " + include, e);
            } catch (IncorrectObjectTypeException e) {
                throw new GradleException("Revision string did not point to a commit: " + include, e);
            } catch (IOException e) {
                throw new GradleException("Problem resolving revision string: " + include, e);
            }
        }
    }
    if (excludes != null) {
        for (String exclude : excludes) {
            try {
                ObjectId commit = repo.resolve(exclude);
                if (commit == null) {
                    throw new GradleException("No commit found for revision string: " + exclude);
                } else {
                    cmd.add(commit);
                }
            } catch (AmbiguousObjectException e) {
                throw new GradleException("Revision string is ambiguous: " + exclude, e);
            } catch (MissingObjectException e) {
                throw new GradleException("Commit could not be found in repository: " + exclude, e);
            } catch (IncorrectObjectTypeException e) {
                throw new GradleException("Revision string did not point to a commit: " + exclude, e);
            } catch (IOException e) {
                throw new GradleException("Problem resolving revision string: " + exclude, e);
            }
        }
    }
    cmd.setSkip(skipCommits);
    cmd.setMaxCount(maxCommits);

    try {
        Iterable<RevCommit> commits = cmd.call();
        List<Commit> tempLog = new ArrayList<Commit>();
        for (RevCommit commit : commits) {
            tempLog.add(GitUtil.revCommitToCommit(commit));
        }
        this.log = Collections.unmodifiableList(tempLog);
    } catch (GitAPIException e) {
        throw new GradleException("Problem with log.", e);
    }
    //TODO add progress monitor to log progress to Gradle status bar
}

From source file:org.eclipse.orion.server.git.jobs.ListBranchesJob.java

License:Open Source License

@Override
protected IStatus performJob() {
    try {/*from www.ja v  a2s.c  o  m*/
        File gitDir = GitUtils.getGitDir(path);
        Repository db = new FileRepository(gitDir);
        Git git = new Git(db);
        List<Ref> branchRefs = git.branchList().call();
        List<Branch> branches = new ArrayList<Branch>(branchRefs.size());
        for (Ref ref : branchRefs) {
            branches.add(new Branch(cloneLocation, db, ref));
        }
        Collections.sort(branches, Branch.COMPARATOR);
        JSONObject result = new JSONObject();
        JSONArray children = new JSONArray();
        int firstBranch = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
        int lastBranch = pageSize > 0 ? firstBranch + pageSize - 1 : branches.size() - 1;
        lastBranch = lastBranch > branches.size() - 1 ? branches.size() - 1 : lastBranch;
        if (pageNo > 1 && baseLocation != null) {
            String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
        }
        if (lastBranch < branches.size() - 1) {
            String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
        }
        for (int i = firstBranch; i <= lastBranch; i++) {
            Branch branch = branches.get(i);
            if (commitsSize == 0) {
                children.put(branch.toJSON());
            } else {
                String branchName = branch.getName(true, false);
                ObjectId toObjectId = db.resolve(branchName);
                Ref toRefId = db.getRef(branchName);
                if (toObjectId == null) {
                    String msg = NLS.bind("No ref or commit found: {0}", branchName);
                    return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null);
                }
                toObjectId = getCommitObjectId(db, toObjectId);

                Log log = null;
                // single commit is requested and we already know it, no need for LogCommand 
                if (commitsSize == 1 && toObjectId instanceof RevCommit) {
                    log = new Log(cloneLocation, db, Collections.singleton((RevCommit) toObjectId), null, null,
                            toRefId);
                } else {
                    LogCommand lc = git.log();
                    // set the commit range
                    lc.add(toObjectId);
                    lc.setMaxCount(this.commitsSize);
                    Iterable<RevCommit> commits = lc.call();
                    log = new Log(cloneLocation, db, commits, null, null, toRefId);
                }
                log.setPaging(1, commitsSize);
                children.put(branch.toJSON(log.toJSON()));
            }
        }
        result.put(ProtocolConstants.KEY_CHILDREN, children);
        result.put(ProtocolConstants.KEY_TYPE, Branch.TYPE);
        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when listing branches for {0}", path);
        return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
    }
}

From source file:org.eclipse.orion.server.git.jobs.ListTagsJob.java

License:Open Source License

@Override
protected IStatus performJob() {
    try {/*  ww  w  .j ava  2 s  .co  m*/
        // list all tags
        File gitDir = GitUtils.getGitDir(path);
        Repository db = new FileRepository(gitDir);
        Git git = new Git(db);
        List<Ref> refs = git.tagList().call();
        JSONObject result = new JSONObject();
        List<Tag> tags = new ArrayList<Tag>();
        for (Ref ref : refs) {
            Tag tag = new Tag(cloneLocation, db, ref);
            tags.add(tag);
        }
        Collections.sort(tags, Tag.COMPARATOR);
        JSONArray children = new JSONArray();
        int firstTag = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
        int lastTag = pageSize > 0 ? firstTag + pageSize - 1 : tags.size() - 1;
        lastTag = lastTag > tags.size() - 1 ? tags.size() - 1 : lastTag;
        if (pageNo > 1 && baseLocation != null) {
            String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
        }
        if (lastTag < tags.size() - 1) {
            String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
        }
        for (int i = firstTag; i <= lastTag; i++) {
            Tag tag = tags.get(i);
            if (this.commitsSize == 0) {
                children.put(tag.toJSON());
            } else {
                // add info about commits if requested
                LogCommand lc = git.log();
                String toCommitName = tag.getRevCommitName();
                ObjectId toCommitId = db.resolve(toCommitName);
                Ref toCommitRef = db.getRef(toCommitName);
                toCommitId = getCommitObjectId(db, toCommitId);
                lc.add(toCommitId);
                lc.setMaxCount(this.commitsSize);
                Iterable<RevCommit> commits = lc.call();
                Log log = new Log(cloneLocation, db, commits, null, null, toCommitRef);
                log.setPaging(1, commitsSize);
                children.put(tag.toJSON(log.toJSON()));
            }
        }
        result.put(ProtocolConstants.KEY_CHILDREN, children);
        result.put(ProtocolConstants.KEY_TYPE, Tag.TYPE);
        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when listing tags for {0}", path);
        return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
    }
}

From source file:org.eclipse.orion.server.git.jobs.RemoteDetailsJob.java

License:Open Source License

@Override
protected IStatus performJob() {

    try {//from   ww w  .  j  a  va2s  . c  om
        File gitDir = GitUtils.getGitDir(path);
        Repository db = new FileRepository(gitDir);
        Git git = new Git(db);
        Set<String> configNames = db.getConfig().getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
        for (String configN : configNames) {
            if (configN.equals(configName)) {
                Remote remote = new Remote(cloneLocation, db, configN);
                JSONObject result = remote.toJSON();
                if (!result.has(ProtocolConstants.KEY_CHILDREN)) {
                    return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
                }
                JSONArray children = result.getJSONArray(ProtocolConstants.KEY_CHILDREN);
                if (children.length() == 0 || (commitsSize == 0 && pageSize < 0)) {
                    return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
                }

                int firstChild = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
                int lastChild = pageSize > 0 ? firstChild + pageSize - 1 : children.length() - 1;
                lastChild = lastChild > children.length() - 1 ? children.length() - 1 : lastChild;
                if (pageNo > 1 && baseLocation != null) {
                    String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
                    if (commitsSize > 0) {
                        prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
                    }
                    result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
                }
                if (lastChild < children.length() - 1) {
                    String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
                    if (commitsSize > 0) {
                        next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
                    }
                    result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
                }

                JSONArray newChildren = new JSONArray();
                for (int i = firstChild; i <= lastChild; i++) {
                    JSONObject branch = children.getJSONObject(i);
                    if (commitsSize == 0) {
                        newChildren.put(branch);
                    } else {
                        LogCommand lc = git.log();
                        String branchName = branch.getString(ProtocolConstants.KEY_ID);
                        ObjectId toObjectId = db.resolve(branchName);
                        Ref toRefId = db.getRef(branchName);
                        if (toObjectId == null) {
                            String msg = NLS.bind("No ref or commit found: {0}", branchName);
                            return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null);
                        }
                        toObjectId = getCommitObjectId(db, toObjectId);

                        // set the commit range
                        lc.add(toObjectId);
                        lc.setMaxCount(this.commitsSize);
                        Iterable<RevCommit> commits = lc.call();
                        Log log = new Log(cloneLocation, db, commits, null, null, toRefId);
                        log.setPaging(1, commitsSize);
                        branch.put(GitConstants.KEY_TAG_COMMIT, log.toJSON());
                        newChildren.put(branch);
                    }
                }

                result.put(ProtocolConstants.KEY_CHILDREN, newChildren);

                return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
            }
        }
        String msg = NLS.bind("Couldn't find remote : {0}", configName);
        return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null);
    } catch (Exception e) {
        String msg = NLS.bind("Couldn't get remote details : {0}", configName);
        return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
    }

}

From source file:org.eclipse.osee.ote.version.git.GitVersions.java

License:Open Source License

public Map<File, RevCommit> getLastCommits() {
    Map<File, RevCommit> commits = new HashMap<File, RevCommit>();
    Map<File, List<File>> gitToFiles = new HashMap<File, List<File>>();
    for (File file : files) {
        if (!file.exists()) {
            continue;
        }/*  w  w  w . j a  va2s  . c o  m*/
        File gitFolder = findGitDirUp(file);
        if (gitFolder == null) {
            continue;
        }
        List<File> gitfiles = gitToFiles.get(gitFolder);
        if (gitfiles == null) {
            gitfiles = new ArrayList<File>();
            gitToFiles.put(gitFolder, gitfiles);
        }
        gitfiles.add(file);
    }
    for (Entry<File, List<File>> entry : gitToFiles.entrySet()) {
        try {
            Repository repository = buildRepository(entry.getKey());
            Git git = new Git(repository);

            for (File gitfile : entry.getValue()) {
                LogCommand log = git.log();
                log.setMaxCount(1);
                String pathFilter = getPathFilterFromFullPathAndGitFolder(gitfile, entry.getKey());
                log.addPath(pathFilter);
                Iterable<RevCommit> iterable = log.call();
                Iterator<RevCommit> it = iterable.iterator();
                if (it.hasNext()) {
                    RevCommit commit = it.next();
                    commits.put(gitfile, commit);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoHeadException e) {
            e.printStackTrace();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
    }
    return commits;
}

From source file:org.modeshape.connector.git.GitFunction.java

License:Apache License

/**
 * Add the first page of commits in the history names of the tags as children of the current node.
 * /*from  ww w .  j a  va  2s.co m*/
 * @param git the Git object; may not be null
 * @param spec the call specification; may not be null
 * @param writer the document writer for the current node; may not be null
 * @param pageSize the number of commits to include, and the number of commits that will be in the next page (if there are
 *        more commits)
 * @throws GitAPIException if there is a problem accessing the Git repository
 */
protected void addCommitsAsChildren(Git git, CallSpecification spec, DocumentWriter writer, int pageSize)
        throws GitAPIException {
    // Add commits in the log ...
    LogCommand command = git.log();
    command.setSkip(0);
    command.setMaxCount(pageSize);

    // Add the first set of commits ...
    int actual = 0;
    String commitId = null;
    for (RevCommit commit : command.call()) {
        commitId = commit.getName();
        writer.addChild(spec.childId(commitId), commitId);
        ++actual;
    }
    if (actual == pageSize) {
        // We wrote the maximum number of commits, so there's (probably) another page ...
        writer.addPage(spec.getId(), commitId, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE);
    }
}

From source file:org.modeshape.connector.git.GitFunction.java

License:Apache License

/**
 * Add an additional page of commits in the history names of the tags as children of the current node.
 * //  w  w  w . j  ava  2s.  c o m
 * @param git the Git object; may not be null
 * @param repository the Repository object; may not be null
 * @param spec the call specification; may not be null
 * @param writer the page writer for the current node; may not be null
 * @param pageKey the page key for this page; may not be null
 * @throws GitAPIException if there is a problem accessing the Git repository
 * @throws IOException if there is a problem reading the Git repository
 */
protected void addCommitsAsPageOfChildren(Git git, Repository repository, CallSpecification spec,
        PageWriter writer, PageKey pageKey) throws GitAPIException, IOException {
    RevWalk walker = new RevWalk(repository);
    try {
        // The offset is the ID of the last commit we read, so we'll need to skip the first commit
        String lastCommitIdName = pageKey.getOffsetString();
        ObjectId lastCommitId = repository.resolve(lastCommitIdName);
        int pageSize = (int) pageKey.getBlockSize();
        // int offset = pageKey.getOffsetInt();
        // int maxCount = pageSize + offset;
        LogCommand command = git.log();
        command.add(lastCommitId);
        command.setMaxCount(pageSize + 1);
        // Add the first set of commits ...
        int actual = 0;
        String commitId = null;
        for (RevCommit commit : command.call()) {
            commitId = commit.getName();
            if (commitId.equals(lastCommitIdName))
                continue;
            writer.addChild(spec.childId(commitId), commitId);
            ++actual;
        }
        if (actual == pageSize) {
            assert commitId != null;
            // We wrote the maximum number of commits, so there's (probably) another page ...
            writer.addPage(pageKey.getParentId(), commitId, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE);
        }
    } finally {
        walker.dispose();
    }
}

From source file:org.modeshape.connector.git.GitFunctionalTest.java

License:Apache License

@Test
public void shouldGetFirstDozenCommitsInHistoryForTag() throws Exception {
    Ref ref = repository.getRef("modeshape-3.0.0.Final");
    ref = repository.peel(ref);/*from www  . j  a v  a2  s  . co  m*/
    RevWalk walker = new RevWalk(repository);
    walker.setRetainBody(true);
    try {
        RevCommit commit = walker.parseCommit(ref.getObjectId());
        LogCommand command = git.log();
        command.add(commit.getId());
        command.setMaxCount(12);
        for (RevCommit rev : command.call()) {
            commit = walker.parseCommit(rev);
            print(commit);
        }
    } finally {
        walker.dispose();
    }
}