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

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

Introduction

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

Prototype

public LogCommand add(AnyObjectId start) throws MissingObjectException, IncorrectObjectTypeException 

Source Link

Document

Mark a commit to start graph traversal from.

Usage

From source file:com.buildautomation.jgit.api.ListTags.java

License:Apache License

public static void listTags() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            List<Ref> call = git.tagList().call();
            for (Ref ref : call) {
                System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());

                // fetch all commits for this tag
                LogCommand log = git.log();

                Ref peeledRef = repository.peel(ref);
                if (peeledRef.getPeeledObjectId() != null) {
                    log.add(peeledRef.getPeeledObjectId());
                } else {
                    log.add(ref.getObjectId());
                }/*  ww  w  .  ja  v a2s. co m*/

                Iterable<RevCommit> logs = log.call();
                for (RevCommit rev : logs) {
                    System.out.println("Commit: "
                            + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
                }
            }
        }
    }
}

From source file:com.madgag.agit.LogFragment.java

License:Open Source License

@Override
public Loader<List<RevCommit>> onCreateLoader(int id, Bundle args) {
    return new AsyncLoader<List<RevCommit>>(getActivity()) {
        public List<RevCommit> loadInBackground() {
            Stopwatch stopwatch = new Stopwatch().start();
            Bundle args = getArguments();
            try {
                Repository repo = new FileRepository(args.getString(GITDIR));

                LogCommand log = new Git(repo).log();
                List<String> untilRevs = getArguments().getStringArrayList(UNTIL_REVS);
                if (untilRevs == null || untilRevs.isEmpty()) {
                    log.all();/*w  w  w.java  2  s  .  c  o m*/
                } else {
                    for (String untilRev : untilRevs) {
                        log.add(repo.resolve(untilRev));
                    }
                }

                List<RevCommit> sampleRevCommits = newArrayList(log.call());

                Log.d(TAG, "Found " + sampleRevCommits.size() + " commits " + stopwatch);

                return sampleRevCommits;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

private static void addHistory(Git git, Trees.Tree tree, ObjectId revision, String path)
        throws MissingObjectException, IncorrectObjectTypeException, GitAPIException {
    LogCommand logCommand = git.log();
    logCommand.add(revision);

    logCommand.addPath(path);//from   w w w  .  j  a  va 2  s. c o m

    for (RevCommit revCommit : logCommand.call()) {
        Commit commit = GitDomain.createCommit(revCommit);
        tree.setLatestCommit(commit);
        break;
    }

}

From source file:edu.nju.cs.inform.jgit.porcelain.ListTags.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            List<Ref> call = git.tagList().call();
            for (Ref ref : call) {
                System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());

                // fetch all commits for this tag
                LogCommand log = git.log();

                Ref peeledRef = repository.peel(ref);
                if (peeledRef.getPeeledObjectId() != null) {
                    log.add(peeledRef.getPeeledObjectId());
                } else {
                    log.add(ref.getObjectId());
                }// w  ww .jav  a2  s .c  o m

                Iterable<RevCommit> logs = log.call();
                for (RevCommit rev : logs) {
                    System.out.println("Commit: "
                            + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
                }
            }
        }
    }
}

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 {//from  w  w  w. j a v  a  2  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   w  w  w  . ja v a  2 s . c o m*/
@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 {/*w  w w.j av a 2  s  . com*/
        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 {/*from  ww w.  java  2s .  c om*/
        // 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 www . j a  v  a  2 s.  co m
        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.orion.server.git.servlets.GitCommitHandlerV1.java

License:Open Source License

private boolean handleGetCommitLog(HttpServletRequest request, HttpServletResponse response, Repository db,
        String refIdsRange, String path) throws AmbiguousObjectException, IOException, ServletException,
        JSONException, URISyntaxException, CoreException {
    int page = request.getParameter("page") != null ? new Integer(request.getParameter("page")).intValue() : 0; //$NON-NLS-1$ //$NON-NLS-2$
    int pageSize = request.getParameter("pageSize") != null //$NON-NLS-1$
            ? new Integer(request.getParameter("pageSize")).intValue() //$NON-NLS-1$
            : PAGE_SIZE;//from   w  w w .  ja v a2 s .c o  m

    ObjectId toObjectId = null;
    ObjectId fromObjectId = null;

    Ref toRefId = null;
    Ref fromRefId = null;

    Git git = new Git(db);
    LogCommand log = git.log();

    if (refIdsRange != null) {
        // git log <since>..<until>
        if (refIdsRange.contains("..")) { //$NON-NLS-1$
            String[] commits = refIdsRange.split("\\.\\."); //$NON-NLS-1$
            if (commits.length != 2) {
                String msg = NLS.bind("Failed to generate commit log for ref {0}", refIdsRange);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
            }

            fromObjectId = db.resolve(commits[0]);
            fromRefId = db.getRef(commits[0]);
            if (fromObjectId == null) {
                String msg = NLS.bind("Failed to generate commit log for ref {0}", commits[0]);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
            }

            toObjectId = db.resolve(commits[1]);
            toRefId = db.getRef(commits[1]);
            if (toObjectId == null) {
                String msg = NLS.bind("No ref or commit found: {0}", commits[1]);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
            }
        } else {
            toObjectId = db.resolve(refIdsRange);
            toRefId = db.getRef(refIdsRange);
            if (toObjectId == null) {
                String msg = NLS.bind("No ref or commit found: {0}", refIdsRange);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
            }
        }

        // set the commit range
        log.add(toObjectId);

        if (fromObjectId != null)
            log.not(fromObjectId);
    } else {
        // git log --all
        // workaround for git log --all - see bug 353310
        List<Ref> branches = git.branchList().setListMode(ListMode.ALL).call();
        for (Ref branch : branches) {
            log.add(branch.getObjectId());
        }
    }

    // set the path filter
    TreeFilter filter = null;

    boolean isRoot = true;
    if (path != null && !"".equals(path)) { //$NON-NLS-1$
        filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(path)),
                TreeFilter.ANY_DIFF);
        log.addPath(path);
        isRoot = false;
    }

    try {
        Iterable<RevCommit> commits = log.call();
        Map<ObjectId, JSONArray> commitToBranchMap = getCommitToBranchMap(db);
        JSONObject result = toJSON(db, OrionServlet.getURI(request), commits, commitToBranchMap, page, pageSize,
                filter, isRoot);

        result.put(GitConstants.KEY_REPOSITORY_PATH, isRoot ? "" : path); //$NON-NLS-1$
        if (refIdsRange == null)
            result.put(GitConstants.KEY_CLONE,
                    BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.COMMIT));
        else
            result.put(GitConstants.KEY_CLONE, BaseToCloneConverter.getCloneLocation(getURI(request),
                    BaseToCloneConverter.COMMIT_REFRANGE));

        if (toRefId != null) {
            result.put(GitConstants.KEY_REMOTE, BaseToRemoteConverter.getRemoteBranchLocation(getURI(request),
                    Repository.shortenRefName(toRefId.getName()), db, BaseToRemoteConverter.REMOVE_FIRST_3));

            String refTargetName = toRefId.getTarget().getName();
            if (refTargetName.startsWith(Constants.R_HEADS)) {
                // this is a branch
                result.put(GitConstants.KEY_LOG_TO_REF,
                        BranchToJSONConverter.toJSON(toRefId.getTarget(), db, getURI(request), 3));
            }
        }
        if (fromRefId != null) {
            String refTargetName = fromRefId.getTarget().getName();
            if (refTargetName.startsWith(Constants.R_HEADS)) {
                // this is a branch
                result.put(GitConstants.KEY_LOG_FROM_REF,
                        BranchToJSONConverter.toJSON(fromRefId.getTarget(), db, getURI(request), 3));
            }
        }

        OrionServlet.writeJSONResponse(request, response, result);
        return true;
    } catch (NoHeadException e) {
        String msg = NLS.bind("No HEAD reference found when generating log for ref {0}", refIdsRange);
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e));
    } catch (JGitInternalException e) {
        String msg = NLS.bind("An internal error occured when generating log for ref {0}", refIdsRange);
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e));
    }
}